bKash tokenized payments: what to store and what to never store
How a saved bKash account actually works, what lives in the database, and why an order is never marked paid from a client callback.
bKash is how a large share of Bangladesh pays online. Tokenized payment is the version where a returning customer does not re-enter their account details on every order.
That convenience is the product requirement. Everything below is the engineering it costs.
The two flows
First payment. The customer authorises the payment with bKash directly. If they choose to save the account, the gateway returns an agreement reference. That reference, not their account, is what comes back to you.
Later payments. The saved agreement is used to create a payment without the customer re-entering anything. They confirm, and it completes.
The customer experiences one as several steps and the other as two taps. Underneath they are different API sequences with different failure modes.
What the database holds
create table payment_agreements (
id uuid primary key,
user_id uuid not null references users(id),
agreement_id text not null, -- from the gateway
masked_account text not null, -- e.g. 017****1234, for display only
status text not null, -- active | cancelled | expired
created_at timestamptz not null default now(),
unique (user_id, agreement_id)
);
create table payment_attempts (
id uuid primary key,
order_id uuid not null references orders(id),
agreement_id text,
payment_id text, -- gateway payment reference
amount int not null, -- minor units
status text not null, -- created | executed | verified | failed
idempotency_key text not null,
raw_response jsonb,
created_at timestamptz not null default now(),
unique (idempotency_key)
);What is not in there: no account number, no PIN, no OTP, nothing that could be used to move money outside your merchant relationship. The masked account exists so the customer can recognise which account they saved. It has no other purpose and must never be treated as an identifier.
The mobile app stores none of this. Not the agreement, not a cached token, nothing. It asks the server, which is the only party in a position to be trusted with it.
The rule that matters most
An order becomes paid on a server to server verification. Never on a client callback.
The client is told the payment succeeded. That is a hint, not a fact. It can be replayed, tampered with, or simply never arrive because the customer's connection dropped between paying and returning to the app.
So the sequence is:
- Create the payment server side. Store the attempt with an idempotency key derived from the order, so a retry cannot create a second payment.
- The customer authorises.
- The client tells the server it thinks it is done. The server treats this as a prompt to check, nothing more.
- The server calls the gateway's query or execute endpoint and reads the authoritative status.
- Only then does the order move to paid, in a transaction that also writes the payment record.
The window between step 2 and step 5 is real. The customer has paid and the order is not yet confirmed. Hiding that behind an optimistic success screen is how you end up explaining to someone why their confirmed order has no payment against it. It is shown honestly instead, as a pending state that resolves.
Saving an account is opt in and reversible
A saved payment agreement the customer does not remember agreeing to is a trust problem before it is a support problem. So: an explicit checkbox at payment time, never a default, and a visible way to remove a saved account that actually cancels the agreement with the gateway rather than just deleting your row.
Deleting the row without cancelling upstream leaves an active agreement the customer believes is gone. That is the worst of both.
Reconciliation
Gateways go down mid flow. Customers close the app between authorising and returning. So
there is a job that finds attempts stuck in created or executed past a threshold and
queries the gateway for their real status.
Without it, every network blip becomes a support ticket and a manual database edit, and manual database edits on payment rows are how a business loses track of its own money.
Trade-offs
An extra server round trip in checkout. Slower by a few hundred milliseconds. The alternative is trusting the client about money.
Storing raw gateway responses. More storage and a discipline about what is in them. Worth it. The first time a payment is disputed, that column is the difference between an answer and a guess.