The correct development method of the asynchronous withholding interface of the three-party payment interface (summary of online hole filling)

The new pit a few days ago is still hot, so I can share it with you quickly. I believe that if you do n’t read my article seriously , if you do this function, there is a 99% chance of digging, haha!

Disclaimer: I have used some simple processes, simple tables, etc. to make an assumption here. The actual business scenario will be much more complicated than the example!

List a few nouns first:

The table t_order_pay has a payment state and pay_state has several values 

I:init initial state

U: unknown unknown (processing) status

S: success success status

F: fail failure status

When the user purchases the product, the payment result shows that it is unknown, but the status of the purchased product is already being shipped. Our result is inconsistent with the actual payment result!

The reasons for this problem are:

Let me go to the picture first!

0. Data storage status is I

1. Request for deduction (it takes a long time or caused by network problems)

2. While the first step is still waiting, the callback request comes in, and the status I -> S is notified to the merchant to ship the goods

3. The first step timed out after waiting for a long time. The processing process changed the status to U (note that the original status in the library is not checked here, and it is directly modified to U)

It ended up being the result of the inconsistent state we were talking about! !

Top point: the correct way to deal with

1. Don't imagine, the callback must happen after the request! !

2. Every time you must bring the condition of the original state value, the third part of the process should be update t_order_pay set pay_state = U where pay_order=XXX and pay_state = I and if necessary (see the business) through the number of rows affected by the update Follow-up processing!

 

Here is more to remind everyone that when designing a program, we must consider exceptions and how to deal with the program after an error occurs.

I have seen too many people. Dealing with problems, only success, failure! There are no exceptions, timeouts, and other processing logic. Once online, there are various problems! !

 

 

 

Guess you like

Origin blog.csdn.net/kevin_mails/article/details/91956633