Commit f018bc1b authored by Vadym Abramchuk's avatar Vadym Abramchuk Committed by Vadym Abramchuk
Browse files

Issue #3270758 by abramm: Move exceptions handling to commerce_omise.util service

parent a7c5623b
Loading
Loading
Loading
Loading

src/ErrorHelper.php

deleted100644 → 0
+0 −44
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_omise;

use Drupal\commerce_payment\Exception\AuthenticationException;
use Drupal\commerce_payment\Exception\DeclineException;
use Drupal\commerce_payment\Exception\InvalidRequestException;
use Drupal\commerce_payment\Exception\InvalidResponseException;

use OmiseException;
use OmiseInvalidCardException;
use OmiseBadRequestException;
use OmiseAuthenticationFailureException;

/**
 * Translates Omise exceptions and errors into Commerce exceptions.
 */
class ErrorHelper {

  /**
   * Translates Omise exceptions into Commerce exceptions.
   *
   * @param \OmiseException $exception
   *   The Omise exception.
   *
   * @throws \Drupal\commerce_payment\Exception\PaymentGatewayException
   *   The Commerce exception.
   */
  public static function handleException(OmiseException $exception) {
    if ($exception instanceof OmiseInvalidCardException) {
      throw new DeclineException('We encountered an error processing your card details. Please verify your details and try again.');
    }
    elseif ($exception instanceof OmiseBadRequestException) {
      throw new InvalidRequestException('Invalid parameters were supplied to Omise\'s API.');
    }
    elseif ($exception instanceof OmiseAuthenticationFailureException) {
      throw new AuthenticationException('Omise authentication failed.');
    }
    else {
      throw new InvalidResponseException($exception->getMessage());
    }
  }

}
+22 −0
Original line number Diff line number Diff line
@@ -2,7 +2,11 @@

namespace Drupal\commerce_omise;

use Drupal\commerce_payment\Exception\AuthenticationException;
use Drupal\commerce_payment\Exception\DeclineException;
use Drupal\commerce_payment\Exception\HardDeclineException;
use Drupal\commerce_payment\Exception\InvalidRequestException;
use Drupal\commerce_payment\Exception\InvalidResponseException;
use Drupal\commerce_price\Price;

/**
@@ -50,4 +54,22 @@ class OmiseUtil implements OmiseUtilInterface {
    return $map[$cardType];
  }

  /**
   * {@inheritdoc}
   */
  public function handleException(\OmiseException $exception) {
    if ($exception instanceof \OmiseInvalidCardException) {
      throw new DeclineException('We encountered an error processing your card details. Please verify your details and try again.');
    }
    elseif ($exception instanceof \OmiseBadRequestException) {
      throw new InvalidRequestException('Invalid parameters were supplied to Omise\'s API.');
    }
    elseif ($exception instanceof \OmiseAuthenticationFailureException) {
      throw new AuthenticationException('Omise authentication failed.');
    }
    else {
      throw new InvalidResponseException($exception->getMessage());
    }
  }

}
+11 −0
Original line number Diff line number Diff line
@@ -31,4 +31,15 @@ interface OmiseUtilInterface {
   */
  public function mapCreditCardType($cardType);

  /**
   * Translates Omise exceptions into Commerce exceptions.
   *
   * @param \OmiseException $exception
   *   The Omise exception.
   *
   * @throws \Drupal\commerce_payment\Exception\PaymentGatewayException
   *   The Commerce exception.
   */
  public function handleException(\OmiseException $exception);

}
+6 −7
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\commerce_omise\Plugin\Commerce\PaymentGateway;

use Drupal\commerce_omise\ErrorHelper;
use Drupal\commerce_omise\OmiseUtilInterface;
use Drupal\commerce_payment\CreditCard;
use Drupal\commerce_payment\Entity\PaymentInterface;
@@ -185,7 +184,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
      }
    }
    catch (\OmiseException $e) {
      ErrorHelper::handleException($e);
      $this->util->handleException($e);
    }

    $next_state = $capture ? 'completed' : 'authorization';
@@ -208,7 +207,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
      $charge->capture();
    }
    catch (\OmiseException $e) {
      ErrorHelper::handleException($e);
      $this->util->handleException($e);
    }

    $payment->setState('completed');
@@ -234,7 +233,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
      $charge->refunds()->create($data);
    }
    catch (\OmiseException $e) {
      ErrorHelper::handleException($e);
      $this->util->handleException($e);
    }

    // Update payment.
@@ -261,7 +260,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
      $charge->refunds()->create($data);
    }
    catch (\OmiseException $e) {
      ErrorHelper::handleException($e);
      $this->util->handleException($e);
    }

    $old_refunded_amount = $payment->getRefundedAmount();
@@ -317,7 +316,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
      }
    }
    catch (\OmiseException $e) {
      ErrorHelper::handleException($e);
      $this->util->handleException($e);
    }

    $payment_method->delete();
@@ -382,7 +381,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
        }
      }
      catch (\OmiseException $e) {
        ErrorHelper::handleException($e);
        $this->util->handleException($e);
      }
    }
    else {