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

# Handle the payment result

> Read the outcome of each payment attempt from usePrimerCheckout, and use the provider callbacks for logging.

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

Each payment attempt ends in a `paymentOutcome` on [`usePrimerCheckout()`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout). It reports the result of every payment the native SDK processes, for every payment method, so build your result handling on it. The provider callbacks `onCheckoutComplete` and `onError` are useful for logging.

## Payment outcomes

`paymentOutcome` is `null` until the first attempt ends. After that, it holds the latest outcome until the next outcome replaces it or it's cleared, for example by `clearPaymentOutcome()` or by the prebuilt sheet.

| `status` | Fields | Meaning |
| - | - | - |
| `'success'` | `data: CheckoutData` | The payment completed. |
| `'pending'` | `data: CheckoutData` | A Stripe ACH payment was authorized and waits for settlement. |
| `'error'` | `error: PrimerError`, `data: CheckoutData` or `null` | The attempt failed. `data` carries the payment ID and order ID when the native SDK sends them with the failure. |

`data.payment` holds the payment's `id` and `orderId`.

> [!NOTE]
>
> A `'success'` outcome can also be a payment that is still pending. For every payment method, confirm the payment status on your server with the payment ID before you fulfill the order.

## Read the outcome

Read `paymentOutcome` from a component inside `PrimerCheckoutProvider`. `usePrimerCheckout()` throws outside the provider.

```tsx theme={"dark"}
import { useCallback, useEffect, useState } from 'react';
import { Button } from 'react-native';
import {
  PrimerCheckoutProvider,
  PrimerCheckoutSheet,
  usePrimerCheckout,
} from '@primer-io/react-native';
import type { PaymentOutcome } from '@primer-io/react-native';

function OutcomeListener({ onOutcome }: { onOutcome: (outcome: PaymentOutcome) => void }) {
  const { paymentOutcome } = usePrimerCheckout();

  useEffect(() => {
    if (paymentOutcome) {
      onOutcome(paymentOutcome);
    }
  }, [paymentOutcome, onOutcome]);

  return null;
}

export function CheckoutScreen({ fetchClientToken }: { fetchClientToken: () => Promise<string> }) {
  const [clientToken, setClientToken] = useState<string | null>(null);
  const [visible, setVisible] = useState(false);

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

  const handleOutcome = useCallback((outcome: PaymentOutcome) => {
    switch (outcome.status) {
      case 'success':
        confirmOrder(outcome.data.payment?.id);
        break;
      case 'pending':
        showPendingNotice(outcome.data.payment?.id);
        break;
      case 'error':
        logPaymentFailure(outcome.error.errorId, outcome.error.diagnosticsId, outcome.data?.payment?.id);
        break;
    }
  }, []);

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

`confirmOrder`, `showPendingNotice` and `logPaymentFailure` stand for your own code. The effect runs once for each new outcome, as long as `onOutcome` keeps the same identity, which `useCallback` ensures here.

## With the prebuilt sheet

`PrimerCheckoutSheet` shows a result screen for each outcome:

| Outcome | What the sheet does |
| - | - |
| `'success'` | Shows the success screen, then calls `onRequestDismiss` after about 3 seconds. |
| `'pending'` | Shows the pending screen, then calls `onRequestDismiss` after about 3 seconds. |
| `'error'` | Shows the error screen, with a retry button and a way back to the payment methods. |

The sheet closes only when your `onRequestDismiss` sets `visible` to `false`.

To skip the result screens, set `settings.uiOptions`:

* `isSuccessScreenEnabled: false` skips the success and pending screens.
* `isErrorScreenEnabled: false` skips the error screen. The shopper then gets no retry in the sheet.

When a screen is skipped, the sheet calls `onRequestDismiss` instead of showing it, and clears the outcome at the same time. Read the outcome in an effect, as in the example above, so you don't miss it. Skipping a screen doesn't change what the callbacks report.

> [!TIP]
>
> Use a new client token and a new `PrimerCheckoutProvider` for each checkout attempt. Mount the provider when you open the sheet and unmount it in `onDismiss`, as in the example above.

## With a custom layout

Without `PrimerCheckoutSheet`, nothing shows the result for you. Render your own result UI from `paymentOutcome`.

* When the shopper cancels a payment method with its own UI, such as Apple Pay, Google Pay or PayPal, `paymentOutcome` is an error with `errorId` `payment-cancelled`. Check for it so you don't show a cancel as a failed payment. See [PrimerError](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-error.md#where-errors-arrive).

## Callbacks

`PrimerCheckoutProvider` also reports results through two callbacks:

| Callback | Receives | Called when |
| - | - | - |
| `onCheckoutComplete` | `checkoutData: CheckoutData` | The native SDK reports a created payment. |
| `onError` | `error: PrimerError`, `checkoutData: CheckoutData` or `null` | The native SDK reports a failure, for example a declined payment, a failed 3D Secure challenge or a network error. |

```tsx theme={"dark"}
<PrimerCheckoutProvider
  clientToken={clientToken}
  onCheckoutComplete={(checkoutData) => {
    trackPaymentCreated(checkoutData.payment?.id, checkoutData.payment?.orderId);
  }}
  onError={(error, checkoutData) => {
    trackPaymentError(error.errorId, error.diagnosticsId, checkoutData?.payment?.id);
  }}
>
  {/* ... */}
</PrimerCheckoutProvider>
```

`trackPaymentCreated` and `trackPaymentError` stand for your own code.

## Errors when the checkout starts

`usePrimerCheckout().error` is set when the checkout fails to start, for example because the client token has expired. `isReady` stays `false`. Payment errors don't go there: they arrive in `paymentOutcome`.

Read the error's ID and message, as in this example:

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

function StartupError() {
  const { error } = usePrimerCheckout();
  if (!error) return null;

  const raw = error as { errorId?: string; code?: string; description?: string; message?: string };
  const errorId = raw.errorId ?? raw.code;
  const description = raw.description ?? raw.message;

  return <Text>Checkout couldn't start ({errorId}): {description}</Text>;
}
```

In the prebuilt sheet, a start-up error shows the error screen. To try again, close the sheet and mount `PrimerCheckoutProvider` again with a new client token.

## Read a PrimerError

`paymentOutcome.error` and the `error` argument of `onError` are typed as `PrimerError`:

| Field | Type | Description |
| - | - | - |
| `errorId` | `string` | Identifier for the kind of error, for example `payment-failed`. |
| `description` | `string` | Description of the error. |
| `recoverySuggestion` | `string` or `undefined` | Suggested fix, when there is one. |
| `diagnosticsId` | `string` or `undefined` | ID that Primer support uses to trace the request. |
| `errorCode` | `string` or `undefined` | A more specific error code, set for some errors. |

Log `errorId` and `diagnosticsId`, and include `diagnosticsId` in support requests when it's set. See [PrimerError](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/common/primer-error.md) for the error IDs the SDK raises.

## See also

> [card] [usePrimerCheckout](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout.md)
>
> Checkout state, paymentOutcome and start-up errors

> [card] [PrimerCheckoutSheet](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/primer-checkout-sheet.md)
>
> The prebuilt sheet and its dismissal props

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

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


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