55namespace PayPlug \SyliusPayPlugPlugin \Handler ;
66
77use DateTimeImmutable ;
8+ use Doctrine \ORM \EntityManagerInterface ;
89use Payplug \Resource \IVerifiableAPIResource ;
910use Payplug \Resource \Payment ;
1011use Payplug \Resource \PaymentAuthorization ;
1920use Sylius \Component \Resource \Factory \FactoryInterface ;
2021use Sylius \Component \Resource \Repository \RepositoryInterface ;
2122use Symfony \Component \HttpFoundation \Session \Flash \FlashBagInterface ;
23+ use Symfony \Component \Lock \LockFactory ;
2224
2325class PaymentNotificationHandler
2426{
@@ -37,18 +39,26 @@ class PaymentNotificationHandler
3739 /** @var \Sylius\Component\Core\Repository\CustomerRepositoryInterface */
3840 private $ customerRepository ;
3941
42+ private EntityManagerInterface $ entityManager ;
43+
44+ private LockFactory $ lockFactory ;
45+
4046 public function __construct (
4147 LoggerInterface $ logger ,
4248 RepositoryInterface $ payplugCardRepository ,
4349 FactoryInterface $ payplugCardFactory ,
4450 CustomerRepositoryInterface $ customerRepository ,
45- FlashBagInterface $ flashBag
51+ FlashBagInterface $ flashBag ,
52+ EntityManagerInterface $ entityManager ,
53+ LockFactory $ lockFactory
4654 ) {
4755 $ this ->logger = $ logger ;
4856 $ this ->payplugCardRepository = $ payplugCardRepository ;
4957 $ this ->payplugCardFactory = $ payplugCardFactory ;
5058 $ this ->flashBag = $ flashBag ;
5159 $ this ->customerRepository = $ customerRepository ;
60+ $ this ->entityManager = $ entityManager ;
61+ $ this ->lockFactory = $ lockFactory ;
5262 }
5363
5464 public function treat (Generic $ request , IVerifiableAPIResource $ paymentResource , \ArrayObject $ details ): void
@@ -57,18 +67,33 @@ public function treat(Generic $request, IVerifiableAPIResource $paymentResource,
5767 return ;
5868 }
5969
70+ $ lock = $ this ->lockFactory ->createLock ('payment_ ' .$ paymentResource ->id );
71+ $ lock ->acquire (true );
72+
73+ $ this ->entityManager ->refresh ($ request ->getFirstModel ());
74+
75+ if (PayPlugApiClientInterface::STATUS_CREATED === $ request ->getFirstModel ()->getDetails ()) {
76+ $ lock ->release ();
77+
78+ return ;
79+ }
80+
6081 if ($ paymentResource ->is_paid ) {
6182 $ details ['status ' ] = PayPlugApiClientInterface::STATUS_CAPTURED ;
6283 $ details ['created_at ' ] = $ paymentResource ->created_at ;
6384
6485 $ this ->saveCard ($ request ->getFirstModel (), $ paymentResource );
6586
87+ $ lock ->release ();
88+
6689 return ;
6790 }
6891
6992 if ($ this ->isResourceIsAuthorized ($ paymentResource )) {
7093 $ details ['status ' ] = PayPlugApiClientInterface::STATUS_AUTHORIZED ;
7194
95+ $ lock ->release ();
96+
7297 return ;
7398 }
7499
@@ -80,6 +105,8 @@ public function treat(Generic $request, IVerifiableAPIResource $paymentResource,
80105 'message ' => $ paymentResource ->failure ->message ?? '' ,
81106 ];
82107
108+ $ lock ->release ();
109+
83110 return ;
84111 }
85112
@@ -94,6 +121,7 @@ public function treat(Generic $request, IVerifiableAPIResource $paymentResource,
94121 }
95122
96123 $ details ['status ' ] = PayPlugApiClientInterface::FAILED ;
124+ $ lock ->release ();
97125 }
98126
99127 private function saveCard (PaymentInterface $ payment , IVerifiableAPIResource $ paymentResource ): void
0 commit comments