Testing scenarios
Test mode reproduces the failures you will actually meet, not just the happy path. You should not be finding out what a timeout does in production on a Friday. In test mode the mock rail answers instead of a real gateway: no money moves and no prompt ever reaches a real handset. One honest caveat: the mock rail only loads when the API server boots in test mode — the production server boots live, so run this walkthrough on a local stack (see below) and verify production with a real GH₵1.
How test mode decides
reference you send. Include one of the keywords below anywhere in it and you get that behaviour deterministically, every time. Anything else succeeds.Triggers
| Put this in reference | What happens | Why you should test it |
|---|---|---|
| anything else | Succeeds. | The happy path. |
fail | Resolves to failed. | Your decline handling and the message you show. |
timeout | Resolves to unknown and stays there. | The one most integrations get wrong. |
pending | Stays processing. | Your waiting state, and that you do not time out too early. |
reject | Refused at creation with a 402. | Errors on the create call itself. |
insufficient | insufficient_funds. | The most common real decline in Ghana. |
A full walkthrough
Run the stack locally for this walkthrough — three terminals: the API, the ingress that receives gateway callbacks, and the worker that drains the queue. Then drive it from a fourth. The mock callback below is refused in production; it only exists on a local run.
1. A payment that succeeds
REF="order-$(uuidgen | cut -c1-8)"
curl http://localhost:3000/v1/payment_intents \
-H "Authorization: Bearer sk_test_..." \
-H "Idempotency-Key: $(uuidgen)" \
-H "Content-Type: application/json" \
-d "{
\"amount\": 25000,
\"currency\": \"GHS\",
\"method\": \"mobile_money\",
\"network\": \"MTN\",
\"customer\": { \"phone\": \"0244123456\" },
\"reference\": \"$REF\"
}"In test mode no real prompt is sent, so simulate the customer approving by posting the callback the gateway would have sent.
curl http://localhost:3001/provider/mock/callback/YOUR_CALLBACK_TOKEN \
-H "Content-Type: application/json" \
-d "{\"order_id\": \"MOCK-$REF\", \"status\": \"COMPLETED\", \"amount\": \"250.00\"}"Your webhook fires within a few seconds. The balance in your dashboard moves by the net amount, after fees: on this GH₵ 250.00 payment the customer pays GH₵ 257.50 (the 3% fee on top) and you are credited GH₵ 247.50 — 99% of the requested amount, after the 1% settlement deduction. There is no reserve withholding.
2. A payment that fails
Same two calls, but with fail in the reference. Note what happens: the callback still claims COMPLETED, and the merchant is still not credited.
REF="fail-$(uuidgen | cut -c1-8)" # ... same create, same callback claiming COMPLETED ... # Result: status is "failed". No ledger entry. No credit.
This is the forged-callback test
3. A payment that goes unknown
REF="timeout-$(uuidgen | cut -c1-8)"
The intent sits at unknown. The poller re-queries on a widening backoff (10s, 30s, 1m, 5m, 15m, 1h, 3h, 6h, 12h, then hourly). It never resolves in test mode, which is the point: it lets you see your own waiting state hold for as long as you like.
After 24 hours a real one escalates to manual review and stops polling. To watch that without waiting a day, age the row:
UPDATE charges SET created_at = now() - interval '25 hours', next_poll_at = now() WHERE status = 'unknown';
4. Prove you cannot double-charge
Send the identical request twice with the same idempotency key.
IDEM=$(uuidgen) # run the same create twice with -H "Idempotency-Key: $IDEM" # Second response is byte-identical, with idempotent-replay: true # One charge exists, not two.
Then change the amount but keep the key. You get a 409 rather than a surprise charge.
5. Test your webhook handler
Three things worth deliberately breaking:
| Try this | Correct behaviour |
|---|---|
| Send the same event id twice | Your handler no-ops the second time. |
| Tamper with one byte of the body | Signature check fails, you return 400. |
| Replay an event from an hour ago | Timestamp is outside the 5 minute window, you reject it. |
6. Test the checkout page
Open the checkout_url from a test intent and watch the page, not just the API. Drive each state with the reference keywords above and the mock callback from §1:
| Do this | The page should |
|---|---|
| Open the checkout link | Show the merchant name, the amount and the description, then land straight on the consent screen — the registered account name and the total — with no number to type, because the intent already knows it. |
| Confirm | Send the prompt to the phone. Nothing is prompted before you agree. |
| Approve via the mock callback | Show the payment as processing while the prompt is out, then the receipt on success, with a Start new payment button — it stays there until the payer starts a new payment or refreshes. |
| Use a fail reference | Show the failure, offer a retry — and return the same way. |
| Use a pending reference | Stay processing. No second payment, no new prompt. |
| Use a timeout reference | Stay unknown and keep waiting. Never re-charge. |
Open the network tab while it polls: the page must not request the intent faster than every 5 seconds. And the return rule is absolute — pending or processing never redirects, only a recorded failure does; a recorded success stays on the receipt with a Start new payment button.
Before you go live
| Check | Why |
|---|---|
| Webhook signature verified against the raw body | Re-serialised JSON never matches. |
| Handler idempotent on event.id | Delivery is at-least-once. |
unknown treated as pending, never as failed | Or you double-charge real people. |
| Fulfilment on the webhook, not the browser redirect | The redirect is not proof of payment. |
| Secret key in a secrets manager | Not in the repo. |
| USSD fallback shown to the customer | Where the rail provides one — prompts do not always arrive. |
| Checkout polls no faster than every 5 seconds | The checkout re-query is throttled to once per 10 seconds per charge; there is no per-key rate limit. |
| Checkout returns after 3 seconds with a Return now link, only on a recorded failure; on success it stays on the receipt with a Start new payment button | Pending or processing never redirects. |
| Tested a decline, a timeout and a duplicate | All three will happen in your first week. |
When those pass, try the calls in the Payments reference against your own test account.
Verify production with a real GH₵1
Test mode proves your code; it cannot prove the live gateway. Before you call a deploy done, run one real GH₵1 payment with a live key: create the intent, open the checkout_url, confirm on the consent screen, approve the prompt on the phone with your MoMo PIN, and watch it settle. A payment approved on the handset credits the merchant in about 5–7 seconds with the page open, within 15–20 seconds with it closed. Confirm the live build first with GET /version — it answers the commit the API is running.
