---
title: "Discussion on \"How to Test Payment Webhooks From the CLI Before a Real Customer Finds the Bug\" | Hashnode"
description: "Discussion on \"How to Test Payment Webhooks From the CLI Before a Real Customer Finds the Bug\". You've just changed the code that marks orders as paid, and you need to know it still copes when a payment fails, a card is only authorised or a refund bounces. Making a real sandbox payment for each"
url: "https://hashnode.com/posts/how-to-test-payment-webhooks-from-the-cli-before-a-real-customer-finds-the-bug/6af6fd0acaa99e7879255ec1"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to Test Payment Webhooks From the CLI Before a Real Customer Finds the Bug](https://jafaronpayments.hashnode.dev/how-to-test-payment-webhooks-from-the-cli-before-a-real-customer-finds-the-bug?utm_source=hashnode&utm_medium=feed)

You've just changed the code that marks orders as paid, and you need to know it still copes when a payment fails, a card is only authorised or a refund bounces. Making a real sandbox payment for each

2d ago · 8 min read · jafaronpayments.hashnode.dev  
Read the article

-   [#cli](https://hashnode.com/tag/cli)
-   [#testing](https://hashnode.com/tag/testing)
-   [#webhooks](https://hashnode.com/tag/webhooks)
-   [#devops](https://hashnode.com/tag/devops)

## Written by

[Jafar Sadhik](https://hashnode.com/@jaffy)

@jaffy · London, UK

  
[View profile](https://hashnode.com/@jaffy)

Payments at Atoa

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.

## Responses 1

Markdown supported · ⌘↵ to post

[15h ago](https://hashnode.com/posts/how-to-test-payment-webhooks-from-the-cli-before-a-real-customer-finds-the-bug/6af6fd0acaa99e7879255ec1/comment/6af97b58fa6bf7041d0d14ef)

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.
