From 9180dc4478d7a6a24d28065f9bdb285783a58116 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 24 May 2024 10:38:07 +1000 Subject: [PATCH] Refactor for paypal implementation --- .../PayPal/PayPalBasePaymentDriver.php | 23 ++++++++- .../PayPalPPCPPaymentDriver.php | 14 ++++- .../PayPalRestPaymentDriver.php | 20 ++++++-- composer.lock | 12 ++--- .../ninja2020/gateways/paypal/pay.blade.php | 51 ++++++++++++++++++- .../gateways/paypal/ppcp/card.blade.php | 47 ++++++++++++++--- .../gateways/paypal/ppcp/pay.blade.php | 42 +++++++++++++-- 7 files changed, 181 insertions(+), 28 deletions(-) diff --git a/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php b/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php index a3059c20cdcd..b2f5ea5bf36c 100644 --- a/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php +++ b/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php @@ -344,10 +344,15 @@ class PayPalBasePaymentDriver extends BaseDriver ->withHeaders($this->getHeaders($headers)) ->{$verb}("{$this->api_endpoint_url}{$uri}", $data); - if($r->successful()) { + if($r->status() <= 422){ + // if($r->successful()) { return $r; } + nlog($r->body()); + nlog($r->json()); + nlog($r); + SystemLogger::dispatch( ['response' => $r->body()], SystemLog::CATEGORY_GATEWAY_RESPONSE, @@ -357,8 +362,21 @@ class PayPalBasePaymentDriver extends BaseDriver $this->client->company ?? $this->company_gateway->company, ); - throw new PaymentFailed("Gateway failure - {$r->body()}", 401); + + return response()->json(['message' => "Gateway failure - {$r->body()}"], 401); + // throw new PaymentFailed("Gateway failure - {$r->body()}", 401); + + } + + public function handleRetry($response, $request) { + + // $response = $r->json(); + // nlog($response['details']); + + // if(in_array($response['details'][0]['issue'], ['INSTRUMENT_DECLINED', 'PAYER_ACTION_REQUIRED'])) + + return response()->json($response->json()); } /** @@ -369,6 +387,7 @@ class PayPalBasePaymentDriver extends BaseDriver */ public function getHeaders(array $headers = []): array { + return array_merge([ 'Accept' => 'application/json', 'Content-type' => 'application/json', diff --git a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php index 055e41d1587e..47141b678dfa 100644 --- a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php +++ b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php @@ -146,6 +146,12 @@ class PayPalPPCPPaymentDriver extends PayPalBasePaymentDriver try { $r = $this->gatewayRequest("/v2/checkout/orders/{$orderID}/capture", 'post', ['body' => '']); + + if($r->status() == 422) { + //handle conditions where the client may need to try again. + return $this->handleRetry($r, $request); + } + } catch(\Exception $e) { //Rescue for duplicate invoice_id @@ -192,8 +198,10 @@ class PayPalPPCPPaymentDriver extends PayPalBasePaymentDriver $this->client, $this->client->company, ); + + return response()->json(['redirect' => route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)], false)]); - return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); + // return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } else { @@ -212,7 +220,9 @@ class PayPalPPCPPaymentDriver extends PayPalBasePaymentDriver $message = $response['body']['details'][0]['description'] ?? 'Payment failed. Please try again.'; - throw new PaymentFailed($message, 400); + return response()->json(['message' => $message], 400); + + // throw new PaymentFailed($message, 400); } } diff --git a/app/PaymentDrivers/PayPalRestPaymentDriver.php b/app/PaymentDrivers/PayPalRestPaymentDriver.php index 24f44b127487..5e19429d6ff2 100644 --- a/app/PaymentDrivers/PayPalRestPaymentDriver.php +++ b/app/PaymentDrivers/PayPalRestPaymentDriver.php @@ -66,6 +66,8 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver $request['gateway_response'] = str_replace("Error: ", "", $request['gateway_response']); $response = json_decode($request['gateway_response'], true); + nlog($response); + if($request->has('token') && strlen($request->input('token')) > 2) return $this->processTokenPayment($request, $response); @@ -93,7 +95,14 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver } try{ + $r = $this->gatewayRequest("/v2/checkout/orders/{$orderID}/capture", 'post', ['body' => '']); + + if($r->status() == 422){ + //handle conditions where the client may need to try again. + return $this->handleRetry($r, $request); + } + } catch(\Exception $e) { @@ -146,7 +155,9 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver $message = $response['body']['details'][0]['description'] ?? 'Payment failed. Please try again.'; - throw new PaymentFailed($message, 400); + return response()->json(['message' => $message], 400); + + //throw new PaymentFailed($message, 400); } } @@ -202,7 +213,9 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver $this->client->company, ); - return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); + return response()->json(['redirect' => route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)], false)]); + + // return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } @@ -257,10 +270,9 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver if(isset($data['payment_source'])) $order['payment_source'] = $data['payment_source']; - - $r = $this->gatewayRequest('/v2/checkout/orders', 'post', $order); + nlog($r->json()); return $r->json()['id']; } diff --git a/composer.lock b/composer.lock index 820a30388b32..83821bd4ea41 100644 --- a/composer.lock +++ b/composer.lock @@ -4588,16 +4588,16 @@ }, { "name": "horstoeko/zugferd", - "version": "v1.0.45", + "version": "v1.0.47", "source": { "type": "git", "url": "https://github.com/horstoeko/zugferd.git", - "reference": "b778941ebe5b262061443e375e2f6f46bf2a7cec" + "reference": "7a5a8b7bb44f7aebfd66af112662c30383f31dd9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/horstoeko/zugferd/zipball/b778941ebe5b262061443e375e2f6f46bf2a7cec", - "reference": "b778941ebe5b262061443e375e2f6f46bf2a7cec", + "url": "https://api.github.com/repos/horstoeko/zugferd/zipball/7a5a8b7bb44f7aebfd66af112662c30383f31dd9", + "reference": "7a5a8b7bb44f7aebfd66af112662c30383f31dd9", "shasum": "" }, "require": { @@ -4657,9 +4657,9 @@ ], "support": { "issues": "https://github.com/horstoeko/zugferd/issues", - "source": "https://github.com/horstoeko/zugferd/tree/v1.0.45" + "source": "https://github.com/horstoeko/zugferd/tree/v1.0.47" }, - "time": "2024-05-21T14:57:41+00:00" + "time": "2024-05-23T03:12:53+00:00" }, { "name": "http-interop/http-factory-guzzle", diff --git a/resources/views/portal/ninja2020/gateways/paypal/pay.blade.php b/resources/views/portal/ninja2020/gateways/paypal/pay.blade.php index 05a9f6cf84f0..dfc319ddb1fd 100644 --- a/resources/views/portal/ninja2020/gateways/paypal/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/paypal/pay.blade.php @@ -26,7 +26,7 @@ @push('footer') -
+ -