Recipe
- Web
- Android
- iOS
- React Native
checkout.addEventListener('primer:payment-success', (event) => {
const { payment } = event.detail;
window.location.href = `/order/confirmation?id=${payment.orderId}`;
});
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)
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
}
}
)
}
}
}
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.How it works
- Web
- Android
- iOS
- React Native
- Listen for the
primer:payment-successevent on the checkout element - Extract the
paymentobject fromevent.detail - Use
window.location.hrefto redirect to your confirmation page - Pass the
orderIdas a query parameter so your confirmation page can display order details
- Observe
checkout.statewithcollectAsStateWithLifecycle()and react in aLaunchedEffect - Handle
PrimerCheckoutState.Successto accesscheckoutData.payment - Navigate to your confirmation screen using
navControlleror your preferred navigation approach - Use
popUpToto remove the checkout screen from the back stack
| Property | Type | Description |
|---|---|---|
payment.id | String | The Primer payment ID |
payment.orderId | String | Your order ID |
- Use the
onCompletioncallback onPrimerCheckout - Match on
.successto extract thePaymentResult - Update SwiftUI state to navigate to your confirmation view
- Pass
result.paymentIdto your confirmation view.result.paymentMethodTypetells you which method was used.result.amountandresult.currencyCodeare optional and arenilfor card payments, so take the amount and currency for your receipt from your own order.
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") |
- Read
paymentOutcomefromusePrimerCheckout()in a component insidePrimerCheckoutProvider, and react to it in auseEffect - When
statusis'success', read the payment frompaymentOutcome.data.payment - Navigate to your confirmation screen, for example with React Navigation’s
navigation.replace, which also removes the checkout screen from the back stack - Pass the
orderIdso your confirmation screen can display order details. The payment carries no amount or currency, so take them for your receipt from your own order.
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
- Web
- Android
- iOS
- React Native
checkout.addEventListener('primer:payment-success', (event) => {
const { payment } = event.detail;
window.location.href = `/order/confirmation?paymentId=${payment.id}`;
});
is PrimerCheckoutState.Success -> {
val paymentId = s.checkoutData.payment.id
navController.navigate("confirmation/$paymentId")
}
Pass the payment ID to your confirmation view:
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
}
}
)
}
}
}
Pass the payment ID to your confirmation screen. Render this component inside
PrimerCheckoutProvider, as in the recipe: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
- Web
- Android
- iOS
- React Native
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}`;
});
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.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.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, so you can keep the type of the method you started in your own state.See also
Show custom success message
Display a branded success state after payment
Track payment in analytics
Send payment events to your analytics platform