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

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\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Event\OrderAware;
  10. class RefundOrderAction extends FlowAction
  11. {
  12.     /**
  13.      * @var OrderServiceInterface
  14.      */
  15.     private $orderService;
  16.     /**
  17.      * @var RefundManagerInterface
  18.      */
  19.     private $refundManager;
  20.     /**
  21.      * @var LoggerInterface
  22.      */
  23.     private $logger;
  24.     /**
  25.      * @param OrderServiceInterface $orderService
  26.      * @param RefundManagerInterface $refundManager
  27.      * @param LoggerInterface $logger
  28.      */
  29.     public function __construct(OrderServiceInterface $orderServiceRefundManagerInterface $refundManagerLoggerInterface $logger)
  30.     {
  31.         $this->orderService $orderService;
  32.         $this->refundManager $refundManager;
  33.         $this->logger $logger;
  34.     }
  35.     /**
  36.      * @return string
  37.      */
  38.     public static function getName(): string
  39.     {
  40.         return 'action.mollie.order.refund';
  41.     }
  42.     /**
  43.      * @return string[]
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             self::getName() => 'handle',
  49.         ];
  50.     }
  51.     /**
  52.      * @return string[]
  53.      */
  54.     public function requirements(): array
  55.     {
  56.         return [OrderAware::class];
  57.     }
  58.     /**
  59.      * @param FlowEvent $event
  60.      * @throws \Exception
  61.      */
  62.     public function handle(FlowEvent $event): void
  63.     {
  64.         $config $event->getConfig();
  65.         if (empty($config)) {
  66.             return;
  67.         }
  68.         $baseEvent $event->getEvent();
  69.         if (!$baseEvent instanceof OrderAware) {
  70.             return;
  71.         }
  72.         $this->refundOrder($baseEvent$config);
  73.     }
  74.     /**
  75.      * @param OrderAware $baseEvent
  76.      * @param array<mixed> $config
  77.      * @throws \Exception
  78.      */
  79.     private function refundOrder(OrderAware $baseEvent, array $config): void
  80.     {
  81.         $orderNumber '';
  82.         try {
  83.             $orderId $baseEvent->getOrderId();
  84.             $order $this->orderService->getOrder($orderId$baseEvent->getContext());
  85.             $orderNumber $order->getOrderNumber();
  86.             $this->logger->info('Starting Refund through Flow Builder Action for order: ' $orderNumber);
  87.             $request = new RefundRequest(
  88.                 (string)$order->getOrderNumber(),
  89.                 'Refund through Shopware Flow Builder',
  90.                 null
  91.             );
  92.             $this->refundManager->refund($order$request$baseEvent->getContext());
  93.         } catch (\Exception $ex) {
  94.             $this->logger->error(
  95.                 'Error when refunding order with Flow Builder Action',
  96.                 [
  97.                     'error' => $ex->getMessage(),
  98.                     'order' => $orderNumber,
  99.                 ]
  100.             );
  101.             throw $ex;
  102.         }
  103.     }
  104. }