1717use GuzzleHttp \Exception \GuzzleException ;
1818use Payum \Core \Payum ;
1919use SM \Factory \FactoryInterface ;
20+ use Sylius \Component \Core \Model \OrderInterface ;
2021use Sylius \Component \Core \Model \PaymentInterface ;
22+ use Sylius \Component \Core \Model \PaymentMethodInterface ;
23+ use Sylius \Component \Core \Payment \Remover \OrderPaymentsRemoverInterface ;
2124use Sylius \Component \Core \Repository \OrderRepositoryInterface ;
25+ use Sylius \Component \Order \Processor \OrderProcessorInterface ;
2226use Sylius \PayPalPlugin \Provider \OrderProviderInterface ;
2327use Sylius \PayPalPlugin \Resolver \CapturePaymentResolverInterface ;
2428use Symfony \Component \HttpFoundation \JsonResponse ;
@@ -35,6 +39,8 @@ public function __construct(
3539 private readonly ObjectManager $ paymentManager ,
3640 private readonly OrderProviderInterface $ orderProvider ,
3741 private readonly CapturePaymentResolverInterface $ capturePaymentResolver ,
42+ private readonly ?OrderPaymentsRemoverInterface $ orderPaymentsRemover = null ,
43+ private readonly ?OrderProcessorInterface $ orderProcessor = null ,
3844 ) {
3945 if (null !== $ this ->payum ) {
4046 trigger_deprecation (
@@ -66,19 +72,33 @@ public function __construct(
6672 ),
6773 );
6874 }
75+ if (null === $ this ->orderPaymentsRemover ) {
76+ trigger_deprecation (
77+ 'sylius/paypal-plugin ' ,
78+ '1.6 ' ,
79+ 'Not passing an $orderPaymentsRemover to %s constructor is deprecated and will be prohibited in 3.0 ' ,
80+ self ::class,
81+ );
82+ }
83+ if (null === $ this ->orderProcessor ) {
84+ trigger_deprecation (
85+ 'sylius/paypal-plugin ' ,
86+ '1.6 ' ,
87+ 'Not passing an $orderProcessor to %s constructor is deprecated and will be prohibited in 3.0 ' ,
88+ self ::class,
89+ );
90+ }
6991 }
7092
7193 public function __invoke (Request $ request ): Response
7294 {
7395 $ id = $ request ->attributes ->getInt ('id ' );
7496 $ order = $ this ->orderProvider ->provideOrderById ($ id );
7597
76- /** @var PaymentInterface $payment */
77- $ payment = $ order ->getLastPayment (PaymentInterface::STATE_CART );
78-
7998 try {
99+ $ payment = $ this ->getPayment ($ order );
80100 $ this ->capturePaymentResolver ->resolve ($ payment );
81- } catch (GuzzleException $ exception ) {
101+ } catch (\ DomainException | GuzzleException ) {
82102 /** @var FlashBagInterface $flashBag */
83103 $ flashBag = $ request ->getSession ()->getBag ('flashes ' );
84104 $ flashBag ->add ('error ' , 'sylius.pay_pal.something_went_wrong ' );
@@ -94,4 +114,26 @@ public function __invoke(Request $request): Response
94114 'status ' => $ payment ->getState (),
95115 ]);
96116 }
117+
118+ private function getPayment (OrderInterface $ order ): PaymentInterface
119+ {
120+ /** @var PaymentInterface $payment */
121+ $ payment = $ order ->getLastPayment (PaymentInterface::STATE_CART );
122+ /** @var PaymentMethodInterface|null $paymentMethod */
123+ $ paymentMethod = $ payment ->getMethod ();
124+ $ factoryName = $ paymentMethod ?->getGatewayConfig()?->getFactoryName();
125+
126+ if ($ factoryName === 'sylius.pay_pal ' ) {
127+ return $ payment ;
128+ }
129+
130+ if ($ this ->orderPaymentsRemover === null || $ this ->orderProcessor === null ) {
131+ throw new \DomainException ('OrderPaymentsRemover and OrderProcessor must be provided to create a new payment. ' );
132+ }
133+
134+ $ this ->orderPaymentsRemover ->removePayments ($ order );
135+ $ this ->orderProcessor ->process ($ order );
136+
137+ return $ order ->getLastPayment (PaymentInterface::STATE_CART );
138+ }
97139}
0 commit comments