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

# Disable buttons during payment

> Disable external buttons while payment is processing.

Prevent users from clicking buttons outside the checkout while payment is being processed.

## Recipe

> [tab] Web
>
> ```javascript theme={"dark"}
> const submitButton = document.getElementById('external-submit');
>
> checkout.addEventListener('primer:state-change', (event) => {
>   submitButton.disabled = event.detail.isProcessing;
> });
> ```

> [tab] Android
>
> ```kotlin theme={"dark"}
> val controller = rememberCardFormController(checkout)
> val state by controller.state.collectAsStateWithLifecycle()
>
> MyPayButton(
>     enabled = !state.isLoading,
>     onClick = { controller.submit() },
> )
> ```
>
> > [!NOTE]
> >
> > Android's `isLoading` only covers card form submission. There is no unified checkout-level processing state across all payment methods. For `PrimerCheckoutSheet`, swipe dismissal can be controlled via `PrimerSettings.uiOptions.dismissalMechanism`, but the back button is always active.

> [tab] iOS
>
> ```swift theme={"dark"}
> PrimerCardForm(submitButton: { session in
>   Button("Pay") { session.submit() }
>     .disabled(!session.state.isValid || session.state.isLoading)
> })
> ```
>
> Each `PrimerCardForm` slot receives the active `PrimerCardFormSession`. Read `session.state.isLoading` and `session.state.isValid` directly to drive button state -- the session republishes its `state` on every change.

> [tab] React Native
>
> ```tsx theme={"dark"}
> import { useCallback, useEffect, useState } from 'react';
> import { ActivityIndicator, Button, Text, View } from 'react-native';
> import {
>   PrimerCardForm,
>   PrimerCardFormProvider,
>   PrimerCheckoutProvider,
>   usePrimerCardForm,
>   usePrimerCheckout,
>   usePrimerPaymentMethod,
> } from '@primer-io/react-native';
>
> export function CheckoutScreen({ 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>
>         <CardPayment onTryAgain={startCheckout} />
>       </PrimerCardFormProvider>
>     </PrimerCheckoutProvider>
>   );
> }
>
> function CardPayment({ onTryAgain }: { onTryAgain: () => void }) {
>   const { paymentOutcome } = usePrimerCheckout();
>   const card = usePrimerPaymentMethod('PAYMENT_CARD');
>   const cardForm = usePrimerCardForm();
>   const startCard = card.kind === 'card' && card.isAvailable ? card.start : undefined;
>   const [isPaying, setIsPaying] = useState(false);
>
>   useEffect(() => {
>     startCard?.();
>   }, [startCard]);
>
>   useEffect(() => {
>     if (paymentOutcome?.status === 'error') setIsPaying(false);
>   }, [paymentOutcome]);
>
>   const pay = () => {
>     setIsPaying(true);
>     void cardForm.submit();
>   };
>
>   if (paymentOutcome?.status === 'error') {
>     return (
>       <View style={{ gap: 16, padding: 16 }}>
>         <Text>The payment didn't go through. Try again or use another card.</Text>
>         <Button title="Try again" onPress={onTryAgain} />
>       </View>
>     );
>   }
>
>   return (
>     <View style={{ gap: 16, padding: 16 }}>
>       <PrimerCardForm editable={!isPaying} />
>       <Button title="Pay" disabled={!cardForm.isValid || isPaying} onPress={pay} />
>     </View>
>   );
> }
> ```
>
> After a failed payment, "Try again" starts a new checkout with a new client token.

## How it works

> [tab] Web
>
> 1. Get a reference to your external button(s)
> 2. Listen for the `primer:state-change` event
> 3. Set the button's `disabled` property based on `isProcessing`
>
> The `isProcessing` flag is unified across all payment methods -- when any payment is in progress, it returns `true`.

> [tab] Android
>
> 1. Create a `PrimerCardFormController` using `rememberCardFormController()`
> 2. Observe its `state` with `collectAsStateWithLifecycle()`
> 3. Use `state.isLoading` to disable external buttons during card payment processing
>
> | Scope | Property | Coverage |
> | - | - | - |
> | Card form | `cardFormController.state.isLoading` | Card payments only |
> | Checkout session | `PrimerCheckoutState.Loading` / `.Ready` | Initialization only, **not** payment processing |

> [tab] iOS
>
> 1. Capture the `PrimerCardFormSession` passed into a `PrimerCardForm` slot closure
> 2. Read `session.state.isLoading` and `session.state.isValid` to control button state
> 3. Use SwiftUI's `.disabled()` modifier to disable buttons during processing
>
> | Property | Type | Description |
> | - | - | - |
> | `session.state.isLoading` | `Bool` | `true` while a card payment is being submitted |
> | `session.state.isValid` | `Bool` | `true` when all required card fields pass validation |

> [tab] React Native
>
> 1. Keep a paying flag in your component's state
> 2. Set it when the shopper taps Pay, then call [`usePrimerCardForm().submit()`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/card-form/use-primer-card-form)
> 3. Clear it only when [`usePrimerCheckout().paymentOutcome`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/use-primer-checkout) reports a failure, and disable your buttons while it is set
>
> | Property | Type | Description |
> | - | - | - |
> | `usePrimerCardForm().isValid` | `boolean` | `true` when the native SDK reports the card data as valid |
> | `usePrimerCheckout().paymentOutcome` | `PaymentOutcome \| null` | The result of the latest payment attempt. A new value means the attempt has ended |
>
> Native UI, bank selection and Klarna payment methods have their own `isLoading` on [`usePrimerPaymentMethod(type)`](https://primer.io/docs/sdk/react-native-checkout/v3.0.0-beta/api/payment-methods/use-primer-payment-method).

## Variations

### Disable multiple buttons

> [tab] Web
>
> ```javascript theme={"dark"}
> const buttons = document.querySelectorAll('.checkout-action');
>
> checkout.addEventListener('primer:state-change', (event) => {
>   buttons.forEach((button) => {
>     button.disabled = event.detail.isProcessing;
>   });
> });
> ```

> [tab] Android
>
> ```kotlin theme={"dark"}
> val controller = rememberCardFormController(checkout)
> val state by controller.state.collectAsStateWithLifecycle()
> val isProcessing = state.isLoading
>
> Column {
>     MyPayButton(enabled = !isProcessing, onClick = { controller.submit() })
>     MyCancelButton(enabled = !isProcessing, onClick = { onCancel() })
>     MyEditCartButton(enabled = !isProcessing, onClick = { onEditCart() })
> }
> ```

> [tab] iOS
>
> Replace the submit slot with a custom view, then disable your other action buttons from the same observed session:
>
> ```swift theme={"dark"}
> struct CheckoutActions: View {
>   @ObservedObject var session: PrimerCardFormSession
>   let onCancel: () -> Void
>   let onEditCart: () -> Void
>
>   var body: some View {
>     let isProcessing = session.state.isLoading
>     VStack {
>       Button("Pay") { session.submit() }
>         .disabled(!session.state.isValid || isProcessing)
>       Button("Cancel", action: onCancel)
>         .disabled(isProcessing)
>       Button("Edit Cart", action: onEditCart)
>         .disabled(isProcessing)
>     }
>   }
> }
>
> PrimerCardForm(submitButton: { session in
>   CheckoutActions(session: session, onCancel: { /* cancel */ }, onEditCart: { /* edit */ })
> })
> ```

> [tab] React Native
>
> Pass the paying flag from the recipe above to every action that must wait, and render the actions below `PrimerCardFormProvider`:
>
> ```tsx theme={"dark"}
> import { Button, View } from 'react-native';
> import { usePrimerCardForm } from '@primer-io/react-native';
>
> type CheckoutActionsProps = {
>   isPaying: boolean;
>   onPay: () => void;
>   onCancel: () => void;
>   onEditCart: () => void;
> };
>
> function CheckoutActions({ isPaying, onPay, onCancel, onEditCart }: CheckoutActionsProps) {
>   const { isValid } = usePrimerCardForm();
>
>   return (
>     <View style={{ gap: 8 }}>
>       <Button title="Pay" disabled={!isValid || isPaying} onPress={onPay} />
>       <Button title="Cancel" disabled={isPaying} onPress={onCancel} />
>       <Button title="Edit cart" disabled={isPaying} onPress={onEditCart} />
>     </View>
>   );
> }
> ```
>
> Render it in place of the Pay `Button` inside `CardPayment` from the recipe, where `isPaying` and `pay` are in scope. `onCancel` and `onEditCart` are your own handlers:
>
> ```tsx theme={"dark"}
> <CheckoutActions isPaying={isPaying} onPay={pay} onCancel={onCancel} onEditCart={onEditCart} />
> ```

### Add visual feedback

> [tab] Web
>
> ```javascript theme={"dark"}
> const submitButton = document.getElementById('external-submit');
>
> checkout.addEventListener('primer:state-change', (event) => {
>   const { isProcessing } = event.detail;
>
>   submitButton.disabled = isProcessing;
>   submitButton.textContent = isProcessing ? 'Processing...' : 'Pay Now';
>   submitButton.classList.toggle('loading', isProcessing);
> });
> ```

> [tab] Android
>
> ```kotlin theme={"dark"}
> val controller = rememberCardFormController(checkout)
> val state by controller.state.collectAsStateWithLifecycle()
>
> Button(
>     onClick = { controller.submit() },
>     enabled = !state.isLoading,
> ) {
>     if (state.isLoading) {
>         CircularProgressIndicator(modifier = Modifier.size(16.dp))
>         Spacer(modifier = Modifier.width(8.dp))
>         Text("Processing...")
>     } else {
>         Text("Pay Now")
>     }
> }
> ```

> [tab] iOS
>
> ```swift theme={"dark"}
> PrimerCardForm(submitButton: { session in
>   Button(action: { session.submit() }) {
>     if session.state.isLoading {
>       ProgressView()
>         .frame(width: 16, height: 16)
>       Text("Processing...")
>     } else {
>       Text("Pay Now")
>     }
>   }
>   .disabled(!session.state.isValid || session.state.isLoading)
> })
> ```

> [tab] React Native
>
> ```tsx theme={"dark"}
> import { ActivityIndicator, Pressable, Text } from 'react-native';
> import { usePrimerCardForm, usePrimerCheckout } from '@primer-io/react-native';
>
> function PayButton({ isPaying, onPress }: { isPaying: boolean; onPress: () => void }) {
>   const { isValid } = usePrimerCardForm();
>   const { paymentOutcome } = usePrimerCheckout();
>
>   if (paymentOutcome?.status === 'success') return <Text>Payment complete.</Text>;
>
>   return (
>     <Pressable
>       disabled={!isValid || isPaying}
>       onPress={onPress}
>       style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: 8, padding: 12 }}
>     >
>       {isPaying ? (
>         <>
>           <ActivityIndicator size="small" />
>           <Text>Processing...</Text>
>         </>
>       ) : (
>         <Text>Pay Now</Text>
>       )}
>     </Pressable>
>   );
> }
> ```

### Disable navigation during payment

> [tab] Web
>
> ```javascript theme={"dark"}
> checkout.addEventListener('primer:state-change', (event) => {
>   const { isProcessing } = event.detail;
>
>   document.querySelectorAll('a').forEach((link) => {
>     if (isProcessing) {
>       link.dataset.originalHref = link.href;
>       link.removeAttribute('href');
>       link.style.pointerEvents = 'none';
>     } else if (link.dataset.originalHref) {
>       link.href = link.dataset.originalHref;
>       link.style.pointerEvents = '';
>     }
>   });
> });
> ```

> [tab] Android
>
> ```kotlin theme={"dark"}
> val controller = rememberCardFormController(checkout)
> val state by controller.state.collectAsStateWithLifecycle()
>
> BackHandler(enabled = state.isLoading) {
>     // Intercept back press during payment processing
> }
> ```
>
> > [!NOTE]
> >
> > `BackHandler` only intercepts the system back gesture. For `PrimerCheckoutSheet`, swipe-to-dismiss is configured separately via `PrimerSettings.uiOptions.dismissalMechanism`. The SDK does not block the back button during processing.

> [tab] iOS
>
> Surface the session's loading state with the submit slot, then bind it to `.interactiveDismissDisabled()` on the enclosing sheet:
>
> ```swift theme={"dark"}
> @StateObject private var checkoutSession: PrimerCheckoutSession
> @State private var isLoading = false
>
> init(clientToken: String) {
>   _checkoutSession = StateObject(
>     wrappedValue: PrimerCheckoutSession(clientToken: clientToken)
>   )
> }
>
> var body: some View {
>   ScrollView {
>     PrimerCardForm(submitButton: { cardSession in
>       SubmitButton(session: cardSession, isLoading: $isLoading)
>     })
>   }
>   .interactiveDismissDisabled(isLoading)
>   .primerCheckoutSession(checkoutSession)
> }
>
> struct SubmitButton: View {
>   @ObservedObject var session: PrimerCardFormSession
>   @Binding var isLoading: Bool
>
>   var body: some View {
>     Button("Pay") { session.submit() }
>       .disabled(!session.state.isValid || session.state.isLoading)
>       .onChange(of: session.state.isLoading) { isLoading = $0 }
>   }
> }
> ```
>
> > [!NOTE]
> >
> > `.interactiveDismissDisabled()` stops the shopper from swiping away the sheet that holds `PrimerCardForm` while a card payment runs. This includes the time before your `onBeforePaymentCreate` handler decides. Primer's own sheets block swipe-to-dismiss while a payment is processing: the processing screen that `.primerCheckoutSession` presents, `PrimerCheckout` in a sheet, and `PrimerCheckoutPresenter`.

> [tab] React Native
>
> On Android, stop the back button with React Native's `BackHandler` while your paying flag is set:
>
> ```tsx theme={"dark"}
> import { useEffect } from 'react';
> import { BackHandler } from 'react-native';
>
> function useBlockBackWhilePaying(isPaying: boolean) {
>   useEffect(() => {
>     if (!isPaying) return;
>     const subscription = BackHandler.addEventListener('hardwareBackPress', () => true);
>     return () => subscription.remove();
>   }, [isPaying]);
> }
> ```
>
> Call `useBlockBackWhilePaying(isPaying)` in the component that holds the flag. If your card form sits on a navigator screen with a back gesture, turn the gesture off while the flag is set too.

## See also

> [card] [Show loading indicator](https://primer.io/docs/checkout/primer-checkout/guides-and-recipes/show-loading-indicator.md)
>
> Display a loading state during payment processing

> [card] [External submit button](https://primer.io/docs/checkout/primer-checkout/guides-and-recipes/external-submit-button.md)
>
> Trigger payment submission from outside the checkout component


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