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

Issue #3270758 by abramm: Introduce new commerce_omise.util service

parent 02831db4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
services:
  commerce_omise.util:
    class: Drupal\commerce_omise\OmiseUtil

src/OmiseUtil.php

0 → 100644
+53 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_omise;

use Drupal\commerce_payment\Exception\HardDeclineException;
use Drupal\commerce_price\Price;

/**
 * The Omise utilities.
 */
class OmiseUtil implements OmiseUtilInterface {

  /**
   * {@inheritdoc}
   */
  public function formatNumber(Price $amount) {
    $amount_number = $amount->getNumber();

    switch ($amount->getCurrencyCode()) {
      case 'THB':
      case 'SGD':
      case 'USD':
      case 'EUR':
      case 'GBP':
        $amount_number = $amount_number * 100;
        break;

      default:
        break;
    }

    return number_format($amount_number, 0, '.', '');
  }

  /**
   * {@inheritdoc}
   */
  public function mapCreditCardType($cardType) {
    $map = [
      'American Express' => 'amex',
      'Diners Club' => 'dinersclub',
      'JCB' => 'jcb',
      'MasterCard' => 'mastercard',
      'Visa' => 'visa',
    ];
    if (!isset($map[$cardType])) {
      throw new HardDeclineException(sprintf('Unsupported credit card type "%s".', $cardType));
    }

    return $map[$cardType];
  }

}
+34 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_omise;

use Drupal\commerce_price\Price;

/**
 * The Omise utilities service interface.
 */
interface OmiseUtilInterface {

  /**
   * Formats the charge amount for Omise.
   *
   * @param \Drupal\commerce_price\Price $amount
   *   The amount being charged.
   *
   * @return int
   *   The Omise formatted amount.
   */
  public function formatNumber(Price $amount);

  /**
   * Maps the Omise credit card type to a Commerce credit card type.
   *
   * @param string $cardType
   *   The Omise credit card type.
   *
   * @return string
   *   The Commerce credit card type.
   */
  public function mapCreditCardType($cardType);

}
+72 −59
Original line number Diff line number Diff line
@@ -3,13 +3,19 @@
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;
use Drupal\commerce_payment\Entity\PaymentMethodInterface;
use Drupal\commerce_payment\Exception\HardDeclineException;
use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayBase;
use Drupal\commerce_price\MinorUnitsConverterInterface;
use Drupal\commerce_price\Price;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the Omise payment gateway.
@@ -31,6 +37,67 @@ use Drupal\Core\Form\FormStateInterface;
 */
class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {

  /**
   * Omise utils.
   *
   * @var \Drupal\commerce_omise\OmiseUtilInterface
   */
  protected OmiseUtilInterface $util;

  /**
   * Constructs new Omise object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce_payment\PaymentTypeManager $payment_type_manager
   *   The payment type manager.
   * @param \Drupal\commerce_payment\PaymentMethodTypeManager $payment_method_type_manager
   *   The payment method type manager.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time.
   * @param \Drupal\commerce_price\MinorUnitsConverterInterface $minor_units_converter
   *   The minor units converter.
   * @param \Drupal\commerce_omise\OmiseUtilInterface $util
   *   Omise utils.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    EntityTypeManagerInterface $entity_type_manager,
    PaymentTypeManager $payment_type_manager,
    PaymentMethodTypeManager $payment_method_type_manager,
    TimeInterface $time,
    MinorUnitsConverterInterface $minor_units_converter,
    OmiseUtilInterface $util
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time, $minor_units_converter);
    $this->util = $util;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get('plugin.manager.commerce_payment_type'),
      $container->get('plugin.manager.commerce_payment_method_type'),
      $container->get('datetime.time'),
      $container->get('commerce_price.minor_units_converter'),
      $container->get('commerce_omise.util')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -96,7 +163,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {

    $transaction_data = [
      'currency' => $currency_code,
      'amount' => $this->formatNumber($amount),
      'amount' => $this->util->formatNumber($amount),
      'capture' => $capture,
    ];

@@ -160,7 +227,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
      $remote_id = $payment->getRemoteId();
      $charge = \OmiseCharge::retrieve($remote_id, $this->configuration['public_key'], $this->configuration['secret_key']);
      $data = [
        'amount' => $this->formatNumber($amount),
        'amount' => $this->util->formatNumber($amount),
        'void' => TRUE,
      ];

@@ -188,7 +255,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
    try {
      $remote_id = $payment->getRemoteId();
      $data = [
        'amount' => $this->formatNumber($amount),
        'amount' => $this->util->formatNumber($amount),
      ];
      $charge = \OmiseCharge::retrieve($remote_id, $this->configuration['public_key'], $this->configuration['secret_key']);
      $charge->refunds()->create($data);
@@ -226,7 +293,7 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
    }

    $remote_payment_method = $this->doCreatePaymentMethod($payment_method, $payment_details);
    $payment_method->card_type = $this->mapCreditCardType($remote_payment_method['brand']);
    $payment_method->card_type = $this->util->mapCreditCardType($remote_payment_method['brand']);
    $payment_method->card_number = $remote_payment_method['last_digits'];
    $payment_method->card_exp_month = $remote_payment_method['expiration_month'];
    $payment_method->card_exp_year = $remote_payment_method['expiration_year'];
@@ -328,58 +395,4 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
    return [];
  }

  /**
   * Maps the Omise credit card type to a Commerce credit card type.
   *
   * @param string $card_type
   *   The Omise credit card type.
   *
   * @return string
   *   The Commerce credit card type.
   */
  protected function mapCreditCardType($card_type) {
    $map = [
      'American Express' => 'amex',
      'Diners Club' => 'dinersclub',
      'JCB' => 'jcb',
      'MasterCard' => 'mastercard',
      'Visa' => 'visa',
    ];
    if (!isset($map[$card_type])) {
      throw new HardDeclineException(sprintf('Unsupported credit card type "%s".', $card_type));
    }

    return $map[$card_type];
  }

  /**
   * Formats the charge amount for Omise.
   *
   * @param \Drupal\commerce_price\Price $amount
   *   The amount being charged.
   *
   * @return int
   *   The Omise formatted amount.
   */
  protected function formatNumber(Price $amount) {
    $amount_number = $amount->getNumber();

    switch ($amount->getCurrencyCode()) {
      case 'THB':
      case 'SGD':
      case 'USD':
      case 'EUR':
      case 'GBP':
        $amount_number = $amount_number * 100;
        break;

      default:
        break;
    }

    $amount_number = number_format($amount_number, 0, '.', '');

    return $amount_number;
  }

}