> ## 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.

# usePrimerCheckout

> Hook that returns the checkout state, the payment outcome and the checkout actions of the nearest PrimerCheckoutProvider.

> [!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.

`usePrimerCheckout` reads the checkout session of the nearest [`PrimerCheckoutProvider`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/primer-checkout-provider). Use it to follow the checkout as it starts, to read the client session and to react to the result of each payment through `paymentOutcome`.

## Signature

```tsx theme={"dark"}
function usePrimerCheckout(): PrimerCheckoutContextValue
```

`PrimerCheckoutContextValue` is exported from `@primer-io/react-native` as a type. The hook re-renders your component whenever the checkout state changes.

Outside a `PrimerCheckoutProvider`, the hook throws:

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

## Checkout state

Derive the checkout state from `isReady`, `error` and `paymentOutcome`:

| Condition | Meaning |
| - | - |
| `isReady` is `false` and `error` is `null` | The checkout is starting. |
| `error` is not `null` | The checkout failed to start. |
| `isReady` is `true` | The checkout started and the payment methods are loaded. |
| `paymentOutcome` is not `null` | The latest payment attempt has a result. |

## Returns

### Session

| Field | Type | Description |
| - | - | - |
| `isReady` | `boolean` | `true` once the native SDK has started and returned the available payment methods. |
| `error` | `PrimerError \| null` | Set when the checkout fails to start. Payment errors after `isReady` go to `paymentOutcome` instead. See [Reading a start error](#reading-a-start-error). |
| `clientSession` | [`ClientSession`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/checkout-data) `\| null` | The latest client session that the native SDK reported: `totalAmount`, `currencyCode`, `lineItems`, `customer`, `checkoutModules` and the other client session fields. Updated when the client session changes. |
| `acceptedCardNetworks` | `string[] \| null` | The card networks the client session accepts, in order, for example `['VISA', 'MASTERCARD']`. `null` until loaded or when no list is available; `[]` when the list is empty. |
| `availablePaymentMethods` | `PaymentMethod[]` | The payment methods the native SDK returned. Each has `paymentMethodType`, `paymentMethodManagerCategories` and `supportedPrimerSessionIntents`. For display, use [`usePrimerPaymentMethods`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/payment-methods/primer-payment-method-list). |
| `paymentMethodResources` | `Array<AssetResource \| NativeViewResource>` | Logos, colors and native view names for the payment methods. |
| `isLoadingResources` | `boolean` | `true` while the payment method resources load after `isReady`. |
| `resourcesError` | `Error \| null` | Set when the payment method resources fail to load. |
| `settings` | [`PrimerSettings`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-settings) `\| undefined` | The `settings` passed to the provider. |

### Payment result

| Field | Type | Description |
| - | - | - |
| `paymentOutcome` | [`PaymentOutcome`](#paymentoutcome) `\| null` | The result of the latest payment attempt. `null` before the first result. |
| `clearPaymentOutcome` | `() => void` | Sets `paymentOutcome` back to `null`. |
| `retry` | `() => Promise<void>` | Submits the last entered payment data again. `PrimerCheckoutSheet` calls it from its Retry button. When there is nothing to retry, it rejects with the error of the current error outcome, or with a `PrimerError` whose `errorId` is `'retry-unavailable'`. |

### PaymentOutcome

```tsx theme={"dark"}
type PaymentOutcome =
  | { status: 'success'; data: CheckoutData }
  | { status: 'pending'; data: CheckoutData }
  | { status: 'error'; error: PrimerError; data: CheckoutData | null };
```

| `status` | Description |
| - | - |
| `'success'` | The native SDK reported a completed checkout. `data.payment` has the payment `id` and `orderId`. |
| `'pending'` | A Stripe ACH payment that awaits settlement. |
| `'error'` | The payment attempt failed. `error` is the [`PrimerError`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-error). `data` carries the payment data when the native SDK sends it, otherwise `null`. A declined payment arrives here with the `errorId` `'payment-failed'`. |

`paymentOutcome` is shared by all payment methods of the provider. Each new result replaces the previous one. A failed payment doesn't end the session: the shopper can try again, and the next attempt sets a new outcome.

Use `paymentOutcome` as your main result signal. See [Results and callbacks](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/primer-checkout-provider.md#results-and-callbacks).

> [!NOTE]
>
> `PrimerCheckoutSheet` clears `paymentOutcome` when its success or pending screen auto-closes after 3 seconds, when it skips a result screen because it is turned off in settings, and when the shopper leaves the error screen to choose another payment method. Closing the sheet by hand does not clear it. React to the outcome when it changes, for example in an effect, rather than reading it later.

### Lower-level state and actions

The value also carries the state and actions behind the other hooks. Use [`usePrimerPaymentMethod`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/payment-methods/use-primer-payment-method), [`usePrimerCardForm`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/card-form/use-primer-card-form) and [`usePrimerVaultManager`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/payment-methods/use-primer-vault-manager) instead of calling them directly.

**Card and raw data**

| Field | Type | Description |
| - | - | - |
| `activeMethod` | `string \| null` | The card or raw-data payment method that is currently active. |
| `setActiveMethod` | `(method: string \| null) => void` | Activates a card or raw-data payment method, or tears it down with `null`. `usePrimerPaymentMethod(type).start()` calls it for these kinds. |
| `cardFormState` | `CardFormState` | Native validation state: `isValid`, `errors`, `binData`, `metadata` and `requiredFields`. |
| `setRawData` | `(data: RawData) => Promise<void>` | Sends payment data to the active payment method. |
| `selectedCardNetwork` | `CardNetworkId \| null` | The network the shopper picked for a co-badged card. |
| `selectCardNetwork` | `(identifier: CardNetworkId) => Promise<void>` | Sets the co-badged card network. |
| `submit` | `() => Promise<void>` | Submits the active payment method. |

**Native UI payment methods**

| Field | Type | Description |
| - | - | - |
| `nativeUiInFlightType` | `string \| null` | The native UI payment method in progress, or `null`. |
| `startNativeUI` | `(paymentMethodType: string) => Promise<void>` | Opens the native UI of a payment method. |
| `cancelNativeUI` | `(paymentMethodType: string) => void` | Clears the in-progress flag. |

**Bank selection**

| Field | Type | Description |
| - | - | - |
| `banks` | `IssuingBank[]` | The bank list of the active bank selection payment method. |
| `selectedBankId` | `string \| null` | The selected bank. |
| `isBanksLoading` | `boolean` | `true` while the bank list loads or a bank payment is submitted. |
| `startBanks`, `filterBanks`, `selectBank`, `submitBanks`, `stopBanks` | Functions | Start the flow, filter the list, select a bank, submit, and stop the flow. |

**Klarna**

| Field | Type | Description |
| - | - | - |
| `klarnaPaymentCategories` | `KlarnaPaymentCategory[]` | The available Klarna payment categories. |
| `selectedKlarnaCategoryId` | `string \| null` | The selected category. |
| `isKlarnaViewLoaded` | `boolean` | `true` once the Klarna view has loaded. |
| `isKlarnaLoading` | `boolean` | `true` while the session starts or a payment is authorized or finalized. |
| `startKlarna`, `selectKlarnaCategory`, `authorizeKlarna`, `finalizeKlarna`, `stopKlarna` | Functions | Start the flow, select a category, authorize, finalize, and stop the flow. |

**Stripe ACH**

| Field | Type | Description |
| - | - | - |
| `achStep` | `StripeAchStep` | The step of the flow: `'idle'`, `'starting'`, `'collectingDetails'`, `'submittingDetails'`, `'awaitingBankLink'`, `'mandatePending'` or `'answeringMandate'`. |
| `achUserDetails` | `StripeAchUserDetails` | `firstName`, `lastName` and `emailAddress`. |
| `achFieldErrors` | `StripeAchFieldErrors` | Validation messages per field. |
| `achIsValid` | `boolean` | `true` while no field has a validation error. |
| `achMandate` | `StripeAchMandateDisplay \| null` | The mandate text to show, while the mandate waits for an answer. |
| `startAch`, `setAchFirstName`, `setAchLastName`, `setAchEmailAddress`, `submitAchDetails`, `acceptAchMandate`, `declineAchMandate`, `stopAch` | Functions | Start the flow, set the details, submit them, answer the mandate, and stop the flow. |

**Saved payment methods**

| Field | Type | Description |
| - | - | - |
| `vaultedMethods` | `VaultedPaymentMethod[]` | The shopper's saved payment methods, as the native SDK returns them. |
| `vaultedIconUrisById`, `vaultedNamesById` | `Record<string, string \| undefined>` | Icon URIs and display names, by saved payment method id. |
| `isLoadingVaulted` | `boolean` | `true` while the saved payment methods load. |
| `vaultedError` | `Error \| null` | Set when the saved payment methods fail to load. |
| `activeVaultedMethodId` | `string \| null` | The saved payment method the shopper selected, or `null`. |
| `vaultDisplayOverride` | `'expanded' \| null` | Display state of the saved payment methods section in `PrimerCheckoutSheet`. |
| `requiresVaultedCardCvv` | `boolean` | Whether the client session asks for the CVV again when paying with a saved card. |
| `cvvInputVisible` | `boolean` | Whether the sheet shows its CVV field for a saved card. |
| `payFromVault` | `(vaultedPaymentMethodId: string, additionalData?: VaultedPaymentMethodAdditionalData) => Promise<void>` | Starts a payment with a saved payment method. Read the result from `paymentOutcome`. |
| `selectVaultedMethodId`, `requestExpandedVaultDisplay`, `setCvvInputVisible`, `deleteVaultedPaymentMethod` | Functions | Select a saved payment method, expand the section, show the CVV field, and delete a saved payment method. |

## Usage

### Showing the start of the checkout

```tsx theme={"dark"}
import { useEffect } from 'react';
import type { ReactNode } from 'react';
import { ActivityIndicator, Text } from 'react-native';
import { usePrimerCheckout } from '@primer-io/react-native';
import type { PrimerError } from '@primer-io/react-native';
import { logCheckoutError } from './logging';

function CheckoutGate({ children }: { children: ReactNode }) {
  const { isReady, error } = usePrimerCheckout();

  useEffect(() => {
    if (!error) return;
    const startError = error as PrimerError & { code?: string };
    logCheckoutError(startError.errorId ?? startError.code, startError.description ?? startError.message);
  }, [error]);

  if (error) return <Text>Checkout is not available right now.</Text>;
  if (!isReady) return <ActivityIndicator />;
  return <>{children}</>;
}
```

### Reading a start error

Read the ID and message of a start error as in the example above. To try again, pass a new client token to the provider or mount a new provider.

### Reacting to the payment outcome

```tsx theme={"dark"}
import { useEffect } from 'react';
import { usePrimerCheckout } from '@primer-io/react-native';
import { showError, showPending, showReceipt } from './checkout-ui';

function PaymentOutcomeHandler() {
  const { paymentOutcome } = usePrimerCheckout();

  useEffect(() => {
    if (!paymentOutcome) return;
    switch (paymentOutcome.status) {
      case 'success':
        showReceipt(paymentOutcome.data.payment?.id);
        break;
      case 'pending':
        showPending(paymentOutcome.data.payment?.id);
        break;
      case 'error': {
        const { errorId, description, diagnosticsId } = paymentOutcome.error;
        console.warn(`[${errorId}] ${description}`, { diagnosticsId });
        showError('The payment failed. Try again or use another payment method.');
        break;
      }
    }
  }, [paymentOutcome]);

  return null;
}
```

Render `PaymentOutcomeHandler` anywhere inside the provider. See [Handle the payment result](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/guides/handle-payment-result.md) for the full walkthrough.

### Reading the client session

```tsx theme={"dark"}
import { Text } from 'react-native';
import { usePrimerCheckout, usePrimerLocalization } from '@primer-io/react-native';

function OrderTotal() {
  const { clientSession } = usePrimerCheckout();
  const { formatCurrency } = usePrimerLocalization();

  const amount = clientSession?.totalAmount;
  const currency = clientSession?.currencyCode;
  if (amount == null || !currency) return null;

  return <Text>Total: {formatCurrency(amount, currency)}</Text>;
}
```

`formatCurrency` takes the amount in minor units and formats it in the device language.

## See also

> [card] [PrimerCheckoutProvider](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/primer-checkout-provider.md)
>
> Start the session and set the callbacks.

> [card] [Checkout data](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/checkout-data.md)
>
> The fields of CheckoutData, PaymentOutcome and ClientSession.

> [card] [PrimerError](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-error.md)
>
> Error fields and the error ids Primer Checkout raises.

> [card] [Handle the payment result](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/guides/handle-payment-result.md)
>
> React to successful, pending and failed payments.


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