Search code examples
paypalpaypal-rest-sdk

How to handle a PayPal Checkout payment when there is a confirmation API error on our server


We are integrating the PayPal client side Checkout Integration for taking payments on our website. This can be found here:

https://developer.paypal.com/docs/checkout/integrate/#6-verify-the-transaction

Once the payment has been made and approved by PayPal, we need to call our server to verify the transaction and store it within our database. This code can be found below, note the part "Call your server to save the transaction".

<script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name);
        // Call your server to save the transaction
        return fetch('/paypal-transaction-complete', {
          method: 'post',
          body: JSON.stringify({
            orderID: data.orderID
          })
        });
      });
    }
  }).render('#paypal-button-container');
</script>

Now, in the above instance, what happens if the call to "/paypal-transaction-complete" fails? session timeout or lost internet connection? For example, in the Stripe integration, the money is "approved" in on the client side and then only confirmed/charged in our API to "/stripe-transaction-complete". If there is an error, we don't actually charge the money.

Within PayPal, the money is charged before the API call, so the is the small possibility we charge the user but they don't receive the paid order in the database. How would we best handle this? one option would be to call the PayPal API and match all the orders with payments and then either auto-refund or auto-complete the order. But I'm not sure if this is recommended.


Solution

  • For both PayPal and similar issues with Stripe Checkout, this can be addressed using WebHooks.

    https://developer.paypal.com/docs/integration/direct/webhooks/rest-webhooks/#