The specific scenario in my case involves PayPal but it could easily be any other external system.
According to a lot of sources on the Internet, including the official documentation, a basic PayPal integration (say, for an e-store) would look like this:
- Call the PayPal API to execute the payment for an order.
- Save the order details to the database.
What if the payment is successful but there is some problem with the database? This would mean that I charged the user but failed to record that.
If I create the order before I charge the user, it is possible that the payment fails and now I have an unpaid order.
To better manage this issue I came up with this:
- Create an order with a pending payment.
- Execute the payment.
- Update the order status.
- If update fails, try again (DB retries)
- If the retries fail, send an alert to the website admins with the information of what failed and inform the user that there was this problem.
- It is also possible that the email fails to be sent, in which case I try to send an error message back to the user, saying that there was a huge error and we'd like them to get in touch asap (because otherwise this incident would not be recorded at all).
Unfortunately it is also possible that the user never gets this message.
It seems like a basic flow, but I have no idea how to handle it properly. How should I do it?