custom/plugins/SwagB2bPlatform/components/Order/BridgePlatform/OrderPaymentChangedSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Order\BridgePlatform;
  3. use Shopware\B2B\Common\IdValue;
  4. use Shopware\B2B\Order\Framework\OrderContextRepository;
  5. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  6. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class OrderPaymentChangedSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var OrderContextRepository
  12.      */
  13.     private $orderContextRepository;
  14.     /**
  15.      * @var AuthenticationService
  16.      */
  17.     private $authenticationService;
  18.     public function __construct(
  19.         AuthenticationService $authenticationService,
  20.         OrderContextRepository $orderContextRepository
  21.     ) {
  22.         $this->authenticationService $authenticationService;
  23.         $this->orderContextRepository $orderContextRepository;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CheckoutFinishPageLoadedEvent::class => 'onFinishPageLoaded',
  29.         ];
  30.     }
  31.     public function onFinishPageLoaded(CheckoutFinishPageLoadedEvent $event): void
  32.     {
  33.         if (!$this->authenticationService->isB2b()) {
  34.             return;
  35.         }
  36.         $page $event->getPage();
  37.         if (!$page->isChangedPayment()) {
  38.             return;
  39.         }
  40.         $order $page->getOrder();
  41.         $newPaymentId IdValue::create($order->getTransactions()->last()->getPaymentMethodId());
  42.         $this->orderContextRepository->updatePaymentMethodByOrderNumber($order->getOrderNumber(), $newPaymentId);
  43.     }
  44. }