custom/plugins/SwagAmazonPay/src/DataAbstractionLayer/EventListeners/AmazonPayTransitionListener.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) shopware AG <info@shopware.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Swag\AmazonPay\DataAbstractionLayer\EventListeners;
  9. use Psr\Log\LoggerInterface;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
  15. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  16. use Swag\AmazonPay\Components\Client\Service\ChargeServiceInterface;
  17. use Swag\AmazonPay\Components\Config\ConfigServiceInterface;
  18. use Swag\AmazonPay\Components\Config\Struct\AmazonPayConfigStruct;
  19. use Swag\AmazonPay\Installer\CustomFieldsInstaller;
  20. class AmazonPayTransitionListener
  21. {
  22.     /**
  23.      * @var EntityRepositoryInterface
  24.      */
  25.     private $orderRepository;
  26.     /**
  27.      * @var ChargeServiceInterface
  28.      */
  29.     private $chargeService;
  30.     /**
  31.      * @var ConfigServiceInterface
  32.      */
  33.     private $configService;
  34.     /**
  35.      * @var LoggerInterface
  36.      */
  37.     private $logger;
  38.     public function __construct(
  39.         EntityRepositoryInterface $orderRepository,
  40.         ChargeServiceInterface $chargeService,
  41.         ConfigServiceInterface $configService,
  42.         LoggerInterface $logger
  43.     ) {
  44.         $this->orderRepository $orderRepository;
  45.         $this->chargeService $chargeService;
  46.         $this->configService $configService;
  47.         $this->logger $logger;
  48.     }
  49.     public function onOrderStateChange(StateMachineStateChangeEvent $event): void
  50.     {
  51.         try {
  52.             $orderId $event->getTransition()->getEntityId();
  53.             $order $this->getOrderById($orderId$event->getContext());
  54.             if (!$order) {
  55.                 return;
  56.             }
  57.             $this->chargeOnOrderStateChange($order$event->getNextState(), $event->getContext());
  58.         } catch (\Throwable $exception) {
  59.             $this->logger->error('An error occurred while charging on order state change', ['Exception' => $exception->getMessage()]);
  60.         }
  61.     }
  62.     private function getOrderById(string $orderIdContext $context): ?OrderEntity
  63.     {
  64.         $criteria = new Criteria([$orderId]);
  65.         $criteria->addAssociation('transactions');
  66.         $criteria->addAssociation('currency');
  67.         return $this->orderRepository->search(
  68.             $criteria,
  69.             $context
  70.         )->first();
  71.     }
  72.     private function chargeOnOrderStateChange(OrderEntity $orderStateMachineStateEntity $stateContext $context): void
  73.     {
  74.         $pluginConfig $this->configService->getPluginConfig($order->getSalesChannelId());
  75.         $transactions $order->getTransactions();
  76.         if ($pluginConfig->getChargeMode() !== AmazonPayConfigStruct::CHARGE_MODE_SHIPPING) {
  77.             return;
  78.         }
  79.         if ($transactions === null) {
  80.             return;
  81.         }
  82.         $transaction $transactions->first();
  83.         if ($transaction === null) {
  84.             return;
  85.         }
  86.         $customFields $transaction->getCustomFields();
  87.         if (empty($customFields) || empty($customFields[CustomFieldsInstaller::CUSTOM_FIELD_NAME_CHARGE_ID])) {
  88.             return;
  89.         }
  90.         $chargeId $customFields[CustomFieldsInstaller::CUSTOM_FIELD_NAME_CHARGE_ID];
  91.         if ($state->getId() !== $pluginConfig->getOrderChargeTriggerState()) {
  92.             return;
  93.         }
  94.         $softDescriptor $this->configService->getSoftDescriptor($order->getSalesChannelId());
  95.         $currency $order->getCurrency();
  96.         $currencyCode $currency $currency->getIsoCode() : null;
  97.         $this->chargeService->charge(
  98.             $chargeId,
  99.             $order->getAmountTotal(),
  100.             $softDescriptor,
  101.             $currencyCode,
  102.             $context
  103.         );
  104.     }
  105. }