Problem/Motivation
onReturn() parses the request body as a PayPlug notification, but the return is a browser redirect (GET) with no body. In live mode it therefore always throws PaymentGatewayException: Request body is not valid JSON.
This is usually hidden: when the notification arrives first, the order is already placed and onReturn() is never called. When the customer returns before the notification:
- the customer sees "Payment failed" and is sent back, although they have paid;
- they may pay again, which can lead to a double charge;
- a false error is logged.
This has happened on a production site.
Test mode skips this code, so the tests don't catch it.
Steps to reproduce
- Set the gateway to live mode.
- Make a payment, and return to the site before PayPlug's notification has been processed. A local site whose notify URL PayPlug can't reach always returns first.
- "Payment failed at the payment server. Please review your information and try again." is displayed, and PaymentGatewayException: Request body is not valid JSON. is logged.
Proposed resolution
Create the Commerce payment before redirecting to PayPlug, then only update it:
- In OffsitePayPlugForm, save the payment in state new with the PayPlug payment ID as its remote_id.
- In onNotify(), load the payment by remote ID and update it.
- Add PayPlugServiceInterface::retrievePayPlugPayment().
- In onReturn(), fetch the order's latest PayPlug payment from the API and update the Commerce payment from it:
- paid or authorized: continue checkout;
- failed: throw a
DeclineException; - pending: throw a
PaymentGatewayExceptionsaying the payment is pending.
- Remove the test-mode early return, and add a test where the customer returns before the notification.
Remaining tasks
- Void earlier payments still in state new (abandoned attempts) when a new attempt starts?
- Patch and tests, after #3641231 lands.
API changes
New method PayPlugServiceInterface::retrievePayPlugPayment(), which breaks custom implementations of the interface.
Issue fork commerce_payplug-3641539
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments