@@ -77,44 +77,55 @@ public function execute($request): void
7777 $ model = $ request ->getModel ();
7878 /** @var PaymentInterface $payment */
7979 $ payment = $ request ->getFirstModel ();
80- /** @var OrderInterface $order */
81-
8280
81+ /** @var OrderInterface $order */
8382 $ order = $ payment ->getOrder ();
8483
8584 /** @var TokenInterface $token */
8685 $ token = $ request ->getToken ();
8786 $ payUdata = $ this ->prepareOrder ($ token , $ order , $ payment );
8887
89- $ result = $ this ->openPayUBridge ->create ($ payUdata );
88+ $ paymentDetails = $ payment ->getDetails ();
89+ $ forceNewTransaction = false ;
9090
91- if (null !== $ model ['orderId ' ]) {
91+ if (isset ( $ paymentDetails ['orderId ' ]) ) {
9292 /** @var mixed $response */
93- $ response = $ this ->openPayUBridge ->retrieve ((string ) $ model ['orderId ' ])->getResponse ();
93+ $ response = $ this ->openPayUBridge ->retrieve ((string ) $ paymentDetails ['orderId ' ])->getResponse ();
9494 Assert::keyExists ($ response ->orders , 0 );
9595
96- if ($ response && OpenPayUBridgeInterface::PENDING_API_STATUS === $ response ->orders [0 ]->status ) {
97- $ resultResponse = $ result ->getResponse ();
98- if (isset ($ resultResponse ->redirectUri )) {
99- throw new HttpRedirect ($ resultResponse ->redirectUri );
96+ if (OpenPayUBridgeInterface::SUCCESS_API_STATUS === $ response ->status ->statusCode ) {
97+ if ($ response ->orders [0 ]->status === OpenPayUBridgeInterface::PENDING_API_STATUS ) {
98+ // Sometimes PayU doesn't change payment status immediately. We need to wait a bit to check,
99+ // whether it's pending, because client closed a tab (and then force redirecting to new transaction)
100+ // or it's pending, because PayU didn't change payment status at time.
101+
102+ sleep (2 );
103+ $ response = $ this ->openPayUBridge ->retrieve ((string ) $ paymentDetails ['orderId ' ])->getResponse ();
104+ Assert::keyExists ($ response ->orders , 0 );
105+
106+ $ forceNewTransaction = $ response ->orders [0 ]->status === OpenPayUBridgeInterface::PENDING_API_STATUS ;
100107 }
101- }
102108
103- if (OpenPayUBridgeInterface::SUCCESS_API_STATUS === $ response ->status ->statusCode ) {
104- $ model ['statusPayU ' ] = $ response ->orders [0 ]->status ;
105- $ request ->setModel ($ model );
109+ if ($ forceNewTransaction === false ) {
110+ $ model ['statusPayU ' ] = $ response ->orders [0 ]->status ;
111+ $ request ->setModel ($ model );
112+ }
106113 }
107114
108- if (OpenPayUBridgeInterface:: NEW_API_STATUS !== $ response -> orders [ 0 ]-> status ) {
115+ if ($ forceNewTransaction === false ) {
109116 return ;
110117 }
111118 }
112119
120+ $ result = $ this ->openPayUBridge ->create ($ payUdata );
113121 if (null !== $ result ) {
114122 /** @var mixed $response */
115123 $ response = $ result ->getResponse ();
116124 if ($ response && OpenPayUBridgeInterface::SUCCESS_API_STATUS === $ response ->status ->statusCode ) {
117125 $ model ['orderId ' ] = $ response ->orderId ;
126+ $ paymentDetails = $ payment ->getDetails ();
127+ $ paymentDetails ['PayUOrderId ' ] = $ response ->orderId ;
128+ $ payment ->setDetails ($ paymentDetails );
118129
119130 $ request ->setModel ($ model );
120131
0 commit comments