Backend Development 5 min read

Preventing Order Loss in Payment Systems: Best Practices and Strategies

This article explains the typical order flow in payment systems, identifies external and internal order loss scenarios, and provides concrete measures such as adding a "payment in progress" state, setting timeout queries, ensuring idempotent notifications, and using Redis hashes to avoid duplicate submissions.

Architecture Digest
Architecture Digest
Architecture Digest
Preventing Order Loss in Payment Systems: Best Practices and Strategies

The simplified order process starts with order submission followed by payment, which usually goes through a payment gateway (payment center) that interacts with third‑party channels like WeChat, Alipay, or UnionPay. After a successful payment, an asynchronous notification updates the payment order status, which then informs the business applications to update their own order statuses.

Common problems include order loss, caused either by external factors (e.g., missing callbacks, program errors) or internal issues, leading to situations where the user has paid but the server has not updated the order status, potentially resulting in complaints or duplicate payments.

To prevent order loss, the following practices are recommended:

1. Add an intermediate "payment in progress" state to the payment order. When the same order attempts payment, first check for an existing "payment in progress" record and lock the pre‑pay operation. After payment completes, update the record to "payment successful".

2. Define a timeout period (e.g., 30 seconds) in the payment center. If no success callback is received within this window, actively query the payment result at intervals (e.g., every 10, 20, 30 seconds). If the result remains unknown after the maximum attempts, handle it as an exception.

3. Once the payment center receives the result, synchronize it to the business system via MQ or direct calls, adding retry mechanisms such as SpringBoot Retry for reliability.

4. Ensure idempotency for all notification interfaces in both the payment center and business applications, processing each message only once and ignoring duplicates.

5. Business applications should also perform timeout‑driven active queries for payment results.

For timeout‑driven queries, store pending payment orders in a dedicated table and use scheduled tasks to scan and query them.

To prevent duplicate order submissions, compute a hash of the order information and check Redis for an existing key. If the key exists, reject the duplicate; otherwise, create a new key with an expiration time and proceed with order creation, effectively limiting repeated identical operations within a short period.

Additionally, the article includes a reference to WeChat Pay best practices and promotional information about a technical learning group.

RedisIdempotencypaymentPayment Gatewaytimeout handlingorder loss
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.