Loading commerce_omise.services.yml 0 → 100644 +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]; } } src/OmiseUtilInterface.php 0 → 100644 +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); } src/Plugin/Commerce/PaymentGateway/Omise.php +72 −59 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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} */ Loading Loading @@ -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, ]; Loading Loading @@ -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, ]; Loading Loading @@ -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); Loading Loading @@ -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']; Loading Loading @@ -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; } } Loading
commerce_omise.services.yml 0 → 100644 +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]; } }
src/OmiseUtilInterface.php 0 → 100644 +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); }
src/Plugin/Commerce/PaymentGateway/Omise.php +72 −59 Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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} */ Loading Loading @@ -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, ]; Loading Loading @@ -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, ]; Loading Loading @@ -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); Loading Loading @@ -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']; Loading Loading @@ -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; } }