Charge API
Collect Mobile Money payments inside your own website or app, with your own screens, without sending customers to the Wire Gh checkout.
Server side only
The Charge API uses your secret key, so every call must come from your server. Your app collects the number and sends it to your server, and your server calls Wire Gh.
Mobile Money only
The Charge API takes Mobile Money. Wire Gh wallet payments need the customer's wallet PIN, which is only ever entered on the Wire Gh checkout or popup, never in a merchant's app.
How it works
- Collect the detailsIn your app, ask for the customer's Mobile Money number. You can show the network for them: it follows from the first three digits (see networks).
- Create the chargeYour server calls Create Charge with the email, amount and number.
- Guide the customerMost charges come back as
pay_offline: the customer gets a prompt on their phone and approves it with their Mobile Money PIN. Show themdisplay_textwhile they do this. - Confirm the resultListen for the
charge.successwebhook, or check with Check Pending Charge. Then verify and give value.
Create a charge
curl https://api.wiregh.com/charge \
-H "Authorization: Bearer YOUR_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"email": "customer@email.com",
"amount": 5000,
"mobile_money": {
"phone": "0551234987",
"provider": "mtn"
}
}' \
-X POSTconst response = await fetch('https://api.wiregh.com/charge', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_SECRET_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: 'customer@email.com',
amount: 5000,
mobile_money: {
phone: '0551234987',
provider: 'mtn'
}
})
});
const result = await response.json();
console.log(result);<?php
$ch = curl_init('https://api.wiregh.com/charge');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode([
'email' => 'customer@email.com',
'amount' => 5000,
'mobile_money' => [
'phone' => '0551234987',
'provider' => 'mtn',
],
]),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer YOUR_SECRET_KEY',
'Content-Type: application/json',
],
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($result);Handle the response
The data.status field tells you the next step:
| Status | Meaning | Next step |
|---|---|---|
pay_offline | A prompt is on the customer's phone. | Show display_text. Wait for the webhook, or check again after 10 seconds. |
send_otp | The network sent the customer a code. | Collect it and call Submit OTP. |
success | The payment is complete. | Verify and give value. |
failed | The charge did not go through. | Show gateway_response, for example "Declined by customer", and offer to try again with a new charge. |
{
"status": true,
"message": "Charge attempted, awaiting customer approval",
"data": {
"reference": "WG261109798FAXRFERYSSG",
"status": "pay_offline",
"display_text": "Approve the prompt on your phone to complete this payment.",
"gateway_response": "Awaiting customer approval",
"retry_allowed": false
}
}A customer has up to 30 minutes from the start of the transaction to approve the prompt. If they never do, the transaction fails with gateway_response "Timed out waiting for customer" and a charge.failed webhook is sent.
Full example
// 1. Charge the number (on your server)
const charge = await wireGh('POST', '/charge', {
email: 'customer@email.com',
amount: 5000,
mobile_money: { phone: '0551234987', provider: 'mtn' },
metadata: { order_id: '1042' }
});
// 2. Act on the status
switch (charge.data.status) {
case 'success':
// Paid. Verify, then give value.
break;
case 'pay_offline':
// Show charge.data.display_text and wait for the charge.success webhook
break;
case 'send_otp':
// Ask the customer for the code, then call /charge/submit_otp
break;
default:
// failed: show charge.data.gateway_response and let them try again
}
async function wireGh(method, path, body) {
const res = await fetch('https://api.wiregh.com' + path, {
method,
headers: {
Authorization: 'Bearer ' + process.env.WIREGH_SECRET_KEY,
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : undefined
});
return res.json();
}<?php
function wiregh(string $method, string $path, ?array $body = null): array
{
$ch = curl_init('https://api.wiregh.com' . $path);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $body ? json_encode($body) : null,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('WIREGH_SECRET_KEY'),
'Content-Type: application/json',
],
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
return $result;
}
// 1. Charge the number
$charge = wiregh('POST', '/charge', [
'email' => 'customer@email.com',
'amount' => 5000,
'mobile_money' => ['phone' => '0551234987', 'provider' => 'mtn'],
]);
// 2. Act on the status
switch ($charge['data']['status'] ?? 'failed') {
case 'success': /* Paid. Verify, then give value. */ break;
case 'pay_offline': /* Show display_text, wait for the webhook */ break;
case 'send_otp': /* Ask for the code, call /charge/submit_otp */ break;
default: /* failed: show gateway_response */
}Good practice
- Send an
Idempotency-Keywith every charge, so a network retry never charges your customer twice. - Do not check the status in a tight loop. Webhooks are faster and lighter, and repeated checks count towards your rate limit.
- After five failed charges in an hour, a number is paused for an hour. Show the customer a clear message rather than retrying.
- Use the test numbers to try every status before you go live.