Jafar Sadhik works at Atoa, the UK Pay by Bank and card payments platform. Writes about taking payments, open banking and payment APIs for UK businesses and developers.
Responses1
Markdown supported · ⌘↵ to post
Making "fire the same COMPLETED event twice" a named test step is something I rarely see in webhook guides, and the note about a unique constraint behind insertIfAbsent saves people a race condition. Two things in handlePaymentEvent can still lose or misapply an event. First, the key is recorded before the work runs. If markPaid throws, the provider retries, the key already exists, and the order is never marked paid. Writing the key in the same database transaction as the order update, or only after it succeeds, closes that. Second, the key stops duplicates but not ordering. A delayed FAILED from an earlier attempt can arrive after COMPLETED and release stock on an order that is already paid. Checking the order's current state before acting, for example only releasing stock while it is still pending, covers it, and it makes a good seventh CLI test: trigger COMPLETED, then FAILED, for the same orderId.
Making "fire the same COMPLETED event twice" a named test step is something I rarely see in webhook guides, and the note about a unique constraint behind insertIfAbsent saves people a race condition. Two things in handlePaymentEvent can still lose or misapply an event. First, the key is recorded before the work runs. If markPaid throws, the provider retries, the key already exists, and the order is never marked paid. Writing the key in the same database transaction as the order update, or only after it succeeds, closes that. Second, the key stops duplicates but not ordering. A delayed FAILED from an earlier attempt can arrive after COMPLETED and release stock on an order that is already paid. Checking the order's current state before acting, for example only releasing stock while it is still pending, covers it, and it makes a good seventh CLI test: trigger COMPLETED, then FAILED, for the same orderId.