# Direct API

> Charge a payment from your server, on a channel you chose, with no Wajub page.

With the Direct API you render every screen: your server charges the payment on the channel you
picked, and Wajub tells you the outcome. It takes two calls, `POST /payments` to record the payment
and `POST /payments/{id}` to charge it.

> [!CAUTION]
> **This call belongs on your server**
>
> Send it with your secret key, from your backend. Never use an `sk.…` key from a browser: Wajub
> rejects it with a `403`. If your payment screen lives in a browser, use
> [Embedded Components](https://docs.wajub.com/libraries/components) instead.

## 1. Create the payment

Create the payment as the [Payments Quickstart](https://docs.wajub.com/payments/quickstart#1-create-the-payment) shows.
Keep `transaction.id` from the answer. You will not use `authorization_url`, which is for the hosted
page. Keep `authorization_token` as well if you charge a channel whose provider asks the payer for a
code: it is the credential that [confirms that step](#a-code-or-another-detail), and it is returned
at creation only.

The payment is `pending`, and nobody has been charged yet.

## 2. Charge it

`POST /payments/{id}`

- `channel` (string, required): Where to charge: a channel slug from `GET /channels`, such as `cm.mtn` or `cm.orange`. Send `mobile`, or `cm.mobile` for Cameroon, to let Wajub find the operator from the phone number. These two are the only aggregates.
- `data.phone` (string): The Mobile Money number to charge. International format is safest; a local number is read in the country of the channel.
- `data.operator` (string): With `cm.mobile` only: the operator the payer picked, such as `orange`. Used when the number alone does not tell Wajub which operator it belongs to, a ported number for instance.
- `data.otp` (string): The code the payer generated in their operator's menu, for a channel that answered `payer_otp`: digits only. See [A code the payer generates](#a-code-the-payer-generates).
- `data.iban` (string): For a bank channel. `data.account_number` is accepted instead.
- `data.wallet_address` (string): For a crypto channel.
- `data.save_payment_method` (boolean): Save this payment method on the customer, to offer it again next time. Only with the payer's consent. Defaults to `false`.

Every field under `data` is also accepted at the root of the body. Send an `Idempotency-Key` header
unique to this charge, so that a retry after a timeout never charges twice.

**Charge a payment on MTN Mobile Money**

```bash
curl https://api.wajub.com/payments/trx_test_CSUGajfv9xh0XQ5wu2lx \
  -H "Authorization: sk_test.xxxxxxxxxxxxxxxx…" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: charge-order-4172-1" \
  -d '{
    "channel": "cm.mtn",
    "data": { "phone": "+237670000000" }
  }'
```

```javascript
const payment = await wajub.payments.process(
  'trx_test_CSUGajfv9xh0XQ5wu2lx',
  {
    channel: 'cm.mtn',
    data: { phone: '+237670000000' },
  },
  { idempotencyKey: 'charge-order-4172-1' },
);

console.log(payment.status);
```

```python
from wajub import RequestOptions

payment = wajub.payments.process(
    "trx_test_CSUGajfv9xh0XQ5wu2lx",
    {
        "channel": "cm.mtn",
        "data": {"phone": "+237670000000"},
    },
    RequestOptions(idempotency_key="charge-order-4172-1"),
)

print(payment.status)
```

```php
use Wajub\RequestOptions;

$payment = $wajub->payments->process('trx_test_CSUGajfv9xh0XQ5wu2lx', [
  'channel' => 'cm.mtn',
  'data' => [
    'phone' => '+237670000000',
  ],
], new RequestOptions(idempotencyKey: 'charge-order-4172-1'));

echo $payment->status;
```

```go
payment, err := client.Payments.Process(context.Background(), "trx_test_CSUGajfv9xh0XQ5wu2lx", map[string]any{
	"channel": "cm.mtn",
	"data": map[string]any{"phone": "+237670000000"},
}, &wajub.RequestOptions{IdempotencyKey: "charge-order-4172-1"})

if err != nil {
	log.Fatal(err)
}

log.Println(payment.Status)
```

## The answer

For Mobile Money the answer is almost always `202 Accepted`. The prompt is on the payer's handset,
the payment has moved to `processing`, and `message` is the line to show while you wait:

```json title="Response · 202 Accepted"
{
  "code": 202,
  "status": "Accepted",
  "message": "Confirm the transaction on your phone.",
  "transaction": {
    "id": "trx_test_CSUGajfv9xh0XQ5wu2lx",
    "reference": "order-4172",
    "amount": 5000,
    "currency": "XAF",
    "status": "processing",
    "channel": "cm.mtn",
    "sandbox": true
  }
}
```

The outcome arrives later, as a `payment.succeeded` or `payment.failed` webhook. Before you fulfil
the order, [retrieve the payment](https://docs.wajub.com/payments/quickstart#3-verify-before-you-fulfil) and read its
`status`, as in every other mode.

| Code  | What it means                                                                                                                                                                                                                                                      | What to do                                                                                                                                                                         |
| ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `200` | The channel confirmed at once. The payment is `succeeded`                                                                                                                                                                                                          | Verify, then fulfil                                                                                                                                                                |
| `202` | Waiting for the payer, or for a next step named in `action`                                                                                                                                                                                                        | Show `message`, complete the step in `action` if there is one, wait for the webhook                                                                                                |
| `400` | The payment can no longer be charged: it already succeeded, expired or was cancelled, or, in live, its `expires_at` has passed                                                                                                                                     | Create a new payment if needed                                                                                                                                                     |
| `402` | The operator or the card issuer declined. The payment is back to `pending`. Also `error_code: merchant_not_verified` while your account's verification is pending, and `otp_attempts_exceeded` once the operator refused three of the payer's codes on the channel | Let the payer try again, see below. For `merchant_not_verified`, finish your verification                                                                                          |
| `403` | Refused by a restriction on your account, or by [Shield](https://docs.wajub.com/risk/shield). A Shield block carries `error_code: shield_blocked` and its `BLK-…` reference in `block_reference`                                                                                         | Do not retry the same attempt                                                                                                                                                      |
| `404` | No payment with this id, in this environment                                                                                                                                                                                                                       | Check the id and the key                                                                                                                                                           |
| `409` | A charge is already in progress on this payment (`error_code: payment_in_progress`), a call with the same `Idempotency-Key` is still running, or the payment was prepared and its order not validated yet                                                          | Wait for the outcome of the attempt in progress, by webhook or by retrieving the payment. Charge again once it is back to `pending`, or `processing` with no charge left under way |
| `422` | The channel is not accepted, the number does not match the operator, the amount is above the channel's ceiling, or the `Idempotency-Key` was already used for a different request                                                                                  | Fix the request. A reused key needs a new key for a new charge                                                                                                                     |

A `402` carries `error_code`, a stable reason such as `insufficient_funds` that you can translate for
the payer, and `limit_exceeded` when the payment would go past one of your account's limits.

## When `action` is set

Most Mobile Money charges need nothing more from you: `action` is absent and you wait for the
webhook. When the provider behind the channel wants the payer to do something first, the answer is
still a `202`, `action` names that step, and the answer carries what the step needs.

| `action`                                                              | What the payer does                                             | What the answer carries                                                                                                 |
| --------------------------------------------------------------------- | --------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `redirect`                                                            | Approves the payment on the provider's page or in its app       | `action_url`, where to send the payer                                                                                   |
| `confirm_otp`                                                         | Types the code the provider sent them                           | `otp_id`, and `otp_masked_contact`, where the code went, masked, or empty when the provider does not say                |
| `payer_otp`                                                           | Generates a code in their operator's menu, then gives it to you | `payer_otp`, how to get the code. Charge again with it in `data.otp`                                                    |
| `confirm_pin`, `confirm_phone`, `confirm_birthday`, `confirm_address` | Gives the provider one more detail                              | `pin_id` and `pin_instruction`, or the same pair named after `phone`, `birthday` or `address`                           |
| `client_session`                                                      | Pays in a window the provider runs itself                       | `client_session`, with its `id` and, when the provider has a page of its own, `hosted_url`                              |
| `confirm`                                                             | Follows the instruction in `message`                            | Nothing more in live. In sandbox, a crypto channel adds `crypto.deposit`                                                |
| `confirm_3ds`                                                         | Authenticates with the card issuer                              | Nothing for you to handle: that step happens in the payment page or the components, one more reason to take cards there |

The step can differ from one payment to the next on the same channel, so read `action` on every
`202` instead of assuming one per channel.

### A redirect

Wave works this way: the payer approves in the Wave app, not on a prompt. So do the crypto channels
in live, where the provider's page shows the address and the amount to send.

```json title="Response · 202 Accepted, with a redirect"
{
  "code": 202,
  "status": "Accepted",
  "message": "Open Wave to confirm your payment.",
  "action": "redirect",
  "transaction": {
    "id": "trx_CSUGajfv9xh0XQ5wu2lx",
    "amount": 5000,
    "currency": "XOF",
    "status": "processing",
    "channel": "ci.wave"
  },
  "action_url": "https://pay.wave.com/c/cos-0example00"
}
```

Send the payer to `action_url`: a redirect in a browser, a link or a button in an app. When they are
done, the provider brings them back to the `callback` of the payment, if you set one. The outcome
still arrives by webhook, and you still verify the payment before you fulfil.

### A code, or another detail

With `confirm_otp` the provider has sent the payer a code. Collect it on your own screen, then send
it from your server to [`POST /payments/{id}/confirm`](https://docs.wajub.com/api/payments/confirm), with your secret key,
the `otp_id` of the answer and the code.

```bash title="Confirm the code the payer received"
curl https://api.wajub.com/payments/trx_CSUGajfv9xh0XQ5wu2lx/confirm \
  -H "Authorization: sk.xxxxxxxxxxxxxxxx…" \
  -H "Idempotency-Key: confirm-order-1042" \
  -H "Content-Type: application/json" \
  -d '{
    "otp_id": "prc_2b4l4EObTOnOa501JXNi",
    "code": "482913"
  }'
```

The answer is the charge's. `200` when the payment is paid. `202` when the provider asks for one more
step: `action` names it, with what it needs, as on the charge. `402` when the provider refused what
was sent: `error_code` says why (`invalid_otp`, or `max_attempts_exceeded` once the three tries of
the step are used up) and `attempts_remaining` counts the tries left. With none left, charge the
payment again. Send an `Idempotency-Key`: a retried call then answers what the first one did,
without sending the code to the provider twice.

The four other steps take the same call, with their own fields. Each `…_instruction` field of the
charge's answer is the provider's own wording of what it asks for.

| `action`           | Body of `POST /payments/{id}/confirm`                |
| ------------------ | ---------------------------------------------------- |
| `confirm_otp`      | `otp_id`, `code`                                     |
| `confirm_pin`      | `pin_id`, `pin`                                      |
| `confirm_phone`    | `phone_id`, `phone`                                  |
| `confirm_birthday` | `birthday_id`, `birthday` as `YYYY-MM-DD`            |
| `confirm_address`  | `address_id`, `address`, `city`, `state`, `zip_code` |

Each step allows three tries, whether it is confirmed from your integration or from the payment
page.

### A code the payer generates

A few operators, such as Orange Money in Burkina Faso, take a payment only with a code the payer
generates in their operator's menu first. Without it, or with a code the operator refuses, nothing
is charged: the answer is a `202` with `action: payer_otp`, the payment stays `pending`, and
`payer_otp` says how to get the code: `ussd_code` to dial, `amount` and `currency`, the exact amount
to type, `otp_length`, the steps in `steps.fr` and `steps.en`, `reason` (`required`, `invalid` or
`refused`) and `attempts_remaining`. `ussd_code` and `otp_length` may be `null`, and each step is an
object whose `text` is what to show. With `invalid` or `refused`, the answer also carries
`error_code: invalid_otp`. Show the steps and `payer_message`, collect the
code, then charge the payment again on the same channel with the code in `data.otp` and a new
`Idempotency-Key`. Once the operator has refused three codes on that channel, the charge answers
`402` with `error_code: otp_attempts_exceeded`: the payer pays with another channel.

### The provider's own window

With `client_session` the provider collects the payment itself. When `client_session.hosted_url` is
present, send the payer to it as you would for a redirect. Once they are back, call
`POST /payments/{id}/confirm` with `client_session_id` set to `client_session.id`. It answers as the
charge does, `202` while the outcome is not final yet. A provider that only offers a widget inside the
page, with no `hosted_url`, is meant for [Embedded Components](https://docs.wajub.com/libraries/components), which open it
for you.

## Letting Wajub find the operator

With `channel` set to `mobile`, or to `cm.mobile` for Cameroon, Wajub reads the operator from the
number and charges on it: `+23767…` goes to `cm.mtn`. These two are the only aggregates: another
country is charged on its operator's own channel, such as `ci.orange`. When the number cannot be
attributed, a ported one for instance, Wajub uses `data.operator` if you sent it with `cm.mobile`,
and answers `422` otherwise.

With a precise channel such as `cm.mtn`, the number has to belong to that operator. An Orange number
sent on `cm.mtn` is refused with a `422` that names the right channel, instead of being charged on
the wrong network.

Some channels are named after a wallet, not after an operator. Wave (`ci.wave`, `sn.wave`), Express
Union (`cm.eu`), Djamo (`ci.djamo`, `sn.djamo`), Capitec Pay (`za.capitec`), OPay (`ng.opay`) and
PalmPay (`ng.palmpay`) are held whatever the payer's operator, so any number is accepted on them. The other wallets belong to one operator, and
take a number of that operator: `ke.mpesa` a Safaricom number, `tz.mpesa` and `mz.mpesa` a Vodacom
one, `tg.tmoney` a Togocel one, `gh.vodafone` a Telecel one. An aggregate sends such a number to the
wallet. [Phone numbers](https://docs.wajub.com/payments/payment-methods/phone-numbers#how-the-operator-is-identified) lists every wallet
and the operator behind it.

On a payment created with [`accept`](https://docs.wajub.com/payments/payment-methods#choosing-what-a-payment-accepts),
an aggregate is judged by the operator the number resolves to: `cm.mobile` with an MTN number is
accepted wherever `cm.mtn` is.

## Retrying after a decline

A `402` leaves the payment `pending`, so the same payment can be charged again: with another number,
another operator, or the same one once the payer has topped up. Use a **new** `Idempotency-Key` for
that new attempt. Sending the first key again returns the first answer, the `402`, without trying
again.

The same key is still the right one after a timeout or a network error, when you do not know whether
the first call went through.

## Cards

Live refuses raw card data: a card number or a security code in the body is answered with a `422`
and `error_code: raw_card_data_not_accepted`, before anything is charged. Cards are collected by
[Hosted Checkout](https://docs.wajub.com/payments/hosted-checkout), [Embedded Components](https://docs.wajub.com/libraries/components) or a
mobile SDK, which collect them securely.

Sandbox accepts `channel: card` with the published test cards only, so you can exercise the whole
flow. Any other card number is refused there too. A sandbox card charge takes four fields in
`data`, each answered with a `422` on its name when it is missing or malformed:

| Field         | What it holds                                                                                |
| ------------- | -------------------------------------------------------------------------------------------- |
| `card.number` | A [test card](https://docs.wajub.com/developer-tools/testing/test-scenarios#cards). The number decides the outcome |
| `card.holder` | The name on the card. `card_holder` is accepted too                                          |
| `exp`         | The expiry as `MM/YY` or `MM/YYYY`, a year from this one to thirty years ahead               |
| `cvc`         | Three or four digits                                                                         |

The two `card.` keys are names with a dot in them, not a nested object: `"data": {"card.number":
"4242424242424242", "card.holder": "Amina Diallo", "exp": "12/30", "cvc": "123"}`.

## In sandbox

The last six digits of the phone number decide the outcome, as on the hosted page. `…000000`
succeeds; `…000001` is declined at once as live declines: a `402` with `error_code:
insufficient_funds`, and the payment stays `pending`.
The full table is in [Test scenarios](https://docs.wajub.com/developer-tools/testing/test-scenarios).

Sandbox never answers `redirect`, `confirm_otp` or `client_session`: those steps only happen in
live. It answers Mobile Money and bank channels with no `action`, a 3-D Secure test card with
`confirm_3ds`, and a crypto channel with `confirm` and `crypto.deposit`, the address and the amount
to send. Orange Money in Burkina Faso (`bf.orange`) asks for the payer's code with `payer_otp`, as
in live, and accepts `123456`.

The Direct API is available on every plan, in sandbox and in live. Splitting a payment into
instalments, with [`POST /payments/{id}/splits`](https://docs.wajub.com/api/payments/splits), works in both, on Mobile
Money channels in live: [Split payments](https://docs.wajub.com/sync/split-payments) describes it.

- [Choose your integration](https://docs.wajub.com/payments/integration-paths): The four modes compared, and when the Direct API is the right one.
- [Phone numbers](https://docs.wajub.com/payments/payment-methods/phone-numbers): The formats a number can take, and how the operator is found.
- [Payment lifecycle](https://docs.wajub.com/payments/lifecycle): Every status a payment goes through, and the webhook for each.
- [API reference](https://docs.wajub.com/api/payments/process): Every field of POST /payments/{id}.
