Balance Service
Topups and ledger
Recharge should not update balance immediately. Create a topup order first, then credit the wallet when payment is confirmed.
Recharge and ledger are separate concerns in @downcity/services.
Why "user clicked recharge" is not the same as "balance was credited"
Because those are often different steps:
- the user initiates a topup
- later an admin confirms it
- or a Stripe webhook confirms it
- only then should the wallet receive funds
That is why the service exposes:
createTopup()finishTopup()cancelTopup()
User-side recharge
const topup = await user.service("balance").action("topups/create").invoke({
amount: 500,
note: "manual recharge",
});This creates a pending topup order. It does not change balance yet.
Admin-side confirmation
await admin.service("balance").action("topups/finish").invoke({
topup_id: topup.topup_id,
});Confirmation does two things:
- marks the topup as
paid - credits the user balance
It also writes a topup ledger entry.
User-side endpoints
GET /v1/balance/mereturns user-facingbalancein credits, plus exactmicrocreditsGET /v1/balance/history/meGET /v1/balance/topups/mePOST /v1/balance/topups/createPOST /v1/balance/redeem-codes/redeem
Admin-side endpoints
GET /v1/balance/usersGET /v1/balance/historyGET /v1/balance/topupsGET /v1/balance/redeem-codesPOST /v1/balance/addPOST /v1/balance/subPOST /v1/balance/topups/finishPOST /v1/balance/topups/cancelPOST /v1/balance/redeem-codes/createPOST /v1/balance/redeem-codes/disable