Points enter CASHPOCKET in one place and leave in two. An advertiser buys them by card. A tester spends them on mobile coupons or withdraws them to a bank account. Each of those edges touches a third party that the database cannot roll back, and the first one came with a business risk that shaped the whole design.
The structure, and the risk behind it
A rewards platform where points can be bought by card and turned back into cash looks, from a card company’s side, like a way to cash out a credit line. Payment providers and card issuers treat that as a cash-convertibility risk, and a service that carries it can have its card payments blocked. The internal reports on the Toss Payments integration record it as a risk that could have had payments blocked, and the payment structure was designed in response.
The structure I designed keeps the card rail and the cash rail apart.
| Flow | Who | Rail | Rule |
|---|---|---|---|
| Buy points | Advertisers | Card, through Toss Payments | The only thing a card buys. The server computes the price from the point quantity and rejects any other amount |
| Pay for a campaign | Advertisers | Points | A debit of points. No card is involved |
| Earn a reward | Testers | Points | Paid from the campaign’s budget after two evaluations |
| Buy a coupon | Testers | Points | Guarded by purchase rules that are stricter for products close to cash |
| Withdraw | Testers | Bank transfer | Only to a bank account whose holder has been verified. Requested by the tester, approved by an admin |
One more rule separates the rails. Advertiser accounts are refused in the shop, and so are admin accounts as a rule, because their points are for campaigns and settlement.
I took this structure through review with Toss Payments and the card issuers until the connection was completed.
Card top-up

A top-up has three steps, and the middle one happens on the provider’s page.
Create. The advertiser chooses a point quantity. The server validates the range, computes the amount to charge, and inserts a payment row in state created. The browser is sent to the provider with that order.
Confirm. When the browser returns, it calls the confirmation route. The pseudo-code below was written for this page.
confirm(order, caller):
reject unless the caller owns the order
if order.status == approved:
return "already approved"
claim: update the row created | failed -> approving
no row matched -> 409, another request holds it
call the provider's confirm API
transport or provider error -> status = failed (may be retried)
"already processed" -> look the payment up
lookup settles it -> continue
lookup does not -> status = approved_unverified (409)
reject unless the confirmed amount equals the order's amount
status = approved
credit the points through the kernel, key = this order
failure -> status = approved_credit_failed (an admin retries with the same key)
send the alert message
The claim is what keeps two confirmations from both reaching the provider. It is a conditional update on the status, so exactly one request moves the row to approving.
Three states exist only because the provider is a separate system.
failedis safe to retry, which is why the claim accepts it as a starting state.approved_unverifiedcovers the case where the provider reports that it has already processed this payment, and the follow-up lookup cannot establish the result. The money may have moved, so the order is parked.approved_credit_failedsays that the card was charged and the points were not credited. An admin retries the credit, and because the key is the order, a retry after a credit that had in fact gone through moves nothing.
The webhook. The provider also calls a webhook. It is authenticated by a signature over the payload, and the route answers that it is unavailable if the signing secret is not configured. It never credits points. It can mark an order failed only while the order is still in an early state, and a cancellation notice goes through the same revocation path as an admin’s cancel.
Cancelling a card payment
A cancellation has to undo two things in two systems, and the order matters.
- Revoke the points first, through the kernel. If the advertiser has already spent them and the balance is short, the cancellation stops with a conflict and the card is not refunded.
- Call the provider’s cancel API, with an idempotency header of its own.
- If the provider refuses, restore the points with a rollback key.
Revoking first means the failure case is “points removed, card not yet refunded, and then restored”, which costs nobody anything. The other order would have a moment where the card was refunded and the points were still spendable.
Each attempt gets a number from an atomic counter on the payment row, and the revoke and restore keys include it. The amount currently revoked is the sum of those rows, so the state can always be recomputed from the ledger.
Cash-out
A tester requests a withdrawal of points to their bank account.
Verification comes first. The account has to be verified before any request is accepted. The server looks up the account holder through a banking API and compares the name with the user’s. Only a successful match is stored as verified, and if storing it fails the account counts as unverified.
The request is one transaction. After the eligibility checks, one transaction does two writes: the kernel debits the points with the request’s key, and a request row is inserted in state PENDING. A test asserts that an insufficient balance leaves no request row behind, and another that the debit carries both the key and the session.
Admin states are conditional updates.
| From | To | What else happens |
|---|---|---|
PENDING | APPROVED | The withholding amount is computed. Push and alert messages go out after the update |
APPROVED | COMPLETED | Marked by an admin when the payout is done |
PENDING | REJECTED | The points are refunded with the request’s refund key. If the refund fails, the status returns to PENDING |
Each update names the state it expects. Two admins acting on the same request at once produce one success and one conflict.
The coupon shop
A purchase is one transaction with three writes: a conditional decrement of the product’s stock, the debit, and the purchase row. Coupons are then issued by the coupon provider, outside the transaction. If issuing fails, the purchase is compensated with a refund keyed by the purchase.
A refund runs the other way round. All coupons of the purchase are cancelled at the provider first, all or nothing, and only then does a transaction write the refund, the status and the stock.
Failure modes
| What happens | What the system does | Covered by |
|---|---|---|
| The browser confirms the same order twice | One request claims the row. The other gets a conflict, or “already approved” | Tests on the confirmation route for ownership and repeats |
| The amount sent does not match the point quantity | Rejected at creation | A test on order creation |
| The provider confirms but the credit fails | The order is parked as paid-not-credited and retried with the same key | The parked state, and the order key that makes the retry safe |
| A forged webhook arrives | The signature check rejects it | A webhook signature test |
| A cancellation is attempted after the points were spent | The revoke fails and the card is not refunded | A test on the cancel route against the kernel |
| The provider refuses a cancellation | The points are restored with the rollback key | The attempt-numbered revoke and restore keys |
| Two admins process one withdrawal | One conditional update wins, the other returns a conflict | A state test on the admin cash-out route |
| A withdrawal is requested with too few points | The transaction aborts and no request row exists | A test on the cash-out route |
Limits
- Provider calls are not transactional. Every path that crosses to a provider is a sequence with a compensating step, and the parked states exist for the cases where compensation cannot be decided by code.
- Approval is manual. An admin approves a withdrawal and marks it completed. The system records those states and guards each change.
- The shop guards are policy. They are thresholds chosen by people. They reduce the room for abuse and can be tuned.
