The most important state in a UPI app is "I don't know"
You scan a QR code at a shop, enter your UPI PIN, and the network drops.
Did the money move? Your app doesn't know. For a few seconds, nothing it can reach knows either. What the app does in those seconds decides whether people trust it with their money.
Most mobile screens get by with three states: loading, success and error. A payments app needs a fourth, and it's the one teams are most likely to skip.
Why "Failed" is the dangerous answer
When a request times out, the easy move is to catch the exception and show an error. In most apps that's fine. The user taps retry and nothing bad happens.
In a payments app it can cost someone money. The request may have reached the bank before the connection died. If you show "Failed", the user pays again, and now the shopkeeper has been paid twice.
A timeout doesn't mean the payment failed. It means you don't know, and the app should say exactly that.
Model it as a real state
Every screen reads the payment's state. Only the state machine changes it.
Draft
PIN entered → Submitting
Submitting
202 from backend → Pending
timeout or killed → Unknown
Unknown
status found → Pending
never started → Failed
Pending
bank confirms → Success
bank declines → Failed
debited, refunded → Reversed
While a payment is Unknown, the screen says "Checking your payment…". It doesn't say "Failed", and the user can't pay again until the state is resolved.
Save before you send
Users close apps and phones run out of battery. If the process dies mid-request, the app has to be able to pick the story back up, so the payment goes into the local database before the network call.
Future<void> submit(Payment payment) async {
// Written first, so a killed app can find it on next launch.
await db.save(payment.copyWith(status: Status.submitting));
try {
final result = await api.createPayment(
payment,
idempotencyKey: payment.id,
);
await db.save(payment.copyWith(
status: Status.pending,
upiTxnId: result.upiTxnId,
));
} on TimeoutException {
await db.save(payment.copyWith(status: Status.unknown));
}
}
On the next launch, any row still marked submitting or unknown triggers a status check. The app never guesses.
Stop double payments in three places
People tap twice, and retries happen on their own. Each of these layers catches what the others miss:
- UI: disable the Pay button on the first tap.
- Client: create the idempotency key when the payment screen opens, and reuse it for every retry. Never generate a new one on retry.
- Server: keep a unique index on
(user_id, idempotency_key). A repeat returns the original payment instead of creating another.
The server layer is the one that really protects the user. The other two mostly keep bad requests from getting that far.
Treat a push notification as a hint
The backend usually accepts a payment with a 202 and the bank answers later, so the app has to learn the result some other way. Use three methods together:
- Poll with backoff for about two minutes while the result screen is open.
- Push for when the app is in the background. Always confirm with the API before showing success, because a push can be late, duplicated or missing.
- Reconcile on launch by re-checking every payment that hasn't reached a final state.
Write the pending message with care
If the payee's bank times out, a payment can stay pending for minutes, and occasionally much longer. Someone is standing at a counter wondering where their money went. "Processing" doesn't help them. Tell them what they need to know:
Your money is safe. If this payment fails, it will be refunded to your account.
| What happened | What the user sees | What the system does |
|---|---|---|
| Network drops after PIN | Checking your payment… | Stays unknown; checks status; same key |
| App killed mid-payment | Pending payment on the home screen | Reconciles on launch |
| Payee's bank times out | Pending. Your money is safe. | Waits for final status; pushes when resolved |
| Debited, then failed | Failed. ₹149.50 will be refunded. | Tracks the reversal until refunded |
| Push never arrives | Correct status on next open | Status always comes from the API |
What I'd ask a team to test
The normal path through a payment is rarely where the trouble is. Before shipping, I'd want to see:
- A unit test for every state transition, especially the ones out of
Unknown. - A fake backend that can delay, drop and duplicate responses on demand.
- A manual run on a real low-end Android phone: enter the PIN, kill the app, reopen it.
- A plan for festival sales, when banks slow down and far more payments sit in pending.
The wider lesson
Most apps aren't payment apps, but the idea carries over to any write that matters: bookings, orders, medical data. A request that timed out is a question that hasn't been answered yet. Build the app to hold that question open, and to be honest with the user while it does.
This is a design walkthrough, not a description of any company's system. The full version, with API contracts, device binding and QR validation, is in my mobile system design case studies. For parsing UPI links and Bharat QR codes in Dart, see upi_kit.
I'm Chandra ObulaReddy, a Principal Software Engineer (Mobile) in Bangalore. I write about Flutter architecture, Bluetooth LE and leading mobile teams.