> For the complete documentation index, see [llms.txt](https://docs.alternativepayments.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.alternativepayments.io/web-sdk/payment-flow.md).

# Payment Flow

## Overview

The `createPaymentFlow()` method provides a complete, orchestrated payment experience. It handles navigation between all screens automatically, making it the easiest way to integrate payments.

***

## Flow Screens

The payment flow progresses through these screens:

```
┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│  Invoice Detail │ ──▶ │ Payment Method  │ ──▶ │   Confirmation  │
│                 │     │    Selection    │     │                 │
└─────────────────┘     └─────────────────┘     └─────────────────┘
                                                        │
                                                ┌───────▼─────────┐
                                                │ Payment Result  │
                                                │                 │
                                                └─────────────────┘
```

1. **Invoice Detail** - Displays invoice information and line items
2. **Payment Method Selection** - Choose existing or add new payment method
3. **Add Payment Method** - Card or bank account form (if adding new)
4. **Confirmation** - Review payment details before submitting
5. **Result** - Success or error screen

***

## Basic Usage

```typescript
const flow = client.createPaymentFlow({
  containerId: 'payment-container',
  invoiceId: 'inv_xxx', // Required
  onPaymentSuccess: (payment) => {
    console.log('Payment successful:', payment);
  },
  onPaymentError: (error) => {
    console.error('Payment failed:', error);
  },
  onClose: () => {
    console.log('Flow closed');
  },
});
```

***

## Configuration

### PaymentFlowConfig

<table><thead><tr><th width="165.09765625">Property</th><th width="249.75390625">Type</th><th width="107.11328125">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>containerId</code></td><td><code>string</code></td><td>Yes</td><td>ID of the container element</td></tr><tr><td><code>invoiceId</code></td><td><code>string</code></td><td>Yes</td><td>Invoice ID to pay</td></tr><tr><td><code>theme</code></td><td><code>ThemeConfig</code></td><td>No</td><td>Custom theme configuration</td></tr><tr><td><code>onPaymentSuccess</code></td><td><code>(payment: Payment) => void</code></td><td>No</td><td>Called when payment succeeds</td></tr><tr><td><code>onPaymentError</code></td><td><code>(error: Error) => void</code></td><td>No</td><td>Called when payment fails</td></tr><tr><td><code>onClose</code></td><td><code>() => void</code></td><td>No</td><td>Called when flow is closed</td></tr><tr><td><code>onGoBack</code></td><td><code>() => void</code></td><td>No</td><td>Called when user clicks "Go back" on success screen</td></tr></tbody></table>

***

## Common Use Cases

### Basic Payment Flow

```typescript
const flow = client.createPaymentFlow({
  containerId: 'payment-container',
  invoiceId: 'inv_xxx',
  onPaymentSuccess: (payment) => {
    window.location.href = '/payment-success';
  },
  onGoBack: () => {
    window.location.href = '/dashboard';
  },
});
```

### Handle Payment Results

```typescript
const flow = client.createPaymentFlow({
  containerId: 'payment-container',
  invoiceId: 'inv_xxx',
  onPaymentSuccess: (payment) => {
    // Payment completed successfully
    console.log('Payment ID:', payment.id);
    console.log('Amount:', payment.amount);
    console.log('Status:', payment.status);

    // Redirect to success page
    window.location.href = `/success?payment=${payment.id}`;
  },
  onPaymentError: (error) => {
    // Payment failed
    console.error('Error:', error.message);

    // Show error to user or retry
    showNotification('Payment failed. Please try again.');
  },
  onClose: () => {
    // User closed the flow without completing
    // Navigate back or show message
  },
  onGoBack: () => {
    // User clicked "Go back" on success screen
    window.location.href = '/dashboard';
  },
});
```

***

## Component Instance Methods

The flow returns a `ComponentInstance` with these methods:

### mount()

Mounts the component to the DOM. Called automatically on creation.

```typescript
flow.mount();
```

### unmount()

Removes the component from the DOM. Call this when navigating away or cleaning up.

```typescript
flow.unmount();
```

### refresh()

Refreshes the component data. Useful after external changes.

```typescript
await flow.refresh();
```

***

## Cleanup

Always unmount the flow when the user navigates away to prevent memory leaks:

```typescript
// Vanilla JS
window.addEventListener('beforeunload', () => {
  flow.unmount();
});

// React
useEffect(() => {
  const flow = client.createPaymentFlow({ ... });
  return () => flow.unmount();
}, []);

// Vue
onUnmounted(() => {
  flow.unmount();
});
```

***

## Already Paid Invoices

If an invoice is already paid (status: `RECEIVED`), the flow will skip directly to the success screen, showing the payment details without requiring any user action.

***

## Related

{% content-ref url="/pages/v9Oy98re8K8KPpjh799e" %}
[Components](https://docs.alternativepayments.io/web-sdk/components.md)
{% endcontent-ref %}

{% content-ref url="/pages/HAhpMja1SCRewZLwvu7p" %}
[Theming](https://docs.alternativepayments.io/web-sdk/theming.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.alternativepayments.io/web-sdk/payment-flow.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
