Recipe
- Web
- Android
- iOS
- React Native
checkout.addEventListener('primer:ready', (event) => {
const primer = event.detail;
primer.onPaymentFailure = ({ error }) => {
console.error('Payment failed:', {
code: error.code,
message: error.message,
diagnosticsId: error.diagnosticsId, // Share with Primer support
});
};
});
val state by checkout.state.collectAsStateWithLifecycle()
LaunchedEffect(state) {
when (val s = state) {
is PrimerCheckoutState.Failure -> {
val error = s.primerError
Log.e("Checkout", "Payment failed: ${error.description}")
Log.e("Checkout", "Error code: ${error.errorCode}")
Log.e("Checkout", "Diagnostics ID: ${error.diagnosticsId}")
Log.e("Checkout", "Recovery: ${error.recoverySuggestion}")
}
else -> Unit
}
}
PrimerCheckoutSheet(checkout = checkout)
PrimerCheckout(
clientToken: clientToken,
onCompletion: { state in
if case let .failure(error, checkoutData) = state {
print("[Primer] Error ID: \(error.errorId)")
print("[Primer] Description: \(error.errorDescription ?? "N/A")")
print("[Primer] Diagnostics: \(error.diagnosticsId)")
print("[Primer] Recovery: \(error.recoverySuggestion ?? "N/A")")
print("[Primer] Payment ID: \(checkoutData?.payment?.id ?? "none")")
print("[Primer] Order ID: \(checkoutData?.payment?.orderId ?? "none")")
}
}
)
import { useEffect, useState } from 'react';
import { Button } from 'react-native';
import {
PrimerCheckoutProvider,
PrimerCheckoutSheet,
usePrimerCheckout,
} from '@primer-io/react-native';
function ErrorLogger() {
const { paymentOutcome } = usePrimerCheckout();
useEffect(() => {
if (paymentOutcome?.status !== 'error') return;
const { error, data } = paymentOutcome;
console.error('[Primer] Error ID:', error.errorId);
console.error('[Primer] Description:', error.description);
console.error('[Primer] Error code:', error.errorCode ?? 'N/A');
console.error('[Primer] Diagnostics:', error.diagnosticsId ?? 'N/A');
console.error('[Primer] Recovery:', error.recoverySuggestion ?? 'N/A');
console.error('[Primer] Payment ID:', data?.payment?.id ?? 'none');
console.error('[Primer] Order ID:', data?.payment?.orderId ?? 'none');
}, [paymentOutcome]);
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);
};
return (
<>
<Button title="Checkout" onPress={openCheckout} />
{clientToken !== null && (
<PrimerCheckoutProvider clientToken={clientToken}>
<ErrorLogger />
<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:readyevent to access the Primer SDK instance - Set the
onPaymentFailurecallback - Log the error details including
diagnosticsIdwhich helps Primer support investigate issues
- Observe
checkout.statewithcollectAsStateWithLifecycle()and react in aLaunchedEffect - Handle
PrimerCheckoutState.Failureto access thePrimerError - Log the error details including
diagnosticsIdwhich helps Primer support investigate issues
PrimerError includes these properties:| Property | Type | Description |
|---|---|---|
errorId | String | Unique error identifier |
description | String | Human-readable error message |
errorCode | String? | Error code (e.g., "card_declined") |
diagnosticsId | String | Reference ID for Primer support |
recoverySuggestion | String? | Suggested recovery action for the user |
- Receive
PrimerCheckoutStatethrough theonCompletionclosure onPrimerCheckoutor the.primerCheckoutSession(_:theme:onCompletion:)modifier, and match.failure - Access
PrimerErrorproperties for debugging details - Log the
diagnosticsIdwhich helps Primer support investigate issues
PrimerError includes these properties:| Property | Type | Description |
|---|---|---|
errorId | String | Stable identifier for the error category |
errorDescription | String? | Human-readable error message for logging |
diagnosticsId | String | Reference ID for Primer support |
recoverySuggestion | String? | Developer-facing hint on how to fix the cause. nil for most payment failures, such as payment-failed |
.failure also carries checkoutData. When the payment was created before it failed (for example a decline), checkoutData?.payment holds the payment ID and order ID. Log them so you can look up the payment on your backend. checkoutData is nil when the failure happened before a payment existed (initialization, tokenization, connectivity)..failure arrives once per failed payment attempt. With the SDK error screen on (the default), the checkout stays open for a retry. Don’t treat .failure as the end of the session. .success or .dismissed arrives once and ends the session.If loading fails, PrimerCheckout delivers .failure. With the SDK error screen on (the default), the checkout stays open for a retry. With isErrorScreenEnabled: false, the checkout shows nothing and you build your own retry UI. The .primerCheckoutSession modifier delivers a load failure once and ends the session.- Render a component inside
PrimerCheckoutProviderthat readspaymentOutcomefromusePrimerCheckout()and reacts to it in auseEffect - When
statusis'error', read thePrimerErrorfrompaymentOutcome.error - Log the error details including
diagnosticsIdwhich helps Primer support investigate issues
PrimerError has these properties:| Property | Type | Description |
|---|---|---|
errorId | string | Identifier for the kind of error, for example payment-failed |
description | string | Description of the error |
errorCode | string | undefined | Error code, when there is one |
diagnosticsId | string | undefined | Reference ID for Primer support |
recoverySuggestion | string | undefined | Suggested fix, when there is one |
paymentOutcome.data holds the payment ID and order ID when the native SDK sends them with the failure. Log them when they’re there so you can look up the payment on your backend, and treat data as optional: it’s null when no payment data came with the failure.paymentOutcome reports every failure once a payment has started.Each failed attempt sets a new outcome. Don’t treat an 'error' outcome as the end of the checkout session.If the checkout fails to start, paymentOutcome stays null and usePrimerCheckout().error is set instead. See Handle the payment result.The
diagnosticsId is a unique identifier for the error. When contacting Primer support, always include this ID to help them quickly locate and diagnose the issue.Variations
Send errors to logging service
- Web
- Android
- iOS
- React Native
primer.onPaymentFailure = ({ error, paymentMethodType }) => {
// Send to your logging service (Sentry, LogRocket, etc.)
Sentry.captureException(new Error(error.message), {
extra: {
code: error.code,
diagnosticsId: error.diagnosticsId,
paymentMethod: paymentMethodType,
},
});
};
is PrimerCheckoutState.Failure -> {
val error = s.primerError
Sentry.captureException(Exception(error.description)) { scope ->
scope.setExtra("error_code", error.errorCode.orEmpty())
scope.setExtra("diagnostics_id", error.diagnosticsId)
}
}
if case let .failure(error, _) = state {
ErrorLogger.log(
errorId: error.errorId,
description: error.errorDescription ?? "Unknown error",
diagnosticsId: error.diagnosticsId
)
}
import { useEffect } from 'react';
import * as Sentry from '@sentry/react-native';
import { usePrimerCheckout } from '@primer-io/react-native';
export function SentryErrorReporter() {
const { paymentOutcome } = usePrimerCheckout();
useEffect(() => {
if (paymentOutcome?.status !== 'error') return;
const { error, data } = paymentOutcome;
Sentry.captureException(new Error(error.description), {
extra: {
error_id: error.errorId,
error_code: error.errorCode ?? '',
diagnostics_id: error.diagnosticsId ?? '',
payment_id: data?.payment?.id ?? '',
},
});
}, [paymentOutcome]);
return null;
}
PrimerCheckoutProvider.Custom error display
- Web
- Android
- iOS
- React Native
const primerEvents = [
'primer:ready',
'primer:state-change',
'primer:methods-update',
'primer:payment-start',
'primer:payment-success',
'primer:payment-failure',
'primer:card-success',
'primer:card-error',
'primer:bin-data-available',
'primer:vault-methods-update',
];
primerEvents.forEach((eventName) => {
document.addEventListener(eventName, (event) => {
console.log(`[${eventName}]`, event.detail);
});
});
Override the error slot in
PrimerCheckoutSheet to display a custom error screen. Observe checkout.state separately to log diagnostics:val state by checkout.state.collectAsStateWithLifecycle()
LaunchedEffect(state) {
when (val s = state) {
is PrimerCheckoutState.Failure -> {
Log.e("Checkout", "Diagnostics: ${s.primerError.diagnosticsId}")
}
else -> Unit
}
}
PrimerCheckoutSheet(
checkout = checkout,
error = { primerError, _ ->
Column(
modifier = Modifier
.fillMaxWidth()
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Icon(
imageVector = Icons.Default.Warning,
contentDescription = null,
tint = MaterialTheme.colorScheme.error,
modifier = Modifier.size(48.dp),
)
Spacer(Modifier.height(16.dp))
Text(
text = "Something went wrong",
style = MaterialTheme.typography.titleLarge,
)
Spacer(Modifier.height(8.dp))
Text(
text = primerError.description,
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
if (primerError.recoverySuggestion != null) {
Spacer(Modifier.height(4.dp))
Text(
text = primerError.recoverySuggestion!!,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center,
)
}
Spacer(Modifier.height(24.dp))
Button(
onClick = { checkout.refresh() },
modifier = Modifier.fillMaxWidth(),
) {
Text("Try again")
}
Spacer(Modifier.height(8.dp))
OutlinedButton(
onClick = { checkout.dismiss() },
modifier = Modifier.fillMaxWidth(),
) {
Text("Use a different method")
}
}
},
onDismiss = { },
)
Compose the checkout with
PrimerCheckoutSession. Turn off the SDK error screen with PrimerUIOptions(isErrorScreenEnabled: false). Then use each .failure from the .primerCheckoutSession modifier’s onCompletion to drive your own error screen. If you leave the SDK error screen on (the default), it opens in a sheet over your view on every failed payment. Your error view appears only after the shopper dismisses that sheet.struct CheckoutView: View {
@StateObject private var session: PrimerCheckoutSession
@State private var paymentError: PrimerError?
init(clientToken: String) {
let settings = PrimerSettings(uiOptions: PrimerUIOptions(isErrorScreenEnabled: false))
_session = StateObject(wrappedValue: PrimerCheckoutSession(clientToken: clientToken, settings: settings))
}
var body: some View {
ScrollView {
if paymentError != nil {
VStack(spacing: 16) {
Image(systemName: "exclamationmark.triangle.fill")
.font(.largeTitle)
.foregroundColor(.red)
Text("Something went wrong")
.font(.title2)
Text("Please try a different payment method.")
.multilineTextAlignment(.center)
.foregroundColor(.secondary)
Button("Try again") {
paymentError = nil
Task {
if session.phase == .ready {
await session.refresh()
} else {
session.cancel()
await session.start()
}
}
}
.buttonStyle(.borderedProminent)
}
.padding(24)
} else {
PrimerPaymentMethods()
PrimerCardForm()
}
}
.primerCheckoutSession(session) { state in
if case let .failure(error, checkoutData) = state {
print("[Primer] Error: \(error.errorDescription ?? "N/A")")
print("[Primer] Diagnostics: \(error.diagnosticsId)")
print("[Primer] Payment ID: \(checkoutData?.payment?.id ?? "none")")
paymentError = error
}
}
}
}
refresh() only reloads a session that has finished loading. If loading failed, “Try again” calls cancel() and then start() to rebuild the session.Turn off the error screen of “Try again” fetches a new client token and mounts a new
PrimerCheckoutSheet with settings.uiOptions.isErrorScreenEnabled: false. After a failed payment, the sheet then clears paymentOutcome and calls onRequestDismiss instead. Keep the error when the outcome arrives and show your own screen, for example with PrimerErrorScreen:import { useCallback, useEffect, useState } from 'react';
import { Button, View } from 'react-native';
import {
PrimerCheckoutProvider,
PrimerCheckoutSheet,
PrimerErrorScreen,
usePrimerCheckout,
} from '@primer-io/react-native';
import type { PrimerError, PrimerSettings } from '@primer-io/react-native';
const settings: PrimerSettings = {
uiOptions: { isErrorScreenEnabled: false },
};
function ErrorListener({ onError }: { onError: (error: PrimerError) => void }) {
const { paymentOutcome } = usePrimerCheckout();
useEffect(() => {
if (paymentOutcome?.status === 'error') {
console.error('[Primer] Error ID:', paymentOutcome.error.errorId);
console.error('[Primer] Description:', paymentOutcome.error.description);
console.error('[Primer] Diagnostics:', paymentOutcome.error.diagnosticsId ?? 'N/A');
onError(paymentOutcome.error);
}
}, [paymentOutcome, onError]);
return null;
}
export function CheckoutScreen({ fetchClientToken }: { fetchClientToken: () => Promise<string> }) {
const [clientToken, setClientToken] = useState<string | null>(null);
const [visible, setVisible] = useState(false);
const [paymentError, setPaymentError] = useState<PrimerError | null>(null);
const openCheckout = useCallback(async () => {
const token = await fetchClientToken();
setPaymentError(null);
setClientToken(token);
setVisible(true);
}, [fetchClientToken]);
if (paymentError) {
return (
<PrimerErrorScreen
title="Something went wrong"
subtitle="The payment didn't go through. Try again or use a different payment method."
retryLabel="Try again"
onRetry={openCheckout}
/>
);
}
return (
<View style={{ padding: 24 }}>
<Button title="Checkout" onPress={openCheckout} />
{clientToken !== null && (
<PrimerCheckoutProvider clientToken={clientToken} settings={settings}>
<ErrorListener onError={setPaymentError} />
<PrimerCheckoutSheet visible={visible} onRequestDismiss={() => setVisible(false)} />
</PrimerCheckoutProvider>
)}
</View>
);
}
PrimerCheckoutProvider, which starts a new checkout session.Inline error handling
- Web
- Android
- iOS
- React Native
checkout.addEventListener('primer:methods-update', (event) => {
const methods = event.detail.toArray();
console.table(methods.map((m) => ({ type: m.type, id: m.id })));
});
For
PrimerCheckoutHost, observe checkout.state and display your own UI:@Composable
fun InlineCheckoutWithErrors(clientToken: String) {
val checkout = rememberPrimerCheckoutController(clientToken)
val state by checkout.state.collectAsStateWithLifecycle()
var paymentError by remember { mutableStateOf<PrimerError?>(null) }
LaunchedEffect(state) {
when (val s = state) {
is PrimerCheckoutState.Failure -> {
paymentError = s.primerError
}
is PrimerCheckoutState.Success -> {
paymentError = null
}
else -> Unit
}
}
when (state) {
is PrimerCheckoutState.Loading -> {
CircularProgressIndicator()
}
is PrimerCheckoutState.Ready -> {
PrimerCheckoutHost(checkout = checkout) {
val cardFormController = rememberCardFormController(checkout)
Column(Modifier.padding(16.dp)) {
PrimerCardForm(controller = cardFormController)
paymentError?.let { error ->
Spacer(Modifier.height(16.dp))
Card(
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.errorContainer,
),
modifier = Modifier.fillMaxWidth(),
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
imageVector = Icons.Default.Warning,
contentDescription = null,
tint = MaterialTheme.colorScheme.error,
)
Spacer(Modifier.width(12.dp))
Column(Modifier.weight(1f)) {
Text(
text = error.description,
style = MaterialTheme.typography.bodyMedium,
)
if (error.recoverySuggestion != null) {
Text(
text = error.recoverySuggestion!!,
style = MaterialTheme.typography.bodySmall,
)
}
}
}
}
}
}
}
}
}
}
Read per-field validation errors from
PrimerCardFormSession.state.fieldErrors inside a PrimerCardForm slot, and handle each failed payment attempt in onCompletion:struct InlineCheckoutWithErrors: View {
@StateObject private var session: PrimerCheckoutSession
init(clientToken: String) {
_session = StateObject(wrappedValue: PrimerCheckoutSession(clientToken: clientToken))
}
var body: some View {
ScrollView {
PrimerCardForm(
cardDetails: { cardSession in
CardDetails(session: cardSession)
}
)
.padding()
}
.primerCheckoutSession(session) { state in
if case let .failure(error, _) = state {
print("[Primer] Payment error: \(error.errorDescription ?? "N/A")")
}
}
}
}
struct CardDetails: View {
@ObservedObject var session: PrimerCardFormSession
var body: some View {
VStack(alignment: .leading, spacing: 8) {
CardFormDefaults.cardDetails(session)
ForEach(session.state.fieldErrors) { fieldError in
Text("\(fieldError.fieldType): \(fieldError.message)")
.font(.caption)
.foregroundColor(.red)
}
}
}
}
In a card layout you build yourself, read per-field validation errors from After a failed payment, the message and “Try again” replace the form. “Try again” starts a new checkout with a new client token from
usePrimerCardForm().errors and the error of a failed payment from paymentOutcome. The card inputs already show their own field’s error, so read errors for a summary or for fields you build yourself:import { useCallback, useEffect, useState } from 'react';
import { ActivityIndicator, Button, ScrollView, Text, View } from 'react-native';
import {
PrimerCardForm,
PrimerCardFormProvider,
PrimerCheckoutProvider,
usePrimerCardForm,
usePrimerCheckout,
usePrimerPaymentMethod,
} from '@primer-io/react-native';
export function InlineCheckoutWithErrors({ fetchClientToken }: { fetchClientToken: () => Promise<string> }) {
const [clientToken, setClientToken] = useState<string | null>(null);
const startCheckout = useCallback(async () => {
setClientToken(null);
setClientToken(await fetchClientToken());
}, [fetchClientToken]);
useEffect(() => {
void startCheckout();
}, [startCheckout]);
if (clientToken === null) {
return <ActivityIndicator />;
}
return (
<PrimerCheckoutProvider clientToken={clientToken}>
<PrimerCardFormProvider>
<CardFormWithErrors onTryAgain={startCheckout} />
</PrimerCardFormProvider>
</PrimerCheckoutProvider>
);
}
function CardFormWithErrors({ onTryAgain }: { onTryAgain: () => void }) {
const { isReady, paymentOutcome } = usePrimerCheckout();
const card = usePrimerPaymentMethod('PAYMENT_CARD');
const cardForm = usePrimerCardForm();
const [paying, setPaying] = useState(false);
const startCard = card.kind === 'card' ? card.start : undefined;
useEffect(() => {
if (isReady) {
startCard?.();
}
}, [isReady, startCard]);
useEffect(() => {
if (!paymentOutcome) return;
if (paymentOutcome.status === 'error') {
setPaying(false);
const { errorId, description, diagnosticsId, recoverySuggestion } = paymentOutcome.error;
console.error('[Primer] Payment error:', { errorId, description, diagnosticsId, recoverySuggestion });
}
}, [paymentOutcome]);
if (!isReady) {
return <ActivityIndicator />;
}
if (paymentOutcome?.status === 'error') {
return (
<View style={{ padding: 16 }}>
<View style={{ gap: 12, padding: 16, borderRadius: 8, backgroundColor: '#fdecea' }}>
<Text>The payment didn't go through. Try again or use another card.</Text>
<Button title="Try again" onPress={onTryAgain} />
</View>
</View>
);
}
return (
<ScrollView contentContainerStyle={{ gap: 16, padding: 16 }}>
<PrimerCardForm editable={!paying} />
{Object.entries(cardForm.errors).map(([field, message]) => (
<Text key={field} style={{ color: '#d32f2f', fontSize: 12 }}>
{`${field}: ${message}`}
</Text>
))}
<Button
title="Pay"
disabled={!cardForm.isValid || paying}
onPress={() => {
setPaying(true);
void cardForm.submit();
}}
/>
</ScrollView>
);
}
fetchClientToken.See also
Track payment in analytics
Send payment events to your analytics platform
Error handling
Handle payment failures and display error messages