Skip to content

Cancel a charge ​

Stops a pending charge from being paid. A Pay Bill charge stops matching payments at once, and the hosted page stops taking new attempts.

Cancelling cannot recall a prompt that is already on the payer's phone. If the payer approves it anyway, the payment is booked and the charge moves to succeeded, with payment.succeeded sent. Cancelling does not refund anything.

Endpoint ​

POST /v1/payments/merchant/charges/{id}/cancel
AuthSecret key, Authorization: Bearer mkp_…
Scopecharges:write
Who can cancelOnly the merchant or platform that made the charge. A seller cannot cancel a charge a platform made for it
Feature flagRegistered only where direct charges are on

Headers ​

HeaderRequiredValue
AuthorizationYesBearer mkp_test_… or Bearer mkp_live_…
Content-TypeOnly with a bodyapplication/json

No Idempotency-Key is needed: cancelling a charge that is already cancelled answers it again with 200.

Parameters ​

Path ​

NameTypeRequiredConstraints and meaning
idstringYesThe charge id, ch_<country>_<24 hex>

Body ​

None. Send no body, or {}. Anything else is 400, code 1701, cancel takes no body. An empty body sent with Content-Type: application/json is not valid JSON, so either leave out the header or send {}.

Example request ​

sh
curl -X POST https://payments-ke-staging.mookhpay.co/v1/payments/merchant/charges/ch_ke_7e1a4c9b2d5f80e3a6c1b047/cancel \
  -H "Authorization: Bearer $MOOKHPAY_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
js
const BASE = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant";
const id = "ch_ke_7e1a4c9b2d5f80e3a6c1b047";

const res = await fetch(`${BASE}/charges/${id}/cancel`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MOOKHPAY_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: "{}",
});
const body = await res.json();
if (res.status === 409 && body.code === 3003) {
  console.log("Already ended; read the charge to see how");
} else if (!body.ok) {
  throw new Error(`${res.status} ${body.code}: ${body.description}`);
} else {
  console.log(body.data.status);
}
python
import os
import requests

BASE = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant"
charge_id = "ch_ke_7e1a4c9b2d5f80e3a6c1b047"

res = requests.post(
    f"{BASE}/charges/{charge_id}/cancel",
    headers={"Authorization": f"Bearer {os.environ['MOOKHPAY_SECRET_KEY']}"},
    json={},
    timeout=30,
)
body = res.json()
if res.status_code == 409 and body["code"] == 3003:
    print("Already ended; read the charge to see how")
elif not body["ok"]:
    raise RuntimeError(f"{res.status_code} {body['code']}: {body['description']}")
else:
    print(body["data"]["status"])
php
<?php
$base = "https://payments-ke-staging.mookhpay.co/v1/payments/merchant";
$id = "ch_ke_7e1a4c9b2d5f80e3a6c1b047";

$ch = curl_init("$base/charges/$id/cancel");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("MOOKHPAY_SECRET_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => "{}",
]);
$body = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);

if ($status === 409 && $body["code"] === 3003) {
    echo "Already ended; read the charge to see how", PHP_EOL;
} elseif (!$body["ok"]) {
    throw new RuntimeException("$status {$body['code']}: {$body['description']}");
} else {
    echo $body["data"]["status"], PHP_EOL;
}

Response ​

200 OK, with the charge as it is now. charge.cancelled is sent to your endpoints the first time a charge is cancelled.

json
{
  "ok": true,
  "description": "Charge cancelled",
  "data": {
    "id": "ch_ke_7e1a4c9b2d5f80e3a6c1b047",
    "status": "cancelled",
    "test": false,
    "amount": 150000,
    "currency": "KES",
    "fee": 0,
    "net": 150000,
    "reference": "order-8841",
    "description": "A3 print",
    "payment_method": { "type": "paybill" },
    "next_action": null,
    "failure": null,
    "receipt_number": null,
    "hosted_url": "https://mookhpay.co/checkout/cs_ke_0b4d2f7a9c1e53b8d6a2e410",
    "metadata": {},
    "expires_at": "2026-11-20T09:44:00.000Z",
    "created_at": "2026-11-20T09:14:00.000Z"
  }
}

Response fields ​

data is the charge object, with status cancelled and next_action null. Once cancelled, a new charge for the same reference is allowed.

Errors ​

HTTPCodeNameWhenWhat to do
4001701INVALID_REQUEST_DATAA body other than {}, or an empty body sent as JSONSend {} or no body
4003014IDENTITY_HEADER_REFUSEDThe request carries a session or identity headerRemove the header
4012781API_KEY_INVALIDMissing, malformed, unknown or revoked keyCheck the key
4032941API_KEY_SCOPE_REQUIREDThe key lacks charges:writeIssue a key with the scope
4033042API_KEY_IP_NOT_ALLOWEDThe account has allowed addresses and the call came from another addressCall from a listed address
4043001CHARGE_NOT_FOUNDNo such charge, or one you did not makeCheck the id
4093003CHARGE_NOT_CANCELLABLEThe charge has already succeeded, failed or expiredRead the charge to see how it ended
4291804RATE_LIMIT_EXCEEDEDOver the rate limitWait Retry-After seconds
5032782AUTHN_UNAVAILABLEKeys cannot be checkedRetry with backoff

Rate limit ​

120 requests a minute per key owner, shared with the charge reads and the checkout routes. Over it: 429, code 1804, Retry-After in seconds.

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