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

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