Loading commerce_omise.services.yml +1 −0 Original line number Diff line number Diff line services: commerce_omise.util: class: Drupal\commerce_omise\OmiseUtil arguments: ["@entity_type.manager"] src/OmiseUtil.php +74 −0 Original line number Diff line number Diff line Loading @@ -2,18 +2,46 @@ namespace Drupal\commerce_omise; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_payment\Entity\PaymentGatewayInterface; 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; use Drupal\Core\Entity\EntityTypeManagerInterface; /** * The Omise utilities. */ class OmiseUtil implements OmiseUtilInterface { /** * The commerce_payment entity type storage. * * @var \Drupal\commerce_payment\PaymentStorageInterface */ protected $paymentStorage; /** * The commerce_payment_gateway config entity type storage. * * @var \Drupal\commerce_payment\PaymentGatewayStorageInterface */ protected $paymentGatewayStorage; /** * Constructs new OmiseUtil object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. */ public function __construct(EntityTypeManagerInterface $entityTypeManager) { $this->paymentStorage = $entityTypeManager->getStorage('commerce_payment'); $this->paymentGatewayStorage = $entityTypeManager->getStorage('commerce_payment_gateway'); } /** * {@inheritdoc} */ Loading Loading @@ -72,4 +100,50 @@ class OmiseUtil implements OmiseUtilInterface { } } /** * {@inheritdoc} */ public function getOrderPayment(OrderInterface $order) { $payment_ids = $this ->paymentStorage ->getQuery() ->condition('order_id', $order->id()) ->condition('payment_gateway', $this->getOmisePaymentGatewaysIds(), 'IN') ->execute(); if (empty($payment_ids)) { // Throw error early if the query returned empty result. throw new \InvalidArgumentException('No Omise payments found for order ID ' . $order->id()); } $payments = $this ->paymentStorage ->loadMultiple($payment_ids); if (count($payments) > 1) { throw new \InvalidArgumentException('More than one Omise payment found for order ID ' . $order->id()); } $payment = reset($payments); if (!$payment) { throw new \InvalidArgumentException('No Omise payments found for order ID ' . $order->id()); } return $payment; } /** * Returns list of IDs of all enabled Omise payment gateways. */ protected function getOmisePaymentGatewaysIds() { $paymentGateways = array_filter( $this->paymentGatewayStorage->loadMultiple(), function (PaymentGatewayInterface $gateway) { return $gateway->getPluginId() === 'omise' && $gateway->status(); }); return array_map(function (PaymentGatewayInterface $gateway) { return $gateway->id(); }, $paymentGateways); } } src/OmiseUtilInterface.php +15 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\commerce_omise; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_price\Price; /** Loading Loading @@ -42,4 +43,18 @@ interface OmiseUtilInterface { */ public function handleException(\OmiseException $exception); /** * Retrieves Omise payment from the order. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order entity. * * @return \Drupal\commerce_payment\Entity\PaymentInterface * The Omise payment entity corresponding given order. * * @throws \InvalidArgumentException * Thrown if there are no or more than one Omise payments for the order. */ public function getOrderPayment(OrderInterface $order); } Loading
commerce_omise.services.yml +1 −0 Original line number Diff line number Diff line services: commerce_omise.util: class: Drupal\commerce_omise\OmiseUtil arguments: ["@entity_type.manager"]
src/OmiseUtil.php +74 −0 Original line number Diff line number Diff line Loading @@ -2,18 +2,46 @@ namespace Drupal\commerce_omise; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_payment\Entity\PaymentGatewayInterface; 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; use Drupal\Core\Entity\EntityTypeManagerInterface; /** * The Omise utilities. */ class OmiseUtil implements OmiseUtilInterface { /** * The commerce_payment entity type storage. * * @var \Drupal\commerce_payment\PaymentStorageInterface */ protected $paymentStorage; /** * The commerce_payment_gateway config entity type storage. * * @var \Drupal\commerce_payment\PaymentGatewayStorageInterface */ protected $paymentGatewayStorage; /** * Constructs new OmiseUtil object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. */ public function __construct(EntityTypeManagerInterface $entityTypeManager) { $this->paymentStorage = $entityTypeManager->getStorage('commerce_payment'); $this->paymentGatewayStorage = $entityTypeManager->getStorage('commerce_payment_gateway'); } /** * {@inheritdoc} */ Loading Loading @@ -72,4 +100,50 @@ class OmiseUtil implements OmiseUtilInterface { } } /** * {@inheritdoc} */ public function getOrderPayment(OrderInterface $order) { $payment_ids = $this ->paymentStorage ->getQuery() ->condition('order_id', $order->id()) ->condition('payment_gateway', $this->getOmisePaymentGatewaysIds(), 'IN') ->execute(); if (empty($payment_ids)) { // Throw error early if the query returned empty result. throw new \InvalidArgumentException('No Omise payments found for order ID ' . $order->id()); } $payments = $this ->paymentStorage ->loadMultiple($payment_ids); if (count($payments) > 1) { throw new \InvalidArgumentException('More than one Omise payment found for order ID ' . $order->id()); } $payment = reset($payments); if (!$payment) { throw new \InvalidArgumentException('No Omise payments found for order ID ' . $order->id()); } return $payment; } /** * Returns list of IDs of all enabled Omise payment gateways. */ protected function getOmisePaymentGatewaysIds() { $paymentGateways = array_filter( $this->paymentGatewayStorage->loadMultiple(), function (PaymentGatewayInterface $gateway) { return $gateway->getPluginId() === 'omise' && $gateway->status(); }); return array_map(function (PaymentGatewayInterface $gateway) { return $gateway->id(); }, $paymentGateways); } }
src/OmiseUtilInterface.php +15 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\commerce_omise; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_price\Price; /** Loading Loading @@ -42,4 +43,18 @@ interface OmiseUtilInterface { */ public function handleException(\OmiseException $exception); /** * Retrieves Omise payment from the order. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order entity. * * @return \Drupal\commerce_payment\Entity\PaymentInterface * The Omise payment entity corresponding given order. * * @throws \InvalidArgumentException * Thrown if there are no or more than one Omise payments for the order. */ public function getOrderPayment(OrderInterface $order); }