Skip to main content
Execute a one-step payment charge that immediately captures funds from the customer’s payment method. The charge is routed to the appropriate payment provider based on your configured routing rules.

Method signature

async charge(request: ChargeRequest): Promise<PaymentResult>

Parameters

body.amount
number
required
Amount in the smallest currency unit (e.g., cents for USD, centavos for BRL).
body.currency
string
required
ISO 4217 currency code (e.g., "USD", "BRL", "EUR").
body.paymentMethod
PaymentMethodInput
required
Payment method information. Can be a tokenized card, raw card data, bank transfer, wallet, PIX, or boleto.
body.customer
CustomerInput
Customer billing and contact information.
body.description
string
Human-readable description of the charge.
body.metadata
object
Arbitrary key-value pairs forwarded to the payment provider.
body.idempotencyKey
string
Unique key to prevent duplicate charges when retrying the same request. If the same key is used with different request data, a VaultIdempotencyConflictError will be thrown.
body.routing
RoutingPreference
Per-request routing overrides.

Returns

PaymentResult
object
Normalized payment result containing transaction details and routing information.

Error handling

The charge() method may throw the following errors:
Thrown when an idempotency key is reused with different request data.
// src/client/vault-client.ts:612-619
if (existingRecord.payloadHash !== payloadHash) {
  throw new VaultIdempotencyConflictError(
    'Idempotency key was reused with a different payload.',
    { operation, key }
  );
}
Thrown when no eligible provider is found or when routing configuration is invalid.
// src/client/vault-client.ts:428-430
throw new VaultRoutingError(
  'No eligible provider found after exclusions.'
);
Provider errors are caught and wrapped in VaultError instances with provider context. See src/client/vault-client.ts:636-652.

Example

const result = await client.charge({
  amount: 5000,
  currency: 'USD',
  paymentMethod: {
    type: 'card',
    token: 'tok_visa'
  },
  customer: {
    email: '[email protected]',
    name: 'John Doe'
  },
  description: 'Order #1234',
  idempotencyKey: 'order-1234'
});

console.log(result.status); // 'completed'
console.log(result.provider); // 'stripe'
console.log(result.id); // 'txn_abc123'

Implementation details

The charge operation:
  1. Checks for idempotency key and returns cached result if the same request was made previously
  2. Resolves the appropriate payment provider using routing rules (src/client/vault-client.ts:375-437)
  3. Executes the charge through the selected provider adapter
  4. Normalizes the provider response into a standard PaymentResult format
  5. Records the transaction in the provider index for future lookups
  6. Queues transaction report to the platform for analytics (if configured)
The charge method supports automatic idempotency to safely retry failed requests. Store the id from the response to reference this transaction in future operations like refunds.

Build docs developers (and LLMs) love