交易、API 和故障设计
我一直在开发一个需要记录信用卡交易的应用程序,该交易依赖于使用外部 API。在我的应用程序中,我有一个带有总计的发票的概念,以及当信用卡成功付款时从该总计中扣除的交易。
这更多的是一个独立于平台的问题,但我正在使用 Django、Python 和 MySQL。
我的问题主要围绕处理外部 API 时事务的使用以及如何设计软件来处理潜在的故障。 Django 和 MySQL 都支持事务,因此本身不是问题,但假设以下情况:
- 通过付款 API 提交信用卡 信用卡
- 已成功处理
- 然后此响应将作为该发票上的付款记录到数据库中
- 有由于某种原因将付款保存到数据库时出错
您现在要做什么?
如果不涉及 API 调用,答案就很明确了,回滚数据库事务并引发错误。但是调用外部 API 会使事情变得复杂,因为这并不是真正回滚外部 API 调用的方法。
我很感兴趣是否有人遇到过这个问题(对于信用卡或类似类型的交易)以及他们如何解决该问题,或者一般来说在这种情况下软件设计的一些方法。
I have been working on an application that needs to log credit card transactions which is dependent on using an external API. Within my application, I have the concept of an invoice with a total, and a transaction that when successful credit card payment is made, deducts from this total.
This is more of a platform independent question, but I am working with Django, Python and MySQL.
My question centers mainly around the use of transactions when dealing with external API's and how to design your software to handle potential failures. Both Django and MySQL support transactions, so that in itself is not an issue, but suppose the following scenario:
- Credit card submitted though the payment API
- Credit card is successfully processed
- This response is then logged to the database as a payment on that invoice
- There is an error saving the payment to the database for one reason or another
What do you do now?
If there was not an API call involved the answer would be clear, rollback the database transaction and raise an error. But having a call to an external API complicates matters, because this is not really a way to rollback on the external API call.
I am interested if anyone has run into this issue (for credit cards, or similar types of transactions) and how they addressed the problem, or in general some approaches for software design in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难用软件来管理它。但是,如果您的支付网关正在调用回调来表示交易成功,那么如果该回调未能完成,它可能会记录一个错误,并且您应该能够将其配置为在这种情况下提醒您(也许是通过电子邮件)。然后由您手动纠正这种情况。
It's hard to manage this in software. However, if your payment gateway is calling a callback to signify that the transaction is successful, it will presumably log an error if that callback fails to complete, and you should be able to configure it to alert you in that case, perhaps by email. Then it's up to you to rectify the situation manually.