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.

FlowWhoRailRule
Buy pointsAdvertisersCard, through Toss PaymentsThe only thing a card buys. The server computes the price from the point quantity and rejects any other amount
Pay for a campaignAdvertisersPointsA debit of points. No card is involved
Earn a rewardTestersPointsPaid from the campaign’s budget after two evaluations
Buy a couponTestersPointsGuarded by purchase rules that are stricter for products close to cash
WithdrawTestersBank transferOnly 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 row of four states: created, where the order row is inserted; approving, claimed by one request; approved, where the provider confirmed and the amount was checked; and points credited, a kernel call keyed by the order id. Dashed arrows lead down from approving to failed, a provider error that can be retried; from approved to approved_unverified, where the provider says the payment was already done and a lookup did not settle it; and from points credited to approved_credit_failed, paid but not credited, retried by an admin with the same key. Two notes: the claim, moving the row to approving, is a conditional update, so a second confirmation for the same order finds it already claimed and is answered without calling the provider again; and the webhook never credits, it checks a signature and can only mark an order failed or cancelled, so points are credited in one place.
Figure 1. Card top-up as a state machine. The payment row is claimed before the provider is called, and points are credited once with the order's key. Three side states hold the cases that need a second look.

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.

  • failed is safe to retry, which is why the claim accepts it as a starting state.
  • approved_unverified covers 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_failed says 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.

  1. 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.
  2. Call the provider’s cancel API, with an idempotency header of its own.
  3. 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.

FromToWhat else happens
PENDINGAPPROVEDThe withholding amount is computed. Push and alert messages go out after the update
APPROVEDCOMPLETEDMarked by an admin when the payout is done
PENDINGREJECTEDThe 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 happensWhat the system doesCovered by
The browser confirms the same order twiceOne 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 quantityRejected at creationA test on order creation
The provider confirms but the credit failsThe order is parked as paid-not-credited and retried with the same keyThe parked state, and the order key that makes the retry safe
A forged webhook arrivesThe signature check rejects itA webhook signature test
A cancellation is attempted after the points were spentThe revoke fails and the card is not refundedA test on the cancel route against the kernel
The provider refuses a cancellationThe points are restored with the rollback keyThe attempt-numbered revoke and restore keys
Two admins process one withdrawalOne conditional update wins, the other returns a conflictA state test on the admin cash-out route
A withdrawal is requested with too few pointsThe transaction aborts and no request row existsA 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.