custom/plugins/MolliePayments/src/Compatibility/Bundles/FlowBuilder/Actions/RefundOrderAction.php line 88

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Compatibility\Bundles\FlowBuilder\Actions;
  3. use Kiener\MolliePayments\Components\RefundManager\RefundManagerInterface;
  4. use Kiener\MolliePayments\Components\RefundManager\Request\RefundRequest;
  5. use Kiener\MolliePayments\Service\OrderServiceInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  8. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\Event\FlowEvent;
  11. use Shopware\Core\Framework\Event\OrderAware;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class RefundOrderAction extends FlowAction implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var OrderServiceInterface
  17.      */
  18.     private $orderService;
  19.     /**
  20.      * @var RefundManagerInterface
  21.      */
  22.     private $refundManager;
  23.     /**
  24.      * @var LoggerInterface
  25.      */
  26.     private $logger;
  27.     /**
  28.      * @param OrderServiceInterface $orderService
  29.      * @param RefundManagerInterface $refundManager
  30.      * @param LoggerInterface $logger
  31.      */
  32.     public function __construct(OrderServiceInterface $orderServiceRefundManagerInterface $refundManagerLoggerInterface $logger)
  33.     {
  34.         $this->orderService $orderService;
  35.         $this->refundManager $refundManager;
  36.         $this->logger $logger;
  37.     }
  38.     /**
  39.      * @return string
  40.      */
  41.     public static function getName(): string
  42.     {
  43.         return 'action.mollie.order.refund';
  44.     }
  45.     /**
  46.      * @return string[]
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             self::getName() => 'handle',
  52.         ];
  53.     }
  54.     /**
  55.      * @return string[]
  56.      */
  57.     public function requirements(): array
  58.     {
  59.         return [OrderAware::class];
  60.     }
  61.     /**
  62.      * @param StorableFlow $flow
  63.      * @throws \Exception
  64.      * @return void
  65.      */
  66.     public function handleFlow(StorableFlow $flow): void
  67.     {
  68.         $orderId $flow->getStore('orderId');
  69.         $this->refundOrder($orderId$flow->getContext());
  70.     }
  71.     /**
  72.      * @param FlowEvent $event
  73.      * @throws \Exception
  74.      */
  75.     public function handle(FlowEvent $event): void
  76.     {
  77.         $config $event->getConfig();
  78.         if (empty($config)) {
  79.             return;
  80.         }
  81.         $baseEvent $event->getEvent();
  82.         if (!$baseEvent instanceof OrderAware) {
  83.             return;
  84.         }
  85.         $orderId $baseEvent->getOrderId();
  86.         $this->refundOrder($orderId$baseEvent->getContext());
  87.     }
  88.     /**
  89.      * @param string $orderId
  90.      * @param Context $context
  91.      * @throws \Exception
  92.      * @return void
  93.      */
  94.     private function refundOrder(string $orderIdContext $context): void
  95.     {
  96.         $orderNumber '';
  97.         try {
  98.             $order $this->orderService->getOrder($orderId$context);
  99.             $orderNumber $order->getOrderNumber();
  100.             $this->logger->info('Starting Refund through Flow Builder Action for order: ' $orderNumber);
  101.             $request = new RefundRequest(
  102.                 (string)$order->getOrderNumber(),
  103.                 'Refund through Shopware Flow Builder',
  104.                 '',
  105.                 null
  106.             );
  107.             $this->refundManager->refund($order$request$context);
  108.         } catch (\Exception $ex) {
  109.             $this->logger->error(
  110.                 'Error when refunding order with Flow Builder Action',
  111.                 [
  112.                     'error' => $ex->getMessage(),
  113.                     'order' => $orderNumber,
  114.                 ]
  115.             );
  116.             throw $ex;
  117.         }
  118.     }
  119. }