Skip to content

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.

  1. The caller reaches Collect Payment

    Usually after a question that saved the amount, such as amount.
  2. 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.
  3. The session ends with your message

    Approve the GHS 20.00 payment on the prompt that follows. Thank you!
  4. The caller approves on their phone

    Their network shows its own PIN prompt. Your provider then reports the result.
  5. 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.
Good to know: You need your own account with a payment provider. Asterisks never holds the money: it goes straight from the caller to your provider account.

Set up Collect Payment

  1. Collect the amount first

    Add an Ask a Question screen with Answer type Amount and Save the answer as amount. Skip this if the price is fixed.
  2. Add Collect Payment

    Click Collect Payment in the toolbox (under Actions) and connect the question to it.
  3. 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."
  4. Fill in the Payment section

    Use the values from your provider's API documentation:
    SettingWhat to enter
    Payment providerYour provider, or Custom for any other.
    API URLThe provider’s “charge” or “request payment” address.
    API keyYour provider's secret key. It is sent as Authorization: Bearer <key>.
    HTTP methodAlmost always POST.
    AmountWhat to charge: a saved answer like {{amount}}, or a fixed price like 10.00.
    CurrencyGHS unless your provider needs another.
    Callback URLLeave empty, so the result comes to Asterisks.
    Request bodyThe JSON your provider expects. A new step starts with one that charges the dialler; rename the fields to match your provider.
    HeadersAny extra headers your provider asks for.
  5. 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.
Watch out: Asterisks only sends what is in the API URL, Request body and Headers. The Amount, Currency and Callback URL boxes reach your provider only where you write {{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:

Request body
{
  "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.

Watch out: Use {{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 sendsRecorded as
success, successful, paid, completed, approvedPayment completed
failed, declined, cancelledPayment failed
anything elseThe 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.

  1. The caller approves, then dials your shortcode again

    Say so in the closing message: "Approve the prompt, then dial again for your receipt."
  2. Asterisks checks what your provider reported

    Paid: the caller continues down PAID. Failed or declined: down FAIL.
  3. Nothing reported yet

    We have not received your payment yet. Approve the prompt on your phone.
    1. Check again
    2. Cancel
    2 takes the FAIL route.

After either route, {{PaymentStatus}} holds success, failed or cancelled, for a later Smart Router or a request to your system.

Good to know: Check flow warns when PAID is connected but the result cannot arrive: when you filled in your own Callback URL, or when the request never sends {{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.