Skip to content

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.

Scoperefunds:write. In an organisation, only an Owner can issue it
ReviewEvery refund, by MOOKHPay, before money moves
Switched onPer 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 ​

statusMeaningFinal
requestedWaiting for MOOKHPay to review it. Nothing has movedNo
approvedMOOKHPay approved it and is sending the money backNo
refundedThe money is back with the payer. provider_receipt and refunded_at are setYes
declinedMOOKHPay declined it. Nothing moved. decline_reason says whyYes

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 ​

  • amount is 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. Otherwise 422 and code 2702. 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 422 and code 2863, with what is left in data.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.reasonMeaning
not_paidThe payment has not been paid
nothing_leftThe payment has been refunded in full already
other_refundThe payment is already being refunded another way, such as a card refund MOOKHPay staff started
platform_paymentThe payment was taken through a platform, for a seller. These cannot be refunded through MOOKHPay
test_paymentThe payment was a test, made with an mkp_test_ key or a test number. Test payments are never refunded
not_inboundThe 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=.

EventThen
refund.succeededThe money is back with the payer. Mark the order refunded
refund.declinedNothing moved. decline_reason says why. Ask again if the reason can be fixed
refund.failedNothing 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.

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