Payment webhook idempotency exercise v1.0
Fone | 27 October 2026

Run with Python 3.9+ from this directory:
python3 -m unittest -v

No dependencies, network calls, account setup or credentials are needed.
Tests create a temporary SQLite database for each case and clean it up.
This is an original teaching example, not the StableIn product source.
You may use and adapt these example files, with attribution to Fone.
Provided as an educational example without warranty; review before any deployment.

receive_verified assumes authentication, merchant/order binding and payment
validation already happened. Never expose it directly as a webhook endpoint.
The eligible flag represents locally known eligibility, not live payment proof.
Concurrent tests use SQLite's single-writer serialization. The entitlement is a
local row. Exceptions model transaction interruptions, not OS/power failure.
No production HTTP receiver, MySQL row locks, worker leases, external effects,
refund reconciliation or payment signing is implemented here.

Nine included tests cover duplicate receipt, distinct events for one order,
changed payload conflicts, two transaction interruption points, concurrent
receivers, concurrent workers, ineligible orders and unknown orders.

Project integration checklist
[ ] Verify the original request bytes and signature freshness.
[ ] Bind provider, merchant, order, network, asset and integer amount.
[ ] Define event identity and fulfillment-unit identity separately.
[ ] Commit received events and pending work together before acknowledging.
[ ] Enforce fulfillment uniqueness in the database.
[ ] Recheck current eligibility and reject stale state.
[ ] Prevent an expired worker lease from committing newer work.
[ ] Commit the local entitlement and completion record together.
[ ] Define post-delivery refunds and external API recovery separately.
[ ] Repeat tests against your actual database, provider and failure model.

Full explanation:
https://fone.cc/insights/idempotent-payment-webhooks-digital-goods/
Product context:
https://fone.cc/products/stablein-payments/
