Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling successful payment processing but database update failure

I am trying to implement a stripe checkout process in one of my express.js routes. To do this, I have:

  • Official Node.js Stripe module
  • Official client-side Stripe module
  • A json logger I use to log things like javascript errors, incoming requests and responses from external services like stripe, mongodb, etc…
  • An Order model defined using mongoose - a MongoDB ODM

My steps are as follows:

Client:

  1. Submit order details which include a stripe payment token

Server:

  1. Create an unpaid order and save to database (order.status is created)
  2. Use stripe client to charge user's credit/debit card
  3. Update order and save to database (order.status is accepted or failed depending on response from Stripe)

Question: If payment is successful after step 2 but an error occurs updating the order in step 3 (due to database server error, outage or similar), what are some appropriate ways to handle this failure scenario and potentially recover from it?

like image 977
George Haidar Avatar asked Aug 20 '14 14:08

George Haidar


1 Answers

With payment systems, you always need a consolidation process (hourly, daily, monthly) based on sane accounting principles that will check that every money flow is matched.

In your case, I suggest that every external async call logs the sent parameters and the received response. If you do not have a response within a certain time, you know that something has gone wrong on the external system (Stripe, in your case) or on the way back from the external system (you mention a database failure on your side)

Basically, for each async "transaction" that you spawn, you know when you start it and have to decide of a reasonable amount of time before it ends. Thus you have an expected_end_ts in the database.

If you have not received an answer after expected_end_ts, you know that something is wrong. Then you could ask for the status to Stripe or another PSP. Hopefully the API will give you a sane answer as to whether the payment went through or not.

Also note that you should add a step between 1. and 2 : re-read the database. You want to make sure that every payment request you make is really in the database, stored exactly as you are going to send it.

like image 154
Jerome WAGNER Avatar answered Oct 13 '22 01:10

Jerome WAGNER