Skip to content

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 paymentsYou pay into pockets
Who the customer paysThe seller, with your fee taken off (on_behalf_of)You, into your own main balance
Where the person's money sitsTheir main walletA pocket named after your platform, in their wallet
How money reaches themEach payment lands with themYou pay it in when you decide it's theirs
FitsMarketplaces where each sale belongs to one sellerWork you pay for later, escrow, payouts after approval, earnings split across many jobs
The restBalances, feeds and payouts read the whole accountBalances, 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 ​

  1. 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.
  2. 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.
  3. 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.
  4. 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 ​

ThingWhere it comes from
An organisation switched on as a platform, with a pocket set upMOOKHPay 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 scopesAn 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 endpointAPI 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.

ScopeUsed for
checkoutsTaking customers' payments into your main balance
accounts:writeOpening an account for each person
accounts:linkAsking someone who already has MOOKHPay to link their account
accounts:readReading pockets, balances and movements, and the fee quote
transfers:writePaying into pockets. Only an Owner can issue it, because it moves your own money
payouts:writePaying out of pockets, and setting the number a pocket pays out to
balance:readOptional. 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.statusMeansWhat to do
openingMOOKHPay is opening the pocket. It takes momentsWait. A pay-in now answers 409 with code 3063 (PLATFORM_POCKET_NOT_READY): retry it shortly with the same Idempotency-Key
connectedReady to pay into and out ofPay in
disconnectedThe person disconnected your platform in MOOKHPayStop 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.

FieldRequired
amountYesMinor units, in whole currency units: a multiple of 100. 150000 is KES 1,500.00
external_referenceYesYour 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)
remarksNoUp 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.

HTTPCodedata.errorWhat to do
4223060PLATFORM_BALANCE_INSUFFICIENTYour 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
4223062PLATFORM_TRANSFER_CAP_REACHEDYour 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
4093061PLATFORM_ACCOUNT_DISCONNECTEDThe person disconnected you. Keep the money in your own records
4093063PLATFORM_POCKET_NOT_READYThe pocket is still opening. Retry shortly with the same key
4223064PLATFORM_POCKET_NOT_SET_UPYour platform has no pocket. Ask MOOKHPay
4092945PLATFORM_REFERENCE_TAKENAnother pay-in already used this external_reference
4223015IDEMPOTENCY_KEY_REUSEDThe key was used with a different body
4013021PAYOUT_SIGNATURE_INVALIDThe 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 /fees before you show a "withdraw all" amount, and record the fee the payout answers with: that one is final;
  • answers 202 and settles later with the payout.paid or payout.failed webhook. 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.

  1. POST /accounts/:id/payout-destination with rail (mpesa in Kenya) and phone. MOOKHPay texts a six-digit code to that number and answers { verification_id, expires_at }. One code a minute per account.
  2. The person types the code into your app. POST /accounts/:id/payout-destination/verify with verification_id and code answers { 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/balance answers the pocket's available balance.
  • GET /accounts/:id/transactions lists the pocket's movements. type is transfer_in (your pay-in), payout, moved_in, moved_out or payment_out. Money the person moved themselves reads "Moved by you", with reference null.

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:

EventWhat to do
payment.succeeded, payment.failedA customer's checkout. Read the checkout back before you credit anything; see Confirming payments
account.linkedThe person approved your link. Their pocket is opening: pay in what you owe them
account.disconnectedStop paying in. Keep what you owe them in your own records
payout.paid, payout.failedA 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:

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.

Staging documentation. A checkout made with a secret key on staging charges a real phone. See Environments.