# Generating DirectBilling Transactions

To start the payment process at the GSM operator, you need to generate a new transaction by querying our API. In response, you will receive a dedicated link (`redirectUrl`) to which you should redirect the customer so they can enter their phone number and confirm the account charge.

## Endpoint and Authorization

**Endpoint:** `POST /directbilling/{serviceId}/transactions`
**Version:** `1.0.0`
**Authorization:** Bearer Token (requires `Authorization: Bearer YOUR_API_KEY` header)

### Path Parameters

| Parameter | Type | Required | Description |
|  --- | --- | --- | --- |
| `serviceId` | `string` | **YES** | Your DirectBilling service identifier (e.g., `e14f8074`). Parameter passed directly in the URL. |


## 🌟 Unique Feature: `amountType` (Earn as Much as You Want!)

Typically, payment gateways force you to provide a fixed gross amount that the customer must pay. In the case of DirectBilling payments, where operator commissions can be complicated, calculating the correct customer price can be troublesome.

We solved this problem by introducing the unique **`amountType`** parameter! It allows you to define *what* the amount (`amount`) you're providing actually represents.

This way, you can send us a request saying: *"I want to earn 10 PLN net, calculate for me how much the customer needs to pay"*.

Available `amountType` variants:

* **`gross`** (default) – the amount you provide is the final gross amount the customer will pay. Your profit will be calculated after deducting commissions.
* **`net`** – you provide the net amount.
* **`required`** – **absolute hit!** You provide your final commission amount (how much you want to earn "net"). Our system will check the operator commission tables and based on them, charge the customer an appropriate, higher gross amount to guarantee your expected profit.


## Request Body

The request should be sent in JSON format (`Content-Type: application/json`).

### Example Payload

```json
{
  "amount": 10.00,
  "amountType": "required",
  "control": "VIP_Order_123",
  "returns": {
    "complete": "https://yourstore.com/success",
    "failure": "https://yourstore.com/error"
  }
}
```

*(In the above example, the store tells SimPay: "Generate a transaction from which I want to receive exactly 10.00 PLN into my wallet. Calculate the gross amount yourselves").*

## Success Response (HTTP 200 OK)

When the request is valid, the system will create a transaction and return a redirect address for the customer.

```json
{
  "success": true,
  "data": {
    "transactionId": "1d87a1b3-18f8-4146-bcb1-c0c9f293b04f",
    "redirectUrl": "https://db.simpay.pl/1d87a1b3-18f8-4146-bcb1-c0c9f293b04f"
  }
}
```

Tip
Save the `data.transactionId` value in your database and link it with the customer's cart. This is the ID by which you will identify the transaction when you receive an asynchronous **[IPN notification](/notifications/directbilling)** about its payment in the future.

## Error Codes

If incorrect data is provided or there are authentication issues, the API will return one of the following HTTP codes along with an appropriate `errorCode` in JSON format:

* **HTTP 401 (Unauthorized)** – missing token header or the API key is invalid (`errorCode`: `UNAUTHORIZED`).
* **HTTP 403 (Forbidden)** – your token does not have permissions for this action or you are making a request from an unauthorized IP address (`errorCode`: `INVALID_ABILITY_PROVIDED` or `IP_ADDRESS_NOT_WHITELISTED`).
* **HTTP 404 (Not Found)** – the specified service was not found or invalid endpoint address (`errorCode`: `SERVICE_NOT_FOUND`, `ROUTE_NOT_FOUND`).
* **HTTP 422 (Unprocessable Entity)** – validation error of the provided data (e.g., missing required amount). The response will contain an `errors` object with details.


**Example error 422:**

```json
{
  "success": false,
  "errorCode": "VALIDATION_ERROR",
  "errors": {
    "amount": [
      "The amount field is required."
    ]
  }
}
```