custom/plugins/CkoShopware6/src/Content/Flow/Dispatching/Action/FullRefundAction.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cko\Shopware6\Content\Flow\Dispatching\Action;
  3. use Cko\Shopware6\Facade\PaymentRefundFacade;
  4. use Cko\Shopware6\Service\Builder\RefundBuilder;
  5. use Cko\Shopware6\Service\Order\AbstractOrderService;
  6. use Cko\Shopware6\Service\Order\OrderService;
  7. use Cko\Shopware6\Struct\Request\Refund\RefundItemRequestCollection;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\Event\FlowEvent;
  12. use Shopware\Core\Framework\Event\OrderAware;
  13. class FullRefundAction extends FlowAction
  14. {
  15.     private LoggerInterface $logger;
  16.     private AbstractOrderService $orderService;
  17.     private PaymentRefundFacade $refundFacade;
  18.     private RefundBuilder $refundBuilder;
  19.     public function __construct(
  20.         LoggerInterface $logger,
  21.         AbstractOrderService $orderService,
  22.         PaymentRefundFacade $refundFacade,
  23.         RefundBuilder $refundBuilder
  24.     ) {
  25.         $this->logger $logger;
  26.         $this->orderService $orderService;
  27.         $this->refundFacade $refundFacade;
  28.         $this->refundBuilder $refundBuilder;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [self::getName() => 'handle'];
  33.     }
  34.     public static function getName(): string
  35.     {
  36.         return 'action.checkout_com.full_refund';
  37.     }
  38.     public function requirements(): array
  39.     {
  40.         return [OrderAware::class];
  41.     }
  42.     public function handle(FlowEvent $event): void
  43.     {
  44.         $baseEvent $event->getEvent();
  45.         if (!$baseEvent instanceof OrderAware) {
  46.             return;
  47.         }
  48.         $this->logger->info(sprintf('Action starting to full refund with order ID: %s'$baseEvent->getOrderId()));
  49.         $this->handleFullRefund($baseEvent->getOrderId(), $baseEvent->getContext());
  50.     }
  51.     private function handleFullRefund(string $orderIdContext $context): void
  52.     {
  53.         $order $this->orderService->getOrder($context$orderId, ['lineItems']);
  54.         $orderCustomFields OrderService::getCheckoutOrderCustomFields($order);
  55.         $checkoutPaymentId $orderCustomFields->getCheckoutPaymentId();
  56.         // Skip if the order don't have checkout payment ID
  57.         if (empty($checkoutPaymentId)) {
  58.             $this->logger->error(sprintf('No checkout.com payment ID found for order number: %s'$order->getOrderNumber()));
  59.             return;
  60.         }
  61.         $refundRequest $this->refundBuilder->buildRefundRequestForFullRefund($order);
  62.         $refundItemsRequest $refundRequest->getItems();
  63.         if (!$refundItemsRequest instanceof RefundItemRequestCollection) {
  64.             $this->logger->error('The $refundItemsRequest must be instance of RefundItemRequestCollection');
  65.             return;
  66.         }
  67.         // Skip if the refund request don't have any items
  68.         if ($refundItemsRequest->count() === 0) {
  69.             $this->logger->warning('The refund request do not have any items to refund');
  70.             return;
  71.         }
  72.         $this->refundFacade->refundPayment($refundRequest$context);
  73.     }
  74. }