# Build a complete payment integration (/docs/get-started/first-payment)



> To collect a mobile money payment, send a payment request from your server, wait for Hubtel's callback with the final result, and check the status of any payment that has no result after 5 minutes.
**Before you start**

- **A Hubtel business with a merchant account**: Business APIs work through your business's merchant account. If you don't have one yet, sign up and set it up first. [Create one at business.hubtel.com](https://business.hubtel.com)
- **API ID and API key**: They identify you on every request. Send them using HTTP Basic authentication. [Payment keys](/dashboard/api-keys)
- **Collection account number**: This is where your customers' payments arrive. You'll add it to the request URL. [Account IDs](/dashboard/account-ids)
- **An allowlisted server IP address**: Send your server's public IP address to Hubtel to get it allowlisted. Until then, your requests return 403 Forbidden or time out. [IP allowlisting](/docs/networking)

Don't have these yet? [Start in the sandbox](/dashboard/keys) with your test keys while you set them up.

This guide uses Direct Receive Money because your server handles each stage of the payment directly. If you take payments on a website, [Online Checkout](/docs/guides/online-checkout) hosts the payment page for you, but the callback and reconciliation steps below still apply.

## How a mobile money payment works [#how-a-mobile-money-payment-works]

1. Your server sends a payment request to Hubtel. Hubtel returns `0001`: *pending*.
2. The customer receives a prompt on their phone. They can approve it with their PIN, decline it, or let it expire.
3. Hubtel sends the final result in a POST request to your `PrimaryCallbackUrl`.
4. If no callback arrives within 5 minutes, your server requests the result with **[Check a collection's status](/docs/api/v1/transaction-status/get-transaction-status)**.

Your integration needs to handle a customer who doesn't respond, a failed payment and a callback that never arrives.

## Prepare your integration [#prepare-your-integration]

Set the [example environment variables](/docs/get-started/keys#credentials), deploy your callback endpoint, and replace the sample customer number and callback URL. These examples call production and can move real money. To test first, use the [sandbox host, keys and test numbers](/docs/testing). In C#, initialise `apiId`, `apiKey` and `collectionAccount` from the corresponding environment variables.

## 1. Charge the wallet [#1-charge-the-wallet]

**cURL**

  ```bash
  curl https://rmp.hubtel.com/merchantaccount/merchants/$HUBTEL_COLLECTION_ACCOUNT/receive/mobilemoney \
    -u "$HUBTEL_API_ID:$HUBTEL_API_KEY" -H "Content-Type: application/json" \
    -d '{"CustomerMsisdn":"233249111411","Channel":"mtn-gh","Amount":45.00,"Description":"Order #1042","ClientReference":"order-1042","PrimaryCallbackUrl":"https://example.com/hubtel/callback"}'
  ```

**Node**

  ```ts
  const auth = Buffer.from(`${process.env.HUBTEL_API_ID}:${process.env.HUBTEL_API_KEY}`).toString("base64");

  const res = await fetch(`https://rmp.hubtel.com/merchantaccount/merchants/${process.env.HUBTEL_COLLECTION_ACCOUNT}/receive/mobilemoney`, {
    method: "POST",
    headers: { Authorization: `Basic ${auth}`, "Content-Type": "application/json" },
    body: JSON.stringify({
      CustomerMsisdn: "233249111411",
      Channel: "mtn-gh",
      Amount: 45.0,
      Description: "Order #1042",
      ClientReference: "order-1042",
      PrimaryCallbackUrl: "https://example.com/hubtel/callback",
    }),
  });
  const payment = await res.json();
  // payment.ResponseCode === "0001" → pending. Save order-1042 as "awaiting payment".
  ```

**PHP**

  ```php
  $url = 'https://rmp.hubtel.com/merchantaccount/merchants/' . getenv('HUBTEL_COLLECTION_ACCOUNT') . '/receive/mobilemoney';

  $ch = curl_init($url);
  curl_setopt_array($ch, [
      CURLOPT_POST => true,
      CURLOPT_USERPWD => getenv('HUBTEL_API_ID') . ':' . getenv('HUBTEL_API_KEY'),
      CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS => json_encode([
          'CustomerMsisdn' => '233249111411',
          'Channel' => 'mtn-gh',
          'Amount' => 45.00,
          'Description' => 'Order #1042',
          'ClientReference' => 'order-1042',
          'PrimaryCallbackUrl' => 'https://example.com/hubtel/callback',
      ]),
  ]);
  $payment = json_decode(curl_exec($ch), true);
  curl_close($ch);
  ```

**C#**

  ```csharp
  using System.Net.Http.Headers;
  using System.Net.Http.Json;
  using System.Text;
  using System.Text.Json;

  var http = new HttpClient();
  var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{apiId}:{apiKey}"));
  http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

  var res = await http.PostAsJsonAsync($"https://rmp.hubtel.com/merchantaccount/merchants/{collectionAccount}/receive/mobilemoney", new
  {
      CustomerMsisdn = "233249111411",
      Channel = "mtn-gh",
      Amount = 45.00m,
      Description = "Order #1042",
      ClientReference = "order-1042",
      PrimaryCallbackUrl = "https://example.com/hubtel/callback",
  });
  var payment = await res.Content.ReadFromJsonAsync<JsonElement>();
  ```

**Python**

  ```python
  import os
  import requests

  res = requests.post(
      f"https://rmp.hubtel.com/merchantaccount/merchants/{os.environ['HUBTEL_COLLECTION_ACCOUNT']}/receive/mobilemoney",
      auth=(os.environ["HUBTEL_API_ID"], os.environ["HUBTEL_API_KEY"]),
      json={
          "CustomerMsisdn": "233249111411",
          "Channel": "mtn-gh",
          "Amount": 45.00,
          "Description": "Order #1042",
          "ClientReference": "order-1042",
          "PrimaryCallbackUrl": "https://example.com/hubtel/callback",
      },
      timeout=30,
  )
  payment = res.json()
  ```

Check the HTTP status and response body before saving the result. An accepted request has `ResponseCode` `"0001"`: the payment is pending while the customer approves it on their phone. Save the order as awaiting payment.

Use a new `ClientReference` for every payment (your order ID works) of at most 36 characters. If the request times out, don't send it again: [check the status](/docs/guides/reconciliation) with the same reference first. See [Client references](/docs/conventions#client-reference).

## 2. Receive the callback [#2-receive-the-callback]

Hubtel POSTs the final result to your `PrimaryCallbackUrl`, usually within 30 seconds:

```json
{
  "ResponseCode": "0000",
  "Message": "success",
  "Data": { "Amount": 45, "Charges": 0.88, "AmountAfterCharges": 45, "Description": "The MTN Mobile Money payment has been approved and processed successfully", "ClientReference": "order-1042", "TransactionId": "a1c92e8776b0964b1b2448ba16528876", "OrderId": "a1c92e8776b0964b1b2448ba16528876", "AmountCharged": 45.88 }
}
```

Follow [Handle a callback](/docs/webhooks#handle) to validate and store the result, acknowledge delivery, and fulfil once. Use the documented Receive Money callback address and verify the payment before delivery.

Store the `OrderId` too: you need it to [refund](/docs/guides/refunds) the payment.

## 3. Handle failed payments [#3-handle-failed-payments]

A failed Receive Money callback has `ResponseCode` `2001`, for example when the customer declines or has insufficient funds. Read `Data.Description`, show the customer a clear message, and let them try again with a **new** `ClientReference`. Treat an unexpected code or missing result as unresolved; check the status before starting another payment.

## 4. Reconcile payments without a callback [#4-reconcile-payments-without-a-callback]

Check the status of any payment that has no final result after 5 minutes. This is mandatory:

```bash
curl "https://api-txnstatus.hubtel.com/transactions/$HUBTEL_COLLECTION_ACCOUNT/status?clientReference=order-1042" \
  -u "$HUBTEL_API_ID:$HUBTEL_API_KEY"
```

A `data.status` of `Paid` means you can fulfil the order. [Reconciliation and status checks](/docs/guides/reconciliation) explains how to schedule and process these checks.

## Test the complete flow [#next-steps]

Use the [sandbox scenarios](/docs/testing#test-numbers) to test success, failure and a lost callback. Confirm that a successful payment fulfils once, a failed payment does not fulfil, and a lost callback is resolved by your status check. Then complete the [go-live steps](/docs/get-started/go-live).
