Sylius / PayPalPlugin

Official integration with PayPal Commerce Platform
37 stars 56 forks source link

Notice: Undefined index: status #189

Closed jeandanyel closed 3 years ago

jeandanyel commented 3 years ago

Hi 🙂

Sometimes I get this error and it is not possible to pay and complete the order. I'll try to find out why this is happening to give you more details...

Thank you!

[2021-02-27T09:04:31.884693-05:00] paypal.ERROR: POST request to "https://api.paypal.com/v2/checkout/orders" failed with debug ID 1e418bc543e0d [] [] [2021-02-27T09:04:32.000958-05:00] paypal.ERROR: Error: Expected an order id to be passed [] [] [2021-02-27T09:04:36.553244-05:00] paypal.ERROR: Error: Unexpected token < in JSON at position 0 [] []

image

jeandanyel commented 3 years ago

What I got today, I still don't know why 😔

[2021-03-05T13:44:27.340188-05:00] paypal.ERROR: POST request to "https://api.paypal.com/v2/checkout/orders" failed with debug ID a4ddc9911946b [] [] [2021-03-05T13:44:28.250741-05:00] paypal.ERROR: Error: Expected an order id to be passed [] [] [2021-03-05T13:44:36.278537-05:00] paypal.ERROR: Error: Unexpected token < in JSON at position 0 [] [] [2021-03-05T13:44:47.289368-05:00] paypal.ERROR: Error: Unexpected token < in JSON at position 0 [] []

jeandanyel commented 3 years ago

I am finally able to reproduce the bug!

When we complete the order, we are redirected to a page where we find two PayPal buttons. When we click the pay button, a window open to process the payment.

But if we go back to Sylius (without closing the PayPal window) and then we refresh the page, we got an error message.

Video here: https://youtu.be/TkR1Sj1W0hw

Logs: [2021-03-01T11:52:04.059566-05:00] paypal.ERROR: POST request to "https://api.sandbox.paypal.com/v2/checkout/orders" failed with debug ID 6734d2ed6fc0a [] [] [2021-03-01T11:52:05.079994-05:00] paypal.ERROR: Error: Unexpected token < in JSON at position 0 [] []

Thanks!

Zales0123 commented 3 years ago

Thank you for the report! We will take a look at it and see how it can be fixed 🖖

jeandanyel commented 3 years ago

I found something that might help to fix the issue...

When the problem occurs, we can see these logs in the profiler:

image

If we take a look to the CreatePayPalOrderAction class, at the line 65, the function getLastPayment(PaymentInterface::STATE_NEW) return null because the current payent has PROCESSING as state.

image

I tried to replace the line 65 with these lines and it works... but it's a temporary fix!

$payment = $order->getLastPayment();

if($payment->getState() !== PaymentInterface::STATE_NEW) {
    $payment->setState(PaymentInterface::STATE_NEW);
}
Zales0123 commented 3 years ago

Hello @jeandanyel, it's been a while 🖖 I'd dug deeper into the issue and I believe I've found the problem. When going to PayPal, the payment is already set as "processing". Once you normally close the Paypal window, the payment is cancelled (with a proper onCancel callback). However, when we just refresh the page, the payment is not cancelled so no new one with a "new" state is created. And it results in such an error.

What we should probably do, we should listen to page refreshing (probably with a listener to beforeunload event?) and cancel the payment as well.

I hope we will be able to fix it soon, thanks again for the report 🎉