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

# Redirect after successful payment

> Navigate users to a confirmation page after completing payment.

Redirect users to a confirmation or thank you page after a successful payment.

## Recipe

> [tab] Web
>
> ```javascript theme={"dark"}
> checkout.addEventListener('primer:payment-success', (event) => {
>   const { payment } = event.detail;
>   window.location.href = `/order/confirmation?id=${payment.orderId}`;
> });
> ```

> [tab] Android
>
> ```kotlin theme={"dark"}
> val state by checkout.state.collectAsStateWithLifecycle()
> LaunchedEffect(state) {
>     when (val s = state) {
>         is PrimerCheckoutState.Success -> {
>             val payment = s.checkoutData.payment
>             navController.navigate("confirmation/${payment.orderId}") {
>                 popUpTo("checkout") { inclusive = true }
>             }
>         }
>         else -> Unit
>     }
> }
>
> PrimerCheckoutSheet(checkout = checkout)
> ```

> [tab] iOS
>
> ```swift theme={"dark"}
> struct CheckoutView: View {
>   let clientToken: String
>   @State private var paymentResult: PaymentResult?
>   @State private var paymentCompleted = false
>
>   var body: some View {
>     if paymentCompleted, let result = paymentResult {
>       ConfirmationView(result: result)
>     } else {
>       PrimerCheckout(
>         clientToken: clientToken,
>         onCompletion: { state in
>           if case .success(let result) = state {
>             paymentResult = result
>             paymentCompleted = true
>           }
>         }
>       )
>     }
>   }
> }
> ```

> [tab] React Native
>
> ```tsx theme={"dark"}
> import { useEffect, useState } from 'react';
> import { Button } from 'react-native';
> import {
>   PrimerCheckoutProvider,
>   PrimerCheckoutSheet,
>   usePrimerCheckout,
> } from '@primer-io/react-native';
>
> type CheckoutNavigation = {
>   replace: (screen: 'Confirmation', params: { orderId?: string }) => void;
> };
>
> function RedirectOnSuccess({ navigation }: { navigation: CheckoutNavigation }) {
>   const { paymentOutcome } = usePrimerCheckout();
>
>   useEffect(() => {
>     if (paymentOutcome?.status === 'success') {
>       navigation.replace('Confirmation', { orderId: paymentOutcome.data.payment?.orderId });
>     }
>   }, [paymentOutcome, navigation]);
>
>   return null;
> }
>
> export function CheckoutScreen({
>   fetchClientToken,
>   navigation,
> }: {
>   fetchClientToken: () => Promise<string>;
>   navigation: CheckoutNavigation;
> }) {
>   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}>
>           <RedirectOnSuccess navigation={navigation} />
>           <PrimerCheckoutSheet
>             visible={visible}
>             onRequestDismiss={() => setVisible(false)}
>             onDismiss={() => setClientToken(null)}
>           />
>         </PrimerCheckoutProvider>
>       )}
>     </>
>   );
> }
> ```
>
> `fetchClientToken` stands for the call to your backend that returns a new client token. Each checkout attempt mounts a new `PrimerCheckoutProvider` with a new client token, as in the [`PrimerCheckoutSheet` example](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/primer-checkout-sheet#usage).

## How it works

> [tab] Web
>
> 1. Listen for the `primer:payment-success` event on the checkout element
> 2. Extract the `payment` object from `event.detail`
> 3. Use `window.location.href` to redirect to your confirmation page
> 4. Pass the `orderId` as a query parameter so your confirmation page can display order details

> [tab] Android
>
> 1. Observe `checkout.state` with `collectAsStateWithLifecycle()` and react in a `LaunchedEffect`
> 2. Handle `PrimerCheckoutState.Success` to access `checkoutData.payment`
> 3. Navigate to your confirmation screen using `navController` or your preferred navigation approach
> 4. Use `popUpTo` to remove the checkout screen from the back stack
>
> | Property | Type | Description |
> | - | - | - |
> | `payment.id` | `String` | The Primer payment ID |
> | `payment.orderId` | `String` | Your order ID |

> [tab] iOS
>
> 1. Use the `onCompletion` callback on `PrimerCheckout`
>
> 2. Match on `.success` to extract the `PaymentResult`
>
> 3. Update SwiftUI state to navigate to your confirmation view
>
> 4. Pass `result.paymentId` to your confirmation view. `result.paymentMethodType` tells you which method was used. `result.amount` and `result.currencyCode` are optional and are `nil` for card payments, so take the amount and currency for your receipt from your own order.
>
> > [!NOTE]
> >
> > By default, `PrimerCheckout` shows its own success screen and calls `onCompletion` with `.success` when that screen auto-dismisses after 3 seconds. To redirect as soon as the payment succeeds, turn off the SDK screen with `PrimerSettings(uiOptions: PrimerUIOptions(isSuccessScreenEnabled: false))` and pass it as `primerSettings`.
>
> | Property | Type | Description |
> | - | - | - |
> | `paymentId` | `String` | The Primer payment ID |
> | `paymentMethodType` | `String?` | The payment method type used (e.g. `"PAYMENT_CARD"`) |

> [tab] React Native
>
> 1. Read `paymentOutcome` from [`usePrimerCheckout()`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout) in a component inside `PrimerCheckoutProvider`, and react to it in a `useEffect`
>
> 2. When `status` is `'success'`, read the payment from `paymentOutcome.data.payment`
>
> 3. Navigate to your confirmation screen, for example with React Navigation's `navigation.replace`, which also removes the checkout screen from the back stack
>
> 4. Pass the `orderId` so your confirmation screen can display order details. The payment carries no amount or currency, so take them for your receipt from your own order.
>
> > [!NOTE]
> >
> > `paymentOutcome` is set as soon as the payment succeeds, while `PrimerCheckoutSheet` shows its success screen, so you don't need to wait for that screen to close. To skip it, set `settings.uiOptions.isSuccessScreenEnabled` to `false`. To also redirect after a payment that waits for settlement, such as Stripe ACH, check for `'pending'` as well.
>
> | Property | Type | Description |
> | - | - | - |
> | `payment.id` | `string \| undefined` | The Primer payment ID |
> | `payment.orderId` | `string \| undefined` | Your order ID |

## Variations

### Redirect with payment ID

> [tab] Web
>
> ```javascript theme={"dark"}
> checkout.addEventListener('primer:payment-success', (event) => {
>   const { payment } = event.detail;
>   window.location.href = `/order/confirmation?paymentId=${payment.id}`;
> });
> ```

> [tab] Android
>
> ```kotlin theme={"dark"}
> is PrimerCheckoutState.Success -> {
>     val paymentId = s.checkoutData.payment.id
>     navController.navigate("confirmation/$paymentId")
> }
> ```

> [tab] iOS
>
> Pass the payment ID to your confirmation view:
>
> ```swift theme={"dark"}
> struct CheckoutView: View {
>   let clientToken: String
>   @State private var paymentId: String?
>
>   var body: some View {
>     if let paymentId {
>       ConfirmationView(paymentId: paymentId)
>     } else {
>       PrimerCheckout(
>         clientToken: clientToken,
>         onCompletion: { state in
>           if case .success(let result) = state {
>             paymentId = result.paymentId
>           }
>         }
>       )
>     }
>   }
> }
> ```

> [tab] React Native
>
> Pass the payment ID to your confirmation screen. Render this component inside `PrimerCheckoutProvider`, as in the recipe:
>
> ```tsx theme={"dark"}
> import { useEffect } from 'react';
> import { usePrimerCheckout } from '@primer-io/react-native';
>
> type CheckoutNavigation = {
>   replace: (screen: 'Confirmation', params: { paymentId?: string }) => void;
> };
>
> export function RedirectWithPaymentId({ navigation }: { navigation: CheckoutNavigation }) {
>   const { paymentOutcome } = usePrimerCheckout();
>
>   useEffect(() => {
>     if (paymentOutcome?.status === 'success') {
>       navigation.replace('Confirmation', { paymentId: paymentOutcome.data.payment?.id });
>     }
>   }, [paymentOutcome, navigation]);
>
>   return null;
> }
> ```

### Redirect to different pages based on payment method

> [tab] Web
>
> ```javascript theme={"dark"}
> checkout.addEventListener('primer:payment-success', (event) => {
>   const { payment, paymentMethodType } = event.detail;
>
>   const redirectUrls = {
>     PAYMENT_CARD: '/confirmation/card',
>     PAYPAL: '/confirmation/paypal',
>     default: '/confirmation',
>   };
>
>   const url = redirectUrls[paymentMethodType] || redirectUrls.default;
>   window.location.href = `${url}?id=${payment.orderId}`;
> });
> ```

> [tab] Android
>
> > [!NOTE]
> >
> > Payment method type is not available on `PrimerCheckoutState.Success`. The SDK manages payment method routing internally. Use the `payment.orderId` to redirect to a single confirmation screen.

> [tab] iOS
>
> > [!NOTE]
> >
> > The completion result exposes `result.paymentMethodType` (e.g. `"PAYMENT_CARD"`), so you can branch your redirect on it. Pass `result.paymentId` along to your confirmation screen.

> [tab] React Native
>
> > [!NOTE]
> >
> > Payment method type is not available in `paymentOutcome` or `CheckoutData`. Use `payment.orderId` to redirect to a single confirmation screen. In a layout you build yourself, your code starts each payment method with [`usePrimerPaymentMethod`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/payment-methods/use-primer-payment-method), so you can keep the type of the method you started in your own state.

## See also

> [card] [Show custom success message](https://primer.io/docs/checkout/primer-checkout/guides-and-recipes/show-success-message.md)
>
> Display a branded success state after payment

> [card] [Track payment in analytics](https://primer.io/docs/checkout/primer-checkout/guides-and-recipes/track-payment-analytics.md)
>
> Send payment events to your analytics platform


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