From e43d638d103025aeb408196ed889c0b7fa8c486b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 4 Nov 2022 14:36:47 +1100 Subject: [PATCH] Improve error handling with Checkout Payment Gateway --- app/PaymentDrivers/CheckoutCom/CreditCard.php | 88 +++++++++++++++++-- app/PaymentDrivers/CheckoutCom/Utilities.php | 5 +- .../CheckoutComPaymentDriver.php | 7 +- 3 files changed, 90 insertions(+), 10 deletions(-) diff --git a/app/PaymentDrivers/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index ffe18c0165b5..27e6c4d67243 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -130,13 +130,38 @@ class CreditCard implements MethodInterface $http_status_code = $e->http_status_code; $error_details = $e->error_details; - throw new PaymentFailed($e->getMessage()); + if(is_array($error_details)) { + $error_details = end($e->error_details['error_codes']); + } + + $human_exception = $error_details ? new \Exception($error_details, 400) : $e; + + + throw new PaymentFailed($human_exception); } catch (CheckoutArgumentException $e) { // Bad arguments - throw new PaymentFailed($e->getMessage()); + + $error_details = $e->error_details; + + if(is_array($error_details)) { + $error_details = end($e->error_details['error_codes']); + } + + $human_exception = $error_details ? new \Exception($error_details, 400) : $e; + + throw new PaymentFailed($human_exception); } catch (CheckoutAuthorizationException $e) { // Bad Invalid authorization - throw new PaymentFailed($e->getMessage()); + + $error_details = $e->error_details; + + if(is_array($error_details)) { + $error_details = end($e->error_details['error_codes']); + } + + $human_exception = $error_details ? new \Exception($error_details, 400) : $e; + + throw new PaymentFailed($human_exception); } } @@ -230,7 +255,6 @@ class CreditCard implements MethodInterface } try { - // $response = $this->checkout->gateway->payments()->request($payment); $response = $this->checkout->gateway->getPaymentsClient()->requestPayment($paymentRequest); @@ -265,21 +289,71 @@ class CreditCard implements MethodInterface $http_status_code = $e->http_status_code; $error_details = $e->error_details; + if(is_array($error_details)) { + $error_details = end($e->error_details['error_codes']); + } + $this->checkout->unWindGatewayFees($this->checkout->payment_hash); - return $this->checkout->processInternallyFailedPayment($this->checkout, $e); + $human_exception = $error_details ? new \Exception($error_details, 400) : $e; + + SystemLogger::dispatch( + $human_exception->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_ERROR, + SystemLog::TYPE_CHECKOUT, + $this->checkout->client, + $this->checkout->client->company, + ); + + return $this->checkout->processInternallyFailedPayment($this->checkout, $human_exception); } catch (CheckoutArgumentException $e) { // Bad arguments + + $error_details = $e->error_details; + + if(is_array($error_details)) { + $error_details = end($e->error_details['error_codes']); + } $this->checkout->unWindGatewayFees($this->checkout->payment_hash); - return $this->checkout->processInternallyFailedPayment($this->checkout, $e); + $human_exception = $error_details ? new \Exception($error_details, 400) : $e; + + SystemLogger::dispatch( + $human_exception->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_ERROR, + SystemLog::TYPE_CHECKOUT, + $this->checkout->client, + $this->checkout->client->company, + ); + + return $this->checkout->processInternallyFailedPayment($this->checkout, $human_exception); } catch (CheckoutAuthorizationException $e) { // Bad Invalid authorization + $error_details = $e->error_details; + + if(is_array($error_details)) { + $error_details = end($e->error_details['error_codes']); + } + $this->checkout->unWindGatewayFees($this->checkout->payment_hash); - return $this->checkout->processInternallyFailedPayment($this->checkout, $e); + $human_exception = $error_details ? new \Exception($error_details, 400) : $e; + + + SystemLogger::dispatch( + $human_exception->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_ERROR, + SystemLog::TYPE_CHECKOUT, + $this->checkout->client, + $this->checkout->client->company, + ); + + return $this->checkout->processInternallyFailedPayment($this->checkout, $human_exception); } } } diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index fc827b3098e5..3c9caef92b25 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -84,10 +84,11 @@ trait Utilities public function processUnsuccessfulPayment($_payment, $throw_exception = true) { + $error_message = ''; - if (array_key_exists('response_summary', $_payment)) { - $error_message = $_payment['response_summary']; + if (array_key_exists('actions', $_payment) && array_key_exists('response_summary', end($_payment['actions']))) { + $error_message = end($_payment['actions'])['response_summary']; } elseif (array_key_exists('status', $_payment)) { $error_message = $_payment['status']; } diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 07a2c6f77906..9e19ac2fe6bb 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -401,8 +401,13 @@ class CheckoutComPaymentDriver extends BaseDriver $this->unWindGatewayFees($payment_hash); $message = $e->getMessage(); + $error_details = ''; + + if(property_exists($e, 'error_details')) + $error_details = $e->error_details; + $data = [ - 'status' => '', + 'status' => $e->error_details, 'error_type' => '', 'error_code' => $e->getCode(), 'param' => '',