1212
1313use BitBag \SyliusImojePlugin \Api \ImojeApi ;
1414use BitBag \SyliusImojePlugin \Resolver \SignatureResolverInterface ;
15+ use Doctrine \ORM \EntityManagerInterface ;
1516use Payum \Core \Action \ActionInterface ;
1617use Payum \Core \ApiAwareInterface ;
1718use Payum \Core \ApiAwareTrait ;
1819use Payum \Core \Bridge \Spl \ArrayObject ;
1920use Payum \Core \Exception \RequestNotSupportedException ;
2021use Payum \Core \Request \Notify ;
22+ use Sylius \Component \Core \Model \PaymentInterface ;
23+ use Sylius \Component \Core \Repository \OrderRepositoryInterface ;
24+ use Sylius \Component \Payment \Factory \PaymentFactoryInterface ;
2125use Symfony \Component \HttpFoundation \Request ;
2226use Symfony \Component \HttpFoundation \RequestStack ;
2327
@@ -30,11 +34,15 @@ final class NotifyAction implements ActionInterface, ApiAwareInterface
3034 public function __construct (
3135 private RequestStack $ requestStack ,
3236 private SignatureResolverInterface $ signatureResolver ,
37+ private PaymentFactoryInterface $ paymentFactory ,
38+ private EntityManagerInterface $ entityManager ,
39+ private OrderRepositoryInterface $ orderRepository ,
3340 ) {
3441 $ this ->request = $ requestStack ->getCurrentRequest ();
3542 $ this ->apiClass = ImojeApi::class;
3643 }
3744
45+ /** @param Notify $request */
3846 public function execute ($ request ): void
3947 {
4048 RequestNotSupportedException::assertSupports ($ this , $ request );
@@ -43,14 +51,38 @@ public function execute($request): void
4351 throw new \Exception ('Request is empty ' );
4452 }
4553
54+ /** @var PaymentInterface $payment */
55+ $ payment = $ request ->getFirstModel ();
56+
4657 /** @var string $content */
4758 $ content = $ this ->request ->getContent ();
4859 $ notificationData = json_decode ($ content , true );
60+ if ($ notificationData ['payment ' ]['status ' ] === 'pending ' ) {
61+ return ;
62+ }
63+
4964 $ transactionData = $ notificationData ['transaction ' ];
5065
5166 $ model = $ request ->getModel ();
5267 $ model ['statusImoje ' ] = $ transactionData ['status ' ];
5368
69+ if ($ notificationData ['payment ' ]['status ' ] === 'error ' ) {
70+ /** @var PaymentInterface $newPayment */
71+ $ newPayment = $ this ->paymentFactory ->createNew ();
72+ $ newPayment ->setState ('new ' );
73+ $ newPayment ->setCurrencyCode ($ payment ->getCurrencyCode ());
74+ $ newPayment ->setAmount ($ payment ->getAmount ());
75+ $ this ->entityManager ->persist ($ newPayment );
76+
77+ $ order = $ payment ->getOrder ();
78+ $ reloadedOrder = $ this ->orderRepository ->findOneBy (['id ' => $ order ->getId ()]);
79+
80+ $ reloadedOrder ->getLastPayment ()->setState ('failed ' );
81+ $ reloadedOrder ->addPayment ($ newPayment );
82+
83+ $ this ->entityManager ->flush ();
84+ }
85+
5486 $ request ->setModel ($ model );
5587 }
5688
0 commit comments