Payments
Charge callers through Paystack, Hubtel, Flutterwave, MTN MoMo or any provider, and record the result.
How payments work
The Collect Payment screen asks your payment provider (Paystack, Hubtel, Flutterwave, MTN MoMo or any other with a web API) to charge the caller, then ends the USSD session with a message. The caller approves the payment on the mobile money prompt that their network sends next.
The caller reaches Collect Payment
Usually after a question that saved the amount, such asamount.Asterisks asks your provider to charge them
It sends the request you set up, using your provider account and API key. Unless you say otherwise, the number charged is the one that dialled.The session ends with your message
Approve the GHS 20.00 payment on the prompt that follows. Thank you!
The caller approves on their phone
Their network shows its own PIN prompt. Your provider then reports the result.The caller dials back (optional)
If you connected the PAID route, dialling your shortcode again picks up where they left off. See Carrying on after the payment.
Set up Collect Payment
Collect the amount first
Add an Ask a Question screen with Answer type Amount and Save the answer asamount. Skip this if the price is fixed.Add Collect Payment
Click Collect Payment in the toolbox (under Actions) and connect the question to it.Write the closing message
In What the phone shows, tell the caller what happens next. If you leave it empty they see "Please check your phone to authorize the payment."Fill in the Payment section
Use the values from your provider's API documentation:Setting What to enter Payment provider Your provider, or Custom for any other. API URL The provider’s “charge” or “request payment” address. API key Your provider's secret key. It is sent as Authorization: Bearer <key>.HTTP method Almost always POST. Amount What to charge: a saved answer like {{amount}}, or a fixed price like10.00.Currency GHS unless your provider needs another. Callback URL Leave empty, so the result comes to Asterisks. Request body The JSON your provider expects. A new step starts with one that charges the dialler; rename the fields to match your provider. Headers Any extra headers your provider asks for. Save and test with test keys
Use your provider's test (sandbox) key first and run the flow from a real phone. Switch to your live key once a test payment goes through.
{{PaymentAmount}}, {{PaymentCurrency}} and {{PaymentCallbackUrl}} in one of those three.Example request
A typical mobile money request body. Field names differ by provider, so copy the shape from your provider's documentation:
{
"amount": "{{PaymentAmount}}",
"currency": "{{PaymentCurrency}}",
"phone": "{{MobileNumber}}",
"reference": "{{SessionId}}",
"callback_url": "{{PaymentCallbackUrl}}"
}{{MobileNumber}}is the number that dialled. To charge another number, ask for it in a question, save it as, say,payerNumber, and write{{payerNumber}}here.{{SessionId}}as the reference lets you match the payment to the USSD session later.- Some providers want the amount in the smallest unit (pesewas), so 20 cedis is 2000. Check your provider's documentation.
Getting the result
Your provider reports the result to a web address. Write {{PaymentCallbackUrl}} wherever your provider takes that address, usually a field in the request body as above. Asterisks fills in an address made for that one payment and records the result against the session.
{{PaymentCallbackUrl}}, not an address you type out. Each one carries a token for its session, and a notification without a valid token is refused. A provider that only accepts one fixed address for the whole account cannot report to Asterisks; have it notify your own system.Asterisks reads the status from data.status, Data.Status or status in the notification:
| Provider sends | Recorded as |
|---|---|
| success, successful, paid, completed, approved | Payment completed |
| failed, declined, cancelled | Payment failed |
| anything else | The status as sent |
Carrying on after the payment
Collect Payment has two dots besides its usual end: PAID and FAIL. With nothing on PAID, the session ends for good once the request is sent. Connect PAID and the session is kept, so the caller can dial again and carry on.
The caller approves, then dials your shortcode again
Say so in the closing message: "Approve the prompt, then dial again for your receipt."Asterisks checks what your provider reported
Paid: the caller continues down PAID. Failed or declined: down FAIL.Nothing reported yet
We have not received your payment yet. Approve the prompt on your phone. 1. Check again 2. Cancel
2takes the FAIL route.
After either route, {{PaymentStatus}} holds success, failed or cancelled, for a later Smart Router or a request to your system.
{{PaymentCallbackUrl}}.Delivering what the caller paid for
Some callers pay and never dial back. To deliver the goods (send a receipt SMS, credit an account), have your provider notify your own system as well, and act on its confirmation there. Give each payment a reference such as {{SessionId}}, and credit a reference once however many times it arrives. Never deliver on the USSD request alone, before the provider confirms.
The bill payment tutorial builds this from start to finish.
If the request fails
Your provider must answer within 2 seconds. If it refuses the request, the caller sees "Payment could not be initiated. Please try again later.", or takes the FAIL route when one is connected. If it is too slow or can't be reached, they see "An error occurred while processing payment. Please try again." Either way the session ends.
- Double-check the API URL and API key (test keys only work with test addresses, and live keys with live ones).
- Check the body is valid JSON: every quote and comma in place.
- Make sure the amount is in the format your provider expects.