Best Practice
Keep transactions in final status
When you are dealing with a transaction, you will be in one of the following transaction status:
- WAIT_BUYER_PAY: the system is waiting for the buyer to pay.
- TRADE_FINISHED: means the trade succeeds and refunding is allowed.
- TRADE_CLOSED: means the trade is unpaid or timed out.
Your system can avoid many potential problems when your transaction is in the final status, which is TRADE_FINISHED or TRADE_CLOSED. To keep transactions in the final status, you can take the following actions:
- Set the time-out value. For example, you can set 3 minutes as the time-out value.
- Get the payment result with the page redirection caused by the synchronous notification.
- Confirm the payment result with the asynchronous notification.
- If no payment result or asynchronous notification is returned, the transaction status must be THE SYSTEM IS CONFIRMING THE PAYMENT RESULT or THE PAYMENT IS WAITING TO BE PAID.
- Every time the user enters the order system, the query API will be called to confirm the payment result.
- For all the unpaid transactions over 3 minutes, set the transaction status as TRADE_CLOSED.
Idempotency
Idempotence is the property of Alipay sending back only one unique result for the same requests that were sent multiple times. Alipay checks the idempotence to keep an accurate transaction or refund result for each request. To help Alipay checks the idempotence, you need to take the following actions:
- For the same payment requests that were sent multiple times, provide the same out_trade_no for the same payment requests. Requests with the same out_trade_no are considered as the same transaction.
- For the same refund requests that were sent multiple times, provide the same out_trade_no and partner_transaction_id for refund requests that were sent multiple times. Requests with the same out_trade_no and partner_transaction_id are considered as the same transaction.
Alipay checks the idempotence for payment and refund requests in the following ways:
- For the payment requests with a same PID:
After receiving a payment request, Alipay checks the idempotence of out_trade_no (partner_transaction_id). Transactions with a same out_trade_no are considered as same transactions. Alipay then checks other information such as the buyer information and the transaction amount. If the information does not match with that in the Alipay system, errors will be returned. Otherwise, Alipay returns the real status of the transaction.
- For the refund request with a same PID:
After receiving a refund request, Alipay checks the idempotence of out_trade_no (partner_transaction_id) and partner_refund_id. Refunds with a same out_trade_no AND partner_transaction_id are considered as same transactions. Alipay then checks information such as the amount of the refund. If this information does not match with that in the Alipay system, errors will be returned. Otherwise, Alipay returns the real status of the refund.
For more details about idempotency, see API idempotency.