# Billing Exports

> Create and cancel invoices or manage balances in Sky Phone's built-in billing ledger from server Lua.

[All exports](/docs/scripts/free-phone/exports) · [Finance integrations](https://github.com/sky-systems/sky_phone/blob/1.6.1/sky_phone/integrations/FINANCE.md)

Call these exports from server Lua. They operate on Sky Phone's built-in invoices and ledger, including when the app is configured to display an external provider. Your calling resource must authorize the action and decide which invoice or account the player can access.

## Billing Exports

### `CreateInvoice(data)`

- **Purpose**: Creates a validated invoice for an online player or known character identifier.
- **Arguments**:

  - `data` (`table`): Invoice definition.
  
    - `recipientSource` (`number?`): Online recipient server ID.
    - `recipientIdentifier` (`string?`): Known character identifier when no source is supplied.
    - `issuerSource` (`number?`): Online issuer server ID.
    - `issuerIdentifier` (`string?`): Known issuer identifier.
    - `issuerAccount` (`string`): Account receiving paid funds.
    - `issuerLabel` (`string`): Display name shown to the recipient.
    - `title` (`string`): Invoice title.
    - `description` (`string?`): Invoice details.
    - `amount` (`number`): Server-validated invoice amount.
    - `dueDays` (`number?`): Optional payment period.
- **Returns**: `string | nil, string?` — invoice ID or `nil` and an error code.

```lua [server.lua]
local invoice_id, error_code = exports["sky_phone"]:CreateInvoice({
    recipientSource = target_source,
    issuerSource = source,
    issuerAccount = "ambulance",
    issuerLabel = "Los Santos Medical",
    title = "Medical treatment",
    description = "Emergency treatment and medication.",
    amount = 1300,
    dueDays = 7,
})

if not invoice_id then
    print(("[medical] Invoice failed: %s"):format(error_code))
    return
end

print(("[medical] Created invoice %s"):format(invoice_id))
```

Never accept an invoice amount from an untrusted client without validating the service and price on
the server.

### `CancelInvoice(invoiceId, actorIdentifier)`

- **Purpose**: Cancels an open invoice.
- **Arguments**:

  - `invoiceId` (`string`): Invoice ID returned by `CreateInvoice`.
  - `actorIdentifier` (`string?`): Optional character or integration identifier for the audit record. Check the actor's permission in your calling resource.
- **Returns**: `boolean`.

```lua [server.lua]
local cancelled = exports["sky_phone"]:CancelInvoice(
    invoice_id,
    issuer_identifier
)

if not cancelled then
    print("Invoice could not be cancelled")
end
```

### `GetBillingAccountBalance(accountKey)`

- **Purpose**: Reads the current internal balance for a billing account.
- **Arguments**:

  - `accountKey` (`string`): Account key such as `ambulance`.
- **Returns**: `number | nil`.

```lua [server.lua]
local balance = exports["sky_phone"]:GetBillingAccountBalance("ambulance") or 0
print(("Medical billing balance: $%s"):format(balance))
```

### `RemoveBillingAccountBalance(accountKey, amount)`

- **Purpose**: Atomically withdraws an amount when sufficient balance is available.
- **Arguments**:

  - `accountKey` (`string`): Billing account key.
  - `amount` (`number`): Positive server-calculated withdrawal amount.
- **Returns**: `boolean`.

```lua [server.lua]
local removed = exports["sky_phone"]:RemoveBillingAccountBalance(
    "ambulance",
    5000
)

if not removed then
    print("Insufficient billing balance or invalid request")
end
```
