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

  1. Set the gateway to live mode.
  2. 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.
  3. "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:

  1. In OffsitePayPlugForm, save the payment in state new with the PayPlug payment ID as its remote_id.
  2. In onNotify(), load the payment by remote ID and update it.
  3. Add PayPlugServiceInterface::retrievePayPlugPayment().
  4. 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 PaymentGatewayException saying the payment is pending.
  5. 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.

Command icon 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

nicolasgraph created an issue.