> ## Documentation Index
> Fetch the complete documentation index at: https://primer.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PrimerCheckoutProvider

> Root component that starts a checkout session from a client token and shares it with the components and hooks below it.

> [!WARNING]
>
> Primer Checkout React Native SDK is currently in **beta** (v3.0.0-beta.1).
> The API is subject to change before the stable release.

`PrimerCheckoutProvider` starts the native SDK with your client token and makes the checkout session available to every Primer component and hook below it. It also applies your theme and calls your callbacks. It renders only its `children` and draws no UI of its own.

## Props

```tsx theme={"dark"}
interface PrimerCheckoutProviderProps {
  clientToken: string;
  settings?: PrimerSettings;
  theme?: PrimerThemeOverride;
  onCheckoutComplete?: (checkoutData: CheckoutData) => void;
  onError?: (error: PrimerError, checkoutData: CheckoutData | null) => void;
  onBeforePaymentCreate?: (
    checkoutPaymentMethodData: CheckoutPaymentMethodData,
    handler: PaymentCreationHandler
  ) => void;
  children?: ReactNode;
}
```

| Prop | Type | Default | Description |
| - | - | - | - |
| `clientToken` | `string` | Required | The client token from your backend. See [Client session](https://primer.io/docs/checkout/client-session.md). The provider starts the session when it mounts. A new value ends the current session and starts a new one. |
| `settings` | [`PrimerSettings`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-settings) | — | SDK configuration. |
| `theme` | [`PrimerThemeOverride`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/theming/primer-theme-override) | — | Design token overrides for light and dark mode. Changes apply on the next render. |
| `onCheckoutComplete` | `(checkoutData: CheckoutData) => void` | — | Called with the payment result when the checkout completes. See [onCheckoutComplete](#oncheckoutcomplete). |
| `onError` | `(error: PrimerError, checkoutData: CheckoutData \| null) => void` | — | Called with payment errors. See [onError](#onerror). |
| `onBeforePaymentCreate` | `(checkoutPaymentMethodData: CheckoutPaymentMethodData, handler: PaymentCreationHandler) => void` | — | Called before the payment is created, so you can continue or stop it. See [onBeforePaymentCreate](#onbeforepaymentcreate). |
| `children` | `ReactNode` | — | Your checkout UI: `PrimerCheckoutSheet`, or your own layout built from Primer components and hooks. |

`PrimerCheckoutProviderProps`, `CheckoutData`, `CheckoutPaymentMethodData`, `PaymentCreationHandler`, `PrimerSettings`, `PrimerThemeOverride` and `PrimerError` are exported from `@primer-io/react-native` as types.

## Results and callbacks

> [!TIP]
>
> Read the result of each payment from [`usePrimerCheckout().paymentOutcome`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout). It reports the success, pending and failure results.

### onCheckoutComplete

Called when the checkout completed and a payment was created. `checkoutData.payment` has the payment `id` and `orderId`.

### onError

Called with the [`PrimerError`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-error) of a payment error, for example a declined payment, a 3DS failure or a network error. The second argument carries the payment data when the native SDK sends it. Read it with optional chaining.

Read errors that stop the checkout from starting from [`usePrimerCheckout().error`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout#returns).

### onBeforePaymentCreate

Called before the native SDK creates the payment. `checkoutPaymentMethodData.paymentMethod` is the payment method type. Answer through the handler:

| Method | Description |
| - | - |
| `handler.continuePaymentCreation()` | Creates the payment. |
| `handler.abortPaymentCreation(errorMessage)` | Stops the payment. `errorMessage` is a `string` or `null`. |

```tsx theme={"dark"}
<PrimerCheckoutProvider
  clientToken={clientToken}
  onBeforePaymentCreate={(data, handler) => {
    if (isCartStillValid()) {
      handler.continuePaymentCreation();
    } else {
      handler.abortPaymentCreation('The cart changed. Review it and try again.');
    }
  }}
>
  {children}
</PrimerCheckoutProvider>
```

### Callbacks in settings

`settings.headlessUniversalCheckoutCallbacks` accepts more callbacks:

| Callback | Arguments |
| - | - |
| `onAvailablePaymentMethodsLoad` | `(paymentMethods: PaymentMethod[])` |
| `onClientSessionUpdate` | `(clientSession: ClientSession)` |
| `onCheckoutComplete` | `(checkoutData: CheckoutData)` |
| `onError` | `(error: PrimerError, checkoutData: CheckoutData \| null)` |
| `onCheckoutAdditionalInfo` | `(additionalInfo: CheckoutAdditionalInfo)` |
| `onBeforePaymentCreate` | `(checkoutPaymentMethodData, handler)` |
| `onTokenizationStart` | `(paymentMethodType: string)` |
| `onPreparationStart` | `(paymentMethodType: string)` |
| `onPaymentMethodShow` | `(paymentMethodType: string)` |
| `onBeforeClientSessionUpdate` | `()` |
| `onCheckoutPending` | `(additionalInfo: CheckoutAdditionalInfo)` |
| `onCheckoutResume` | `(resumeToken: string, handler)` |

Without an `onCheckoutAdditionalInfo` callback, additional info that Primer Checkout doesn't handle itself sets `paymentOutcome` to an error with the `errorId` `'additional-info-unhandled'` once the checkout is ready.

## Session lifecycle

The `clientToken` prop controls the session:

| Event | What happens |
| - | - |
| Mount | The provider starts the native SDK with `clientToken` and `settings`. `usePrimerCheckout().isReady` becomes `true` once the payment methods have loaded. |
| New `clientToken` | The provider ends the current session and starts a new one. All checkout state resets: `isReady` goes back to `false` and `paymentOutcome` to `null`. |
| Re-render with the same `clientToken` | Nothing restarts. |
| Unmount | The provider ends the session and removes its native listeners. Results that arrive afterwards don't reach your app. |

To reload the session, for example after your backend updates the client session, pass a new client token.

### Theme updates

The provider merges `theme.light` and `theme.dark` over the default tokens and applies the result on the next render. Define the theme outside your component or memoize it, so the provider doesn't merge it again on every render.

## Placement

Put `PrimerCheckoutProvider` above every Primer component and hook that reads the checkout session. Outside it, `usePrimerCheckout` throws:

```text theme={"dark"}
usePrimerCheckout must be used within a <PrimerCheckoutProvider>. Wrap your component tree with <PrimerCheckoutProvider clientToken="...">.
```

The components and hooks that need the provider include `PrimerCheckoutSheet`, `PrimerCardFormProvider`, `PrimerPaymentMethodList`, `PrimerAcceptedCardNetworks`, `PrimerCardNetworkSelector`, `usePrimerPaymentMethod`, `usePrimerPaymentMethods` and `usePrimerVaultManager`. `usePrimerTheme` and `usePrimerLocalization` also work outside the provider.

## Usage

### With the prebuilt sheet

```tsx theme={"dark"}
import { useState } from 'react';
import { Button } from 'react-native';
import { PrimerCheckoutProvider, PrimerCheckoutSheet } from '@primer-io/react-native';
import type { PrimerSettings, PrimerThemeOverride } from '@primer-io/react-native';
import { fetchClientToken } from './api';

const settings: PrimerSettings = {
  uiOptions: { appearanceMode: 'SYSTEM' },
};

const theme: PrimerThemeOverride = {
  light: { colors: { brand: '#2F6FED' } },
  dark: { colors: { brand: '#6B9BFF' } },
};

export function CheckoutButton() {
  const [clientToken, setClientToken] = useState<string | null>(null);
  const [visible, setVisible] = useState(false);

  const openCheckout = async () => {
    setClientToken(await fetchClientToken());
    setVisible(true);
  };

  return (
    <>
      <Button title="Checkout" onPress={openCheckout} />
      {clientToken !== null && (
        <PrimerCheckoutProvider clientToken={clientToken} settings={settings} theme={theme}>
          <PrimerCheckoutSheet
            visible={visible}
            onRequestDismiss={() => setVisible(false)}
            onDismiss={() => setClientToken(null)}
          />
        </PrimerCheckoutProvider>
      )}
    </>
  );
}
```

This example creates a new client token, and so a new session, for each checkout attempt.

### Logging errors

```tsx theme={"dark"}
<PrimerCheckoutProvider
  clientToken={clientToken}
  onError={(error, checkoutData) => {
    logCheckoutError({
      errorId: error.errorId,
      diagnosticsId: error.diagnosticsId,
      paymentId: checkoutData?.payment?.id,
    });
  }}
>
  {children}
</PrimerCheckoutProvider>
```

Watch `paymentOutcome` as well, as shown on the [`usePrimerCheckout`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout#usage) page.

## See also

> [card] [usePrimerCheckout](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout.md)
>
> Read the checkout state and the payment outcome.

> [card] [PrimerCheckoutSheet](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/primer-checkout-sheet.md)
>
> Run the whole checkout in a prebuilt bottom sheet.

> [card] [PrimerSettings](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-settings.md)
>
> Configure payment methods and UI options.

> [card] [PrimerThemeOverride](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/theming/primer-theme-override.md)
>
> Override design tokens for light and dark mode.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.