Skip to navigation

Payments

View as MarkdownOpen in Claude

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 or a ServiceAgent 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 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 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(). 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.

from signalwire.operations import Charge, PaymentResult, Payments

Parameters

All parameters are keyword-only.

connector_url
strRequired

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 (nothing owed, not theirs) or Need. An amount of 0 or less is refused.

currency
str | NoneDefaults to None

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

language
str | NoneDefaults to None

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

voice
str | NoneDefaults to None

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

security_code
boolDefaults to True

Whether the card’s security code is asked for.

postal_code
boolDefaults to True

Whether the billing postal code is asked for.

max_attempts
intDefaults to 1

How many times the caller may try the card.

reusable
boolDefaults to False

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

card_types
Sequence[str] | NoneDefaults to None

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

status_url
str | NoneDefaults to None

Where the platform posts each step of the payment.

connector_parameters
Mapping[str, str] | NoneDefaults to None

More name/value pairs sent to the connector.

on_result
Callable[[CallContext, PaymentResult], Any] | NoneDefaults to None

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

before_identity
boolDefaults to False

True to take payments from callers who aren’t identified.

tool
strDefaults to take_payment

The payment tool’s name.

result_tool
strDefaults to payment_result

The result tool’s name.

wording
Mapping[str, str] | NoneDefaults to None

Replacements keyed like PAYMENTS_WORDING. See wording.

state_key
strDefaults to 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 | strRequired

How much to charge: more than 0.

Charge.description
strRequired

What it’s for, as it’s said: “invoice 1042, for March”.

Charge.currency
str | NoneDefaults to 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

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)