Consider a payment flow:
- Create a payment intent
- Collect the payment from user’s card
- Capture the payment
There are two ways to handle it:
- Make the entire flow one API request that awaits for the card presentation to finish. The request can take quite long.
- Break the payment intent creation and payment capture into separate api requests, and each of them takes the time of a normal API request.
For type 1, there is an issue for front end - the http request cannot keep open forever, and the caller will get timeout error if the request does not get a response in ~1 minute.
To resolve this issue, the mechanism is:
- The frontend sends the API request
- When the backend receives the API request, write an empty database record and respond that the request has been received to frontend
- The backend keeps going with the execution until the transaction has finished, and update the database record
- In the meantime, the frontend polls for database update with an interval, and gives feedback to user when it detects the db status has been updated