SMS notices from your own WHMCS: a hook that calls our SMS API on invoices and payments

WhatsApp & APIs·· 16 views· 3 min read

The short answer: if you run your own WHMCS (to resell hosting or services), a short hook can call our SMS API when an invoice is created, when WHMCS sends a reminder or when a payment comes in. The SMS comes out of your account's credit balance, under NETMSG or your own Sender ID.

The moments that matter

WHMCS hook When it runs Example SMS
InvoiceCreated When an invoice is created Invoice [No.] for [AMOUNT] is ready. It is due on [DATE].
InvoicePaymentReminder When WHMCS sends a payment reminder Invoice [No.] is due on [DATE]. You can pay on our portal.
InvoicePaid When the invoice is paid We have received payment for invoice [No.]. Thank you.

When WHMCS creates invoices and sends reminders depends on its automation: see WHMCS automation settings.

A minimal hook

Save a file in your WHMCS includes/hooks/ folder. This example sends a notice when an invoice is paid:

<?php
// includes/hooks/sms_avisos.php
add_hook('InvoicePaid', 1, function ($vars) {
$fac = localAPI('GetInvoice', ['invoiceid' => $vars['invoiceid']]);
$cli = localAPI('GetClientsDetails', ['clientid' => $fac['userid']]);
$num = preg_replace('/[^0-9+]/', '', $cli['phonenumber']);
if ($num === '') { return; }
$chave = trim(file_get_contents('/home/USERNAME/.sms-api-chave'));
$ch = curl_init('https://api.interweb.co.mz/v1/sms/messages');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $chave,
'Content-Type: application/json',
'Idempotency-Key: factura-paga-' . $vars['invoiceid'],
],
CURLOPT_POSTFIELDS => json_encode([
'number' => $num,
'text' => 'We have received payment for invoice ' . $vars['invoiceid'] . '. Thank you.',
]),
]);
curl_exec($ch);
curl_close($ch);
});

1 The key lives in a file outside the public folder, never in the code. Create it on the API page in Meu Interweb with only sms:write and, if WHMCS always runs on the same server, restricted to its IP.
2 The number comes from the client's phone in WHMCS. The example strips dots, spaces and dashes; our API accepts numbers with or without the country code, because the system can tell the country from the first digit: 841234567 goes to Mozambique and 923456789 to Angola. So +258 84 123 4567 and 258841234567 are the same number as 841234567.
3 The Idempotency-Key includes the invoice number: if the hook fires twice for the same invoice, the customer gets one SMS.
4 The 10-second timeout stops a network problem from leaving WHMCS hanging.
Test with a sandbox_ key first. The request is checked but nothing is sent or charged. Only then switch to the prod_ key. And remember: customers cannot reply to the SMS, so say how they can reach you.
For the other hooks the pattern is the same: change the hook name and the text. If a customer pays before a reminder you scheduled, you can cancel it: cancelling a scheduled SMS.

Want your WHMCS to notify customers by SMS? See the API page and create the key.

See the API page

SEE ALSO

What WHMCS is, and when you need it

WHMCS automation settings: when invoices go out, reminders, suspension and termination

Payment reminder SMS: when to send it, what to write and ready-to-use examples

Sending SMS through the API: key, permissions, Sandbox and your first request

Cancelling a scheduled SMS through the API, for example when the customer has already paid

An API key or token has leaked: what to do, in the right order

RECOMMENDED PRODUCT

Reseller hosting with WHM

Create, host and bill your own plans, under your own brand. from $29.90/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.