View source
<?php
namespace Drupal\commerce_braintree;
use Drupal\commerce_payment\Exception\AuthenticationException;
use Drupal\commerce_payment\Exception\HardDeclineException;
use Drupal\commerce_payment\Exception\InvalidRequestException;
use Drupal\commerce_payment\Exception\InvalidResponseException;
use Drupal\commerce_payment\Exception\SoftDeclineException;
class ErrorHelper {
public static function handleException(\Braintree\Exception $exception) {
if ($exception instanceof \Braintree\Exception\Authentication) {
throw new AuthenticationException('Braintree authentication failed.');
}
elseif ($exception instanceof \Braintree\Exception\Authorization) {
throw new AuthenticationException('The used API key is not authorized to perform the attempted action.');
}
elseif ($exception instanceof \Braintree\Exception\NotFound) {
throw new InvalidRequestException('Braintree resource not found.');
}
elseif ($exception instanceof \Braintree\Exception\UpgradeRequired) {
throw new InvalidRequestException('The Braintree client library needs to be updated.');
}
elseif ($exception instanceof \Braintree\Exception\TooManyRequests) {
throw new InvalidRequestException('Too many requests.');
}
elseif ($exception instanceof \Braintree\Exception\ServerError) {
throw new InvalidResponseException('Server error.');
}
elseif ($exception instanceof \Braintree\Exception\DownForMaintenance) {
throw new InvalidResponseException('Request timed out.');
}
else {
throw new InvalidResponseException($exception
->getMessage());
}
}
public static function handleErrors($result) {
if ($result->success) {
return;
}
$errors = $result->errors
->deepAll();
if (!empty($errors)) {
$hard_decline_codes = [
81813,
91828,
81736,
81737,
81750,
91568,
];
foreach ($errors as $error) {
if (in_array($error->code, $hard_decline_codes)) {
throw new HardDeclineException($error->message, $error->code);
}
else {
throw new InvalidRequestException($error->message, $error->code);
}
}
}
$error_statuses = [
'settlement_declined',
'gateway_rejected',
'processor_declined',
];
if ($result->verification && in_array($result->verification->status, $error_statuses)) {
$error = $result->verification;
$status = $result->verification->status;
}
elseif ($result->transaction && in_array($result->transaction->status, $error_statuses)) {
$error = $result->transaction;
$status = $result->transaction->status;
}
if ($status == 'settlement_declined') {
$code = $error->processorSettlementResponseCode;
$text = $error->processorSettlementResponseText;
throw new HardDeclineException($text, $code);
}
elseif ($status == 'gateway_rejected') {
$reason = $error->gatewayRejectionReason;
throw new HardDeclineException('Rejected by the gateway. Reason: ' . $reason);
}
elseif ($status == 'processor_declined') {
$soft_decline_codes = [
2000,
2001,
2002,
2003,
2009,
2016,
2021,
2025,
2026,
2033,
2034,
2035,
2038,
2040,
2042,
2046,
2048,
2050,
2054,
2057,
2062,
];
$code = $error->processorResponseCode;
$text = $error->processorResponseText;
if (!empty($error->additionalProcessorResponse)) {
$text .= ' (' . $error->additionalProcessorResponse . ')';
}
if (in_array($code, $soft_decline_codes) || $code >= 2092 && $code <= 3000) {
throw new SoftDeclineException($text, $code);
}
else {
throw new HardDeclineException($text, $code);
}
}
throw new InvalidRequestException($result);
}
public static function handleErrors3ds($result, $required = FALSE) {
if (empty($result)) {
throw new InvalidRequestException(sprintf('The nonce was not 3D Secured'));
}
if ($result->liabilityShifted) {
return;
}
if ($result->liabilityShiftPossible || $result->status == 'authentication_unavailable') {
throw new SoftDeclineException($result->status);
}
if ($required) {
throw new HardDeclineException($result->status);
}
}
}