跳到正文
DEV Community· Hassan Essam·· 4 小時前AI 評分48

付款 Webhook 不應對同一筆購買重複授權

A Payment Webhook Should Not Grant the Same Purchase Twice

AI 導讀

Snuhy 創辦人 Hassan Essam 指出,付款 Webhook 重試時須按業務身份去重,避免同一筆已確認付款重複授予權限、記錄收款或建立廣告購買事件。文章建議以資料庫唯一約束確保狀態轉換正確,並用 outbox 處理外部通知;發送或排程行銷衡量事件前,亦須檢查用戶是否同意。

正文

When a learning platform sells a digital resource, the interesting part of payment integration is often the second notification.

The first confirmed payment can grant access. A repeated notification must not grant it again, record another collection, or create a second purchase event for advertising analytics.

I am Hassan Essam, founder of Snuhy, an Egyptian learning and digital services platform. This is a small design note about a rule we use when defining payment behavior: count a sale after confirmed payment, and preserve one identity for that sale across retries.

The example below is explanatory pseudocode, not an extract from our production code or a complete payment integration.

Three identities that answer different questions

An integration can have at least three identifiers:

  1. Order ID: which purchase the customer is trying to make.
  2. Provider event ID: which notification arrived.
  3. Payment ID: which confirmed payment the provider is reporting.

Deduplicating only the event ID is insufficient if two distinct notifications describe the same payment. Deduplicating only the order ID can also be wrong when an order legitimately supports multiple collections.

Choose the business key for the action. An access grant might be unique per order and product. A collection should use the confirmed payment identity. An advertising purchase event needs the identity of the sale it represents.

Keep the state transition and its consequences together

receive notification
verify provider authenticity using the documented mechanism
validate merchant, currency, amount, and purchase reference
establish the authoritative payment status

if payment is not confirmed:
    record the relevant status without recording a sale
    return the appropriate acknowledgement

begin database transaction
    lock the relevant payment/order records
    insert confirmed-payment record with a unique business key
    apply the allowed purchase state transition
    create any access grant with its own uniqueness constraint
    insert an outbox item for each eligible external consequence
commit

deliver outbox items outside the transaction

A unique constraint is part of correctness here. Two workers can both pass an application-level “does this exist?” check before either inserts a record. The database needs to reject the conflicting duplicate.

The exact keys and transitions depend on the payment provider and business model. An installment collection, a full purchase, and a refund should not be forced into one event merely because all three involve money.

The outbox does not make external effects magically exactly-once

An outbox helps avoid a committed payment with a missing notification job. It does not eliminate the uncertainty of an external request.

A provider might accept a request just before the worker loses its connection. Retrying without reconciliation can duplicate the external effect. Use the destination's supported idempotency or event-deduplication mechanism, retain the result, and distinguish known failure from uncertain acceptance.

For purchase measurement, use a stable sale identity. If browser and server both report the same sale, configure the destination's deduplication mechanism consistently. Also check measurement consent before enqueueing or sending; confirmation of payment is not itself marketing consent.

Test the repeated event, not just the happy path

Useful scenarios include:

  • The same valid notification arrives twice.
  • Two different event IDs refer to the same payment.
  • Two workers process the payment concurrently.
  • A pending notification arrives after a confirmed one.
  • The merchant, currency, or amount does not match.
  • The payment commits but the external service is unavailable.
  • The external service accepts a request and the response is lost.
  • A visitor declines optional measurement.
  • A later partial collection is legitimate and must remain distinct.

These checks should use isolated fixtures, fake external services, and test-provider facilities. There is no need to create fake revenue in a live financial ledger to prove that retries are handled.

The customer-facing consequence is straightforward: a repeated technical notification should not look like a second purchase. Achieving that requires each downstream action to have a defined business identity and an explicit retry policy.

Disclosure: I am the founder of Snuhy. This article was drafted with AI assistance from recorded project requirements and is presented as a conceptual design example. It makes no claim of a measured conversion uplift or a provider-certified implementation.

來源:DEV Community · dev.to