Appearance
Refunds
A refund sends money from a paid checkout or charge back to the payer, all of it or part. Your server asks for it with POST /refunds; MOOKHPay reviews every refund before any money moves, then sends it back or declines it, and tells your endpoints which.
The money comes out of the account that was paid, the account that owns the key. A payment taken through a platform cannot be refunded.
| Scope | refunds:write. In an organisation, only an Owner can issue it |
| Review | Every refund, by MOOKHPay, before money moves |
| Switched on | Per deployment (REFUND_API_ENABLED). Where it is off, /refunds answers 404 |
Get a key with refunds:write
An Owner replaces the account's key in the hub with one that has refunds:write as well as the scopes the integration already uses: API Integrations, API Keys, Secret Key, Replace Key. The old key keeps working for 24 hours while you deploy the new one. See API keys and scopes.
A key without the scope answers 403 and code 2941.
How a refund moves
status | Meaning | Final |
|---|---|---|
requested | Waiting for MOOKHPay to review it. Nothing has moved | No |
approved | MOOKHPay approved it and is sending the money back | No |
refunded | The money is back with the payer. provider_receipt and refunded_at are set | Yes |
declined | MOOKHPay declined it. Nothing moved. decline_reason says why | Yes |
If an attempt to send the money fails, the refund goes back to requested for MOOKHPay to look at again, and refund.failed is sent. MOOKHPay can then try again, send it another way, or decline it. See refund.failed.
Ask for a refund
Name the payment by exactly one of checkout_id (a hosted checkout) or charge_id (a direct charge). Leave amount out to refund all that is left, or send a part. Send an Idempotency-Key you stored with the refund in your own records, and reuse it on every retry. See Idempotency.
sh
curl -X POST https://payments-ke-staging.mookhpay.co/v1/payments/merchant/refunds \
-H "Authorization: Bearer $MOOKHPAY_SECRET_KEY" \
-H "Idempotency-Key: refund-order-1042-1" \
-H "Content-Type: application/json" \
-d '{
"checkout_id": "cs_ke_3f9a1c7e5b2d4a8e6c0b91d7",
"amount": 50000,
"reason": "One latte was not served"
}'js
const BASE = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant";
const res = await fetch(`${BASE}/refunds`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.MOOKHPAY_SECRET_KEY}`,
"Idempotency-Key": "refund-order-1042-1",
"Content-Type": "application/json",
},
body: JSON.stringify({
checkout_id: "cs_ke_3f9a1c7e5b2d4a8e6c0b91d7",
amount: 50000,
reason: "One latte was not served",
}),
});
const answer = await res.json();
if (!answer.ok) throw new Error(`${res.status} ${answer.code}: ${answer.description}`);
console.log(answer.data.id, answer.data.status);python
import os
import requests
BASE = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant"
res = requests.post(
f"{BASE}/refunds",
json={
"checkout_id": "cs_ke_3f9a1c7e5b2d4a8e6c0b91d7",
"amount": 50000,
"reason": "One latte was not served",
},
headers={
"Authorization": f"Bearer {os.environ['MOOKHPAY_SECRET_KEY']}",
"Idempotency-Key": "refund-order-1042-1",
},
timeout=15,
)
answer = res.json()
if not answer["ok"]:
raise RuntimeError(f"{res.status_code} {answer['code']}: {answer['description']}")
print(answer["data"]["id"], answer["data"]["status"])php
<?php
$base = 'https://payments-ke-staging.mookhpay.co/v1/payments/merchant';
$ch = curl_init("$base/refunds");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode([
'checkout_id' => 'cs_ke_3f9a1c7e5b2d4a8e6c0b91d7',
'amount' => 50000,
'reason' => 'One latte was not served',
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('MOOKHPAY_SECRET_KEY'),
'Idempotency-Key: refund-order-1042-1',
'Content-Type: application/json',
],
]);
$answer = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (!$answer['ok']) {
throw new RuntimeException("$status {$answer['code']}: {$answer['description']}");
}
echo $answer['data']['id'], ' ', $answer['data']['status'], PHP_EOL;The answer is 201 with a refund object in requested. Store its id (rf_…) against your order.
Amounts
amountis an integer in minor units, like every amount you send. Leave it out to refund all that is left of the payment.- A part refund must be whole currency units: a multiple of
100. Otherwise422and code2702. Refunding all that is left need not be whole units. - The refund can be no more than what is left: the payment's amount less what has been refunded already. Asking for more answers
422and code2863, with what is left indata.refundable.
One open refund per payment
A payment can have one refund waiting at a time. Asking again while one is requested or approved answers 409 and code 2862. Once it is refunded or declined you can ask again, while something is left. Refunds asked for in the hub count too, and GET /refunds lists them alongside yours.
What cannot be refunded
409 and code 2861 mean the payment cannot be refunded. data.reason says why, and retrying will not help:
data.reason | Meaning |
|---|---|
not_paid | The payment has not been paid |
nothing_left | The payment has been refunded in full already |
other_refund | The payment is already being refunded another way, such as a card refund MOOKHPay staff started |
platform_payment | The payment was taken through a platform, for a seller. These cannot be refunded through MOOKHPay |
test_payment | The payment was a test, made with an mkp_test_ key or a test number. Test payments are never refunded |
not_inbound | The payment is not one somebody made to this account, such as a top-up |
An id that is not yours answers the same 404 as an unknown one: 2783 for a checkout, 3001 for a charge.
Follow a refund
Subscribe your endpoint to refund.succeeded, refund.declined and refund.failed, and act on the refund's status when one arrives. To check without a webhook, read GET /refunds/:id, or every refund of a payment with GET /refunds?checkout_id=.
| Event | Then |
|---|---|
refund.succeeded | The money is back with the payer. Mark the order refunded |
refund.declined | Nothing moved. decline_reason says why. Ask again if the reason can be fixed |
refund.failed | Nothing moved yet. The refund is open again; wait for refund.succeeded or refund.declined |
How the money goes back
MOOKHPay chooses the way back from how the payer paid: to the MOOKHPay balance they paid from, to their card, or as a payout to their phone. Where none of these can be used, for example for part of a card payment, MOOKHPay sends the money back by hand, and the refund still ends refunded. Your server does not choose.
What a refund costs
A refund paid back to the payer's phone is a payout, and follows the payout fee, which is free for now. The payer receives the full refund amount. If a payout fee is ever charged, your account pays it on top of the refund.
A refund to a MOOKHPay balance or to a card costs nothing. The refund's fee shows what was charged, once method says how the money goes back. If sending the money fails, the fee comes back with the amount. See Fees.