You are here

public static function ErrorHelper::handleErrors in Commerce Braintree 8

Translates Braintree errors into Commerce exceptions.

Parameters

object $result: The Braintree result object.

Throws

\Drupal\commerce_payment\Exception\PaymentGatewayException The Commerce exception.

6 calls to ErrorHelper::handleErrors()
HostedFields::capturePayment in src/Plugin/Commerce/PaymentGateway/HostedFields.php
Captures the given authorized payment.
HostedFields::createPayment in src/Plugin/Commerce/PaymentGateway/HostedFields.php
Creates a payment.
HostedFields::deletePaymentMethod in src/Plugin/Commerce/PaymentGateway/HostedFields.php
Deletes the given payment method.
HostedFields::doCreatePaymentMethod in src/Plugin/Commerce/PaymentGateway/HostedFields.php
Creates the payment method on the gateway.
HostedFields::refundPayment in src/Plugin/Commerce/PaymentGateway/HostedFields.php
Refunds the given payment.

... See full list

File

src/ErrorHelper.php, line 64

Class

ErrorHelper
Translates Braintree exceptions and errors into Commerce exceptions.

Namespace

Drupal\commerce_braintree

Code

public static function handleErrors($result) {
  if ($result->success) {
    return;
  }
  $errors = $result->errors
    ->deepAll();
  if (!empty($errors)) {

    // https://developers.braintreepayments.com/reference/general/validation-errors/all/php
    // Validation errors can be due to a module error (mapped to
    // InvalidRequestException) or due to a user input error (mapped to
    // a HardDeclineException).
    $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);
      }
    }
  }

  // Both verification and the transaction can result in the same errors.
  $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') {

    // https://developers.braintreepayments.com/reference/general/processor-responses/authorization-responses
    $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 a fallback exception for everything else.
  throw new InvalidRequestException($result);
}