当前位置:网站首页>The server prevents repeated payment of orders
The server prevents repeated payment of orders
2022-06-11 14:58:00 【Ant】
The server prevents repeated payment of orders
The figure above shows a simplified order placing process , The first is to submit the order , Then there's the payment . If you pay , It's usually through the payment gateway ( Payment center ), Then the payment center and the third party payment channel ( WeChat 、 Alipay 、 UnionPay ) Interaction , After the payment is successful , Asynchronous notification to payment center , The payment center updates the status of its own payment order , Then inform the business application , Each business updates its order status .
In this process, we often encounter the problem of dropping orders , Whether it's timeout or not receiving callback notification , Or the program itself reports an error , In short, for various reasons , Didn't receive the notice as scheduled and handle the follow-up logic correctly, etc , Will cause the user to pay successfully , But the order status on the server side has not been updated , At this time, there may be complaints , Or users pay repeatedly .
from ③⑤ The resulting drop is called external drop , from ④⑥ The loss of orders caused by this is called internal loss of orders
How to prevent missing orders
Add intermediate status
Payment orders add an intermediate state “ In the payment ”, When the same order goes to pay , Check to see if the status is “ In the payment ” Payment flow , Of course, pay (prepay) Put a lock on it . After the payment is completed, when the payment flow status is updated, it will be changed to “ Successful payment ” state .
Play the role of locking
Define timeout
The payment center has to define a time-out ( such as :30 second ), If the payment is not received within this time frame, the callback will be successful , Then the interface should be called to actively query the payment result , such as 10s、20s、30s Check it out , If no result is found within the maximum number of queries , Exception handling should be done .
Synchronize payment results
After the payment center receives the payment result , Synchronize the results to the business system , Can send MQ, It can also be called directly , If you call it directly, try again ( such as :SpringBoot Retry).
Consider interface idempotency
Whether it's payment centers , Or business applications , When receiving payment result notification, we should consider the idempotency of the interface , Messages are processed only once , The rest is ignored
Active inquiry of payment results
Business applications should also take the initiative to query payment results over time , Overtime active query can put these payment orders into a table when initiating payment , Scan with timed tasks .
In order to prevent duplicate order submission , You can do this :
- When you create an order , Use order information to calculate a hash value
- Judge redis If there key, If yes, duplicate submission is not allowed
- If not, a new key, Put it in redis Set an expiration time in
- Then create the order
In fact, the same operation cannot be repeated for a period of time
Reference material
Sharing plans
Blog content will be synchronized to Tencent cloud + Community , Invite everyone to join us :https://cloud.tencent.com/
license agreement
In this paper A signature - Noncommercial use - Share in the same way 4.0 The international license agreement , Reprint please indicate the source .
边栏推荐
- Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?
- After many years of digital transformation projects, the main architects are desperate: outsourcing should not have been used at the beginning!
- 你违规了吗?
- What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger
- 在微服务架构中管理技术债务
- China's technology goes to sea, tidb database's overseas exploration road | interview with excellent technical team
- Riskscanner of multi Cloud Security compliance scanning platform
- NVIDIA R & D director: how does AI improve chip design?
- C # - how to add and read appsetting in the console application JSON file
- 基于STM32F1的开源小项目
猜你喜欢

MySQL create table error 1067 - invalid default value for 'update_ time‘

Summary of some classic embedded C interview questions

In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology

【SystemVerilog 之 过程块和方法】~ 域、always过程块、initial过程块、函数 function、任务 task、生命周期

你违规了吗?

B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡

【SystemVerilog 之 接口】~ Interface

Hot seek tiger, a list of eco economic models

深度剖析「圈组」关系系统设计 | 「圈组」技术系列文章

With a loss of 13.6 billion yuan in three years, can listing revive Weima?
随机推荐
Backtracking / activity scheduling maximum compatible activities
Anaconda delete virtual environment
Repository Manager之Nexus
Raspberry pie obtains the function of network installation system without the help of other devices
NVIDIA R & D director: how does AI improve chip design?
你违规了吗?
B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
How to manually package your own projects
Cartoon: interesting "cake cutting" problem
Lippon instrument software intern interview
China's technology goes to sea, tidb database's overseas exploration road | interview with excellent technical team
02 Tekton Pipeline
Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?
Live800: several ways for intelligent customer service to improve customer experience
19. insertion, deletion and pruning of binary search tree
Illustration of tiger international quarterly report: revenue of USD 52.63 million continued to be internationalized
Nomad application layout scheme 04 of hashicopy (scaling and updating a job)
Social software soul withdraws its IPO application: Tencent is a major shareholder
leetcode每日一题——搜索插入位置
【SystemVerilog 之 验证】~ 测试平台、硬件设计描述、激励发生器、监测器、比较器