Appearance
Pay into your users' pockets
A pocket is a small wallet with your platform's name on it, inside a person's own MOOKHPay wallet. Your platform pays money into it from your own balance, and the person spends it, moves it or withdraws it. Use pockets when your customers pay you, and you then pay the people who did the work: creators, drivers, tutors, freelancers.
This page explains how pockets work and the four calls you need. For a whole working server, see the tutorial Build a creator payouts platform.
Where pockets are available
Pockets are switched on in Kenya. A platform also needs a pocket set up for it by MOOKHPay staff: its name, colours and the text people get when it opens. Until then, every pocket route answers 422 with code 3064 (PLATFORM_POCKET_NOT_SET_UP). Ask MOOKHPay to set one up when they switch your platform on.
Two ways to run a platform
| Sellers take payments | You pay into pockets | |
|---|---|---|
| Who the customer pays | The seller, with your fee taken off (on_behalf_of) | You, into your own main balance |
| Where the person's money sits | Their main wallet | A pocket named after your platform, in their wallet |
| How money reaches them | Each payment lands with them | You pay it in when you decide it's theirs |
| Fits | Marketplaces where each sale belongs to one seller | Work you pay for later, escrow, payouts after approval, earnings split across many jobs |
| The rest | Balances, feeds and payouts read the whole account | Balances, feeds and payouts read only the pocket |
You don't choose per call. A platform with a pocket set up works in pockets for every account it reaches. This is called pocket mode on these pages.
How money moves
- Customers pay you. Take payments into your own main balance with a hosted checkout, without
on_behalf_of. The money is yours until you pay it in. - You open an account for each person, or ask someone who already has MOOKHPay to link theirs. MOOKHPay opens your pocket in their wallet when the account is made or the link is approved. See Open the account.
- You pay in what you owe them, from your main balance to their pocket. There is no fee. From that moment the money is theirs.
- They withdraw it, or you pay it out for them to their phone. Either way, it leaves from the pocket.
MOOKHPay texts, emails and notifies the person every time you pay into their pocket, naming your platform and the pocket.
What you need
| Thing | Where it comes from |
|---|---|
| An organisation switched on as a platform, with a pocket set up | MOOKHPay staff. Agree your daily payout cap, your daily pay-in cap and your daily account limit with them. The pay-in cap starts at zero, so every pay-in is refused with 3062 until it is set |
| A secret key with the platform scopes | An Owner, on Create A Key in the hub. See Which scopes |
A payout signing secret (mps_…) | An Owner, in the hub under API Integrations. It signs pay-ins, payouts and payout-number changes |
| A webhook endpoint | API Integrations, Webhooks. See Webhooks |
Which scopes
One key does everything below. A scope cannot be added to a key later, so choose them all when the key is made.
| Scope | Used for |
|---|---|
checkouts | Taking customers' payments into your main balance |
accounts:write | Opening an account for each person |
accounts:link | Asking someone who already has MOOKHPay to link their account |
accounts:read | Reading pockets, balances and movements, and the fee quote |
transfers:write | Paying into pockets. Only an Owner can issue it, because it moves your own money |
payouts:write | Paying out of pockets, and setting the number a pocket pays out to |
balance:read | Optional. Reading your own main balance and its movements, to reconcile your books. See Reconcile |
This is not the checkouts-only key the hosted checkout quickstart recommends. A platform key moves money, so keep it on your server and, if you can, ask MOOKHPay to limit it to your servers' IP addresses.
Open the account
Open a MOOKHPay account for the person with POST /accounts, exactly as on Open seller accounts. If their phone number already has MOOKHPay, the call answers 409 with code 2944 (PLATFORM_ACCOUNT_EXISTS) and creates nothing: send them an account link instead, asking for accounts:read and payouts:write.
In pocket mode the account object carries a pocket:
json
"pocket": {
"slug": "kazi",
"name": "Kazi Studio",
"status": "opening",
"balance_currency": "KES"
}pocket.status | Means | What to do |
|---|---|---|
opening | MOOKHPay is opening the pocket. It takes moments | Wait. A pay-in now answers 409 with code 3063 (PLATFORM_POCKET_NOT_READY): retry it shortly with the same Idempotency-Key |
connected | Ready to pay into and out of | Pay in |
disconnected | The person disconnected your platform in MOOKHPay | Stop paying in. Every call about the account answers 409 with code 3061 (PLATFORM_ACCOUNT_DISCONNECTED). Keep what you owe them in your own records until they link again |
While your platform is connected, the person cannot close its pocket. They can move money out of it whenever they like: it is their money.
Pay into a pocket
POST /accounts/:id/transfers moves money from your main balance into the person's pocket, in one step, with no fee.
| Field | Required | |
|---|---|---|
amount | Yes | Minor units, in whole currency units: a multiple of 100. 150000 is KES 1,500.00 |
external_reference | Yes | Your own id for this pay-in, up to 128 characters. Unique across your platform: a second pay-in with the same reference answers 409 with code 2945 (PLATFORM_REFERENCE_TAKEN) |
remarks | No | Up to 100 characters. The person sees it on their pocket, so write it for them: Payment for the Safari Lager campaign |
The request needs an Idempotency-Key and must be signed with your payout signing secret, like a payout. The signature covers the timestamp, the method, the path, the Idempotency-Key and the raw body; see How to sign a request. Each signature is accepted once, so sign every retry again with a new timestamp and the same Idempotency-Key and body.
js
// payIn.mjs (Node 18+)
import { createHmac } from "node:crypto";
const BASE = process.env.MOOKHPAY_API_BASE; // https://payments-ke.mookhpay.co/v1/payments/merchant
export async function payIn(accountId, { amount, reference, remarks }) {
const url = new URL(`${BASE}/accounts/${accountId}/transfers`);
const body = JSON.stringify({ amount, external_reference: reference, remarks });
const idempotencyKey = reference; // one pay-in per reference, so retries can't pay twice
const timestamp = Math.floor(Date.now() / 1000).toString();
const signature =
"sha256=" +
createHmac("sha256", process.env.MOOKHPAY_PAYOUT_SIGNING_SECRET)
.update(`${timestamp}.POST.${url.pathname}.${idempotencyKey}.`)
.update(body)
.digest("hex");
const res = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.MOOKHPAY_SECRET_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": idempotencyKey,
"X-Mookh-Timestamp": timestamp,
"X-Mookh-Signature": signature,
},
body,
});
return { status: res.status, body: await res.json() };
}A pay-in that goes through answers 201 with status succeeded: the money is in the pocket. The same Idempotency-Key and body sent again answer the same pay-in with 200. Check that the amount it returns is the amount you sent.
If MOOKHPay can't finish booking it, the call answers 503 with code 3016, or your request times out. The pay-in is then processing: send the same request again (re-signed, same key and body) until it answers. Never use a new key for it: that would be a second pay-in.
There is no webhook for pay-ins. The call's answer is the confirmation, and GET /accounts/:id/transfers/:transfer_id reads one back, with status succeeded, processing or failed.
When a pay-in is refused
Pocket refusals carry the code's name in data.error, and sometimes more fields. Nothing moves on a refusal. Most leave the key free to send again later; 3060 does not.
| HTTP | Code | data.error | What to do |
|---|---|---|---|
422 | 3060 | PLATFORM_BALANCE_INSUFFICIENT | Your main balance is short. data.available is what it holds. This pay-in is closed: the same key answers 3060 again, and its external_reference is used up. Top up, then pay in again with a new key and a new reference |
422 | 3062 | PLATFORM_TRANSFER_CAP_REACHED | Your daily pay-in cap is used up (East Africa Time). data has cap, used and remaining. Retry with the same key after midnight EAT, or ask MOOKHPay to raise the cap |
409 | 3061 | PLATFORM_ACCOUNT_DISCONNECTED | The person disconnected you. Keep the money in your own records |
409 | 3063 | PLATFORM_POCKET_NOT_READY | The pocket is still opening. Retry shortly with the same key |
422 | 3064 | PLATFORM_POCKET_NOT_SET_UP | Your platform has no pocket. Ask MOOKHPay |
409 | 2945 | PLATFORM_REFERENCE_TAKEN | Another pay-in already used this external_reference |
422 | 3015 | IDEMPOTENCY_KEY_REUSED | The key was used with a different body |
401 | 3021 | PAYOUT_SIGNATURE_INVALID | The signature was missing, wrong, more than 5 minutes old or already used. Sign again |
Pay out of a pocket
The person can withdraw from the pocket in MOOKHPay themselves. To pay it out for them from your app, call POST /accounts/:id/payouts, signed the same way. In pocket mode a payout:
- comes out of the pocket, never the rest of their wallet;
- goes to the number set for the pocket (see below), else to their main payout account, else to their own phone;
- costs a fee on top of the amount, also taken from the pocket. Read the current fee with
GET /feesbefore you show a "withdraw all" amount, and record thefeethe payout answers with: that one is final; - answers
202and settles later with thepayout.paidorpayout.failedwebhook. A failed payout puts the money back in the pocket by itself.
A company account cannot be paid out through a pocket: it answers 422 with code 3037 (PLATFORM_COMPANY_ACCOUNTS_OFF). Its owners withdraw in MOOKHPay.
Set the number a pocket pays out to
If the person wants their withdrawals to go to another number, set it with a one-time code. MOOKHPay sends the code; your app only passes it on.
POST /accounts/:id/payout-destinationwithrail(mpesain Kenya) andphone. MOOKHPay texts a six-digit code to that number and answers{ verification_id, expires_at }. One code a minute per account.- The person types the code into your app.
POST /accounts/:id/payout-destination/verifywithverification_idandcodeanswers{ destination: { rail, phone_masked } }. Store only the masked number.
A wrong code answers 422 with code 3065 (OTP_INVALID) and data.tries_left. After five wrong tries, or ten minutes, it is 3066 (OTP_EXPIRED): start again. Both calls are signed and need payouts:write.
Show the pocket
In pocket mode these two routes see only your pocket, never the rest of the person's wallet:
GET /accounts/:id/balanceanswers the pocket'savailablebalance.GET /accounts/:id/transactionslists the pocket's movements.typeistransfer_in(your pay-in),payout,moved_in,moved_outorpayment_out. Money the person moved themselves reads "Moved by you", withreferencenull.
A common pattern is to show your own records of what you paid in and paid out, and MOOKHPay's movements only for what the person did themselves (moved_in, moved_out, payment_out), so nothing shows twice.
Webhooks
Subscribe your endpoint to:
| Event | What to do |
|---|---|
payment.succeeded, payment.failed | A customer's checkout. Read the checkout back before you credit anything; see Confirming payments |
account.linked | The person approved your link. Their pocket is opening: pay in what you owe them |
account.disconnected | Stop paying in. Keep what you owe them in your own records |
payout.paid, payout.failed | A payout from a pocket settled |
Webhooks are delivered at least once and in no fixed order, and can be late. Run a job every few minutes that does what a missed webhook would have done: resend pay-ins that got no answer or a 503 with the same key, read back payouts still on their way, and read pending account links. See Retries and ordering.
Reconcile your books
Your main balance holds every customer payment you haven't paid in yet, plus your own revenue. With balance:read on your key:
GET /balanceanswers your main balance and your pockets.GET /balance/transactionslists your main balance's movements, oldest first, with a cursor.
Compare available with what your own records say you should hold, every night. When they differ, pair each MOOKHPay movement with one of your records of the same direction and amount, nearest in time, and look at what's left over.
Related
- Build a creator payouts platform, the whole flow in one server
- How platforms work
- Pay out sellers and How to sign a request
- Errors