Refactor exception handling in stripe

This commit is contained in:
David Bomba 2021-02-02 08:33:04 +11:00
parent df623275b8
commit e9d0ac30e2
3 changed files with 44 additions and 121 deletions

View File

@ -35,7 +35,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
public $company; public $company;
public $amount; public $payment_hash;
public $settings; public $settings;
@ -47,7 +47,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
* @param $company * @param $company
* @param $amount * @param $amount
*/ */
public function __construct($client, $message, $company, $amount) public function __construct($client, $message, $company, $payment_hash)
{ {
$this->company = $company; $this->company = $company;
@ -55,7 +55,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
$this->client = $client; $this->client = $client;
$this->amount = $amount; $this->payment_hash = $payment_hash;
$this->company = $company; $this->company = $company;

View File

@ -335,6 +335,8 @@ class BaseDriver extends AbstractPaymentDriver
public function processInternallyFailedPayment($gateway, $e) public function processInternallyFailedPayment($gateway, $e)
{ {
$this->unWindGatewayFees($this->payment_hash);
if ($e instanceof CheckoutHttpException) { if ($e instanceof CheckoutHttpException) {
$error = $e->getBody(); $error = $e->getBody();
} }
@ -344,7 +346,7 @@ class BaseDriver extends AbstractPaymentDriver
else else
$error = $e->getMessage(); $error = $e->getMessage();
AutoBillingFailureMailer::dispatch( PaymentFailureMailer::dispatch(
$gateway->client, $gateway->client,
$error, $error,
$gateway->client->company, $gateway->client->company,
@ -364,31 +366,7 @@ class BaseDriver extends AbstractPaymentDriver
public function tokenBillingFailed($gateway, $e) public function tokenBillingFailed($gateway, $e)
{ {
$this->unWindGatewayFees($this->payment_hash);
if ($e instanceof CheckoutHttpException) {
$error = $e->getBody();
}
else if ($e instanceof Exception) {
$error = $e->getMessage();
}
else
$error = $e->getMessage();
AutoBillingFailureMailer::dispatch(
$gateway->client,
$error,
$gateway->client->company,
$this->payment_hash
);
SystemLogger::dispatch(
$gateway->payment_hash,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
$gateway::SYSTEM_LOG_TYPE,
$gateway->client,
);
} }

View File

@ -79,102 +79,47 @@ class Charge
]); ]);
SystemLogger::dispatch($response, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client); SystemLogger::dispatch($response, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (CardException $e) { } catch (\Exception $e) {
// Since it's a decline, \Stripe\Exception\CardException will be caught
$data = [ $data =[
'status' => $e->getHttpStatus(), 'status' => '',
'error_type' => $e->getError()->type, 'error_type' => '',
'error_code' => $e->getError()->code, 'error_code' => '',
'param' => $e->getError()->param, 'param' => '',
'message' => $e->getError()->message, 'message' => '',
]; ];
$this->stripe->tokenBillingFailed($this->stripe, $e);
switch ($e) {
case ($e instanceof CardException):
$data['status'] => $e->getHttpStatus();
$data['error_type'] => $e->getError()->type;
$data['error_code'] => $e->getError()->code;
$data['param'] => $e->getError()->param;
$data['message'] => $e->getError()->message;
break;
case ($e instanceof RateLimitException):
$data['message'] => 'Too many requests made to the API too quickly';
break;
case ($e instanceof InvalidRequestException):
$data['message'] => 'Invalid parameters were supplied to Stripe\'s API';
break;
case ($e instanceof AuthenticationException):
$data['message'] => 'Authentication with Stripe\'s API failed';
break;
case ($e instanceof ApiErrorException):
$data['message'] => 'Network communication with Stripe failed';
break;
default:
$data['message'] => $e->getMessage();
break;
}
$this->stripe->processInternallyFailedPayment($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client); SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (RateLimitException $e) { }
// Too many requests made to the API too quickly
$data = [
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => 'Too many requests made to the API too quickly',
];
$this->stripe->tokenBillingFailed($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (InvalidRequestException $e) {
// Invalid parameters were supplied to Stripe's API
//
$data = [
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => 'Invalid parameters were supplied to Stripe\'s API',
];
$this->stripe->tokenBillingFailed($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (AuthenticationException $e) {
// Authentication with Stripe's API failed
$data = [
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => 'Authentication with Stripe\'s API failed',
];
$this->stripe->tokenBillingFailed($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (ApiConnectionException $e) {
// Network communication with Stripe failed
$data = [
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => 'Network communication with Stripe failed',
];
$this->stripe->tokenBillingFailed($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (ApiErrorException $e) {
$data = [
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => 'API Error',
];
$this->stripe->tokenBillingFailed($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
//
$data = [
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => $e->getMessage(),
];
$this->stripe->tokenBillingFailed($this->stripe, $e);
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
}
if (! $response) { if (! $response) {
return false; return false;