> This page is for Server SDKs.

> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes.

# Payments

> Task pack that takes a card payment the caller agreed to through your payment connector, without the model hearing the card.

[operations]: /docs/server-sdks/reference/python/agents/operations/operations

[service-agent]: /docs/server-sdks/reference/python/agents/operations/service-agent

[refuse]: /docs/server-sdks/reference/python/agents/operations/refuse

[need]: /docs/server-sdks/reference/python/agents/operations/need

[wording]: /docs/server-sdks/reference/python/agents/operations/wording

[record]: /docs/server-sdks/reference/python/agents/operations/record

[add-language]: /docs/server-sdks/reference/python/agents/agent-base/add-language

[swml-pay]: /docs/swml/reference/calling/pay

`Payments` is a task pack that takes a card payment the caller agreed to, and the
model never hears the card. Add it to an [`Operations`][operations] or a
[`ServiceAgent`][service-agent] with `add_to()`.

1. The caller says what they're paying, such as an invoice number.
2. Your `charge` function returns the amount. The amount never comes from the model.
3. The amount is read back for the caller's yes.
4. The platform's [`pay`][swml-pay] verb takes the card on the keypad and charges it
   through your payment connector.
5. The model is told to call the result tool, which reads the outcome the platform
   recorded. A payment that went through is added to the call's [record][record] with
   its money.

In a chat, the payment tool says a payment is taken on a call, and nothing changes.

The pack adds `pay_result` and `pay_payment_results` to the agent's
`swaig_post_swml_vars` param. The card prompts need a language: `language=`, or the
agent's first language from [`add_language()`][add-language]. Without either, the agent
raises `ValueError` when it's set up. A payment also needs a currency, from the
`Charge`, the pack, or the service.

```python
from signalwire.operations import Charge, PaymentResult, Payments
```

## Parameters

All parameters are keyword-only.

**`connector_url`** `str` — required

Your payment connector, which the platform sends the card to. An empty value raises
`ValueError`.

---

**`charge`** `Callable[[CallContext, str], Charge]` — required

`charge(call, paying_for)` returns the `Charge` for what the caller said they're
paying. May be `async`, and may raise [`Refuse`][refuse] (nothing owed, not theirs)
or [`Need`][need]. An amount of 0 or less is refused.

---

**`currency`** `str | None` — default: None

ISO 4217 code of the amounts. Defaults to the service's `currency`.

---

**`language`** `str | None` — default: None

Language code for the platform's card prompts, such as `"en-US"`. Defaults to the
agent's first language.

---

**`voice`** `str | None` — default: None

Voice for the card prompts. Defaults to the voice of the agent's first language.

---

**`security_code`** `bool` — default: True

Whether the card's security code is asked for.

---

**`postal_code`** `bool` — default: True

Whether the billing postal code is asked for.

---

**`max_attempts`** `int` — default: 1

How many times the caller may try the card.

---

**`reusable`** `bool` — default: False

`True` asks for a reusable token the connector can charge again, in place of a
one-time token.

---

**`card_types`** `Sequence[str] | None` — default: None

Card types taken, such as `["visa", "mastercard"]`. `None` takes Visa, Mastercard,
and American Express.

---

**`status_url`** `str | None` — default: None

Where the platform posts each step of the payment.

---

**`connector_parameters`** `Mapping[str, str] | None` — default: None

More name/value pairs sent to the connector.

---

**`on_result`** `Callable[[CallContext, PaymentResult], Any] | None` — default: None

`on_result(call, result)` receives every `PaymentResult`, for your records. May be
`async`.

---

**`before_identity`** `bool` — default: False

`True` to take payments from callers who aren't identified.

---

**`tool`** `str` — default: take_payment

The payment tool's name.

---

**`result_tool`** `str` — default: payment_result

The result tool's name.

---

**`wording`** `Mapping[str, str] | None` — default: None

Replacements keyed like `PAYMENTS_WORDING`. See [wording][wording].

---

**`state_key`** `str` — default: payments

Where in `call.state` the payment under way is kept.

---

## Methods

`add_to(target)` registers the pack's operations on an `Operations` or a
`ServiceAgent`, and returns `target`.

## Charge

A frozen dataclass that `charge` returns.

**`Charge.amount`** `Decimal | int | float | str` — required

How much to charge: more than 0.

---

**`Charge.description`** `str` — required

What it's for, as it's said: "invoice 1042, for March".

---

**`Charge.currency`** `str | None` — default: None

Its ISO 4217 code. Defaults to the pack's, then the service's.

---

## PaymentResult

A frozen dataclass that `on_result` receives.

**`PaymentResult.result`** `str`

The platform's `pay_result`, such as `success`, `too-many-failed-attempts`,
`payment-connector-error`, `caller-interrupted-with-star`, `caller-hung-up`,
`validation-error`, or `internal-error`.

---

**`PaymentResult.amount`** `Decimal`

The amount charged or attempted.

---

**`PaymentResult.currency`** `str`

The amount's ISO 4217 code.

---

**`PaymentResult.description`** `str`

What the payment was for, from the `Charge`.

---

**`PaymentResult.paying_for`** `str`

What the caller said they were paying, as `charge` received it.

---

**`PaymentResult.details`** `Mapping[str, Any]`

The platform's `pay_payment_results`: the token, confirmation code, and masked card,
when there are any.

---

**`PaymentResult.succeeded`** `bool`

`True` when `result` is `success`.

---

## Example

```python
from signalwire.operations import Charge, Payments, ServiceAgent

BALANCES = {"1042": "54.04"}

agent = ServiceAgent("billing", role="You take payments for a water utility.", currency="USD")
agent.add_language("English", "en-US", "rime.spore")
Payments(
    connector_url="https://pay.example.com/connector",
    charge=lambda call, invoice: Charge(BALANCES[invoice], f"invoice {invoice}"),
).add_to(agent)
```