Skip to content

usePaymentIntent ​

Confirms a PaymentIntent with stripe.confirmPayment(), using the Elements group from the nearest VueStripeElements, and tracks loading and error. Call it in a component rendered inside VueStripeProvider (or an app that installs createVueStripe()); inside VueStripeElements it picks up the Elements group automatically. Accept a payment with the Payment Element walks through the whole flow, and How confirming works explains what confirming does.

Usage ​

vue
<!-- PayButton.vue, rendered inside VueStripeElements next to VueStripePaymentElement -->
<script setup lang="ts">
import { ref } from 'vue'
import { usePaymentIntent } from '@vue-stripe/vue-stripe'

const props = defineProps<{ clientSecret: string }>()

const { confirmPayment, loading, error } = usePaymentIntent()
const paid = ref(false)

const pay = async () => {
  const result = await confirmPayment({
    clientSecret: props.clientSecret,
    confirmParams: { return_url: `${window.location.origin}/complete` }
  })

  // Paid without a redirect. A succeeded PaymentIntent cannot be confirmed again.
  if (result.paymentIntent?.status === 'succeeded') paid.value = true
}
</script>

<template>
  <p v-if="paid">Payment received.</p>
  <template v-else>
    <button :disabled="loading" @click="pay">{{ loading ? 'Processing...' : 'Pay' }}</button>
    <p v-if="error">{{ error }}</p>
  </template>
</template>

Options ​

confirmPayment(options) takes a ConfirmPaymentOptions:

ts
import type { ConfirmPaymentData, StripeElements } from '@stripe/stripe-js'

interface ConfirmPaymentOptions {
  clientSecret: string
  confirmParams?: ConfirmPaymentData
  redirect?: 'if_required' | 'always'
  elements?: StripeElements
  skipSubmit?: boolean
}
OptionTypeDefaultDescription
clientSecretstring—Client secret from the PaymentIntent (required)
confirmParamsConfirmPaymentData{}Stripe's confirmation parameters: return_url, payment_method_data.billing_details, shipping, ...
redirect'if_required' | 'always''if_required'Redirect behavior. The default differs from Stripe's own ('always'): the promise resolves unless the payment method needs a redirect
elementsStripeElementsinjectedOverride the injected Elements instance
skipSubmitbooleanfalseSkip the automatic elements.submit() validation call before confirmation

Return Value ​

ts
interface UsePaymentIntentReturn {
  confirmPayment: (options: ConfirmPaymentOptions) => Promise<ConfirmPaymentResult>
  retrievePaymentIntent: (clientSecret: string) => Promise<ConfirmPaymentResult>
  loading: Readonly<Ref<boolean>>
  error: Readonly<Ref<string | null>>
}
PropertyTypeDescription
confirmPayment(options: ConfirmPaymentOptions) => Promise<ConfirmPaymentResult>Confirm a payment
retrievePaymentIntent(clientSecret: string) => Promise<ConfirmPaymentResult>Look up a PaymentIntent by its client secret
loadingReadonly<Ref<boolean>>true while a confirm or retrieve is in flight (until the last of overlapping calls settles)
errorReadonly<Ref<string | null>>Error message from the last confirm or retrieve attempt

retrievePaymentIntent ​

Looks up the current status of a PaymentIntent by its client secret, for example on your return_url page after a redirect. It shares the loading / error refs with confirmPayment.

ts
const { retrievePaymentIntent } = usePaymentIntent()

const clientSecret = new URLSearchParams(window.location.search).get('payment_intent_client_secret')
if (clientSecret) {
  const { paymentIntent } = await retrievePaymentIntent(clientSecret)
  // paymentIntent?.status: 'succeeded', 'processing', 'requires_payment_method', ...
}

Result ​

confirmPayment and retrievePaymentIntent resolve to a ConfirmPaymentResult, exported from the package:

ts
// Declarations, for reference, not a sample: import the types from '@vue-stripe/vue-stripe'

// Stripe's own result, or the composable's error result
type ConfirmPaymentResult = PaymentIntentResult | VueStripeErrorResult

// PaymentIntentResult (from @stripe/stripe-js):
//   { paymentIntent: PaymentIntent; error?: undefined }
//   | { paymentIntent?: undefined; error: StripeError }

interface VueStripeErrorResult {
  error: { message: string }
  paymentIntent?: undefined
  setupIntent?: undefined
  paymentMethod?: undefined
}

With redirect: 'always', or when the payment method needs a redirect, a successful confirmation navigates away instead of resolving.

Automatic submit ​

Before calling stripe.confirmPayment(), confirmPayment calls elements.submit() to validate the form and collect wallet data, as Stripe requires for the Payment Element. If submit() reports a validation error, confirmPayment stops: error.value is set (to the validation message or 'Form validation failed') and the result resolves to { error: submitError }; stripe.confirmPayment() is not called. Pass skipSubmit: true only when you already called submit() yourself. Why the Payment Element is submitted first explains the requirement.

Error Handling ​

The promise never rejects. Every failure resolves to { error } and sets error.value:

  • a validation error from elements.submit();
  • Stripe's error (StripeError, with type, code and decline_code), for example a declined card;
  • { error: { message: 'Stripe not initialized' } } when Stripe.js is not available (outside a loaded provider, or the plugin's load failed; under the plugin a call made while Stripe.js loads waits for it);
  • { error: { message } } when the Stripe call throws.

So one if (result.error) check covers every failure. Outside a provider, usePaymentIntent() itself throws a VueStripeProviderError. The method confirmers overview lists which composables resolve { error } and which reject. Handle errors and declines shows how to branch on the error type, decline codes included.

TypeScript ​

ts
import { usePaymentIntent } from '@vue-stripe/vue-stripe'
import type {
  ConfirmPaymentOptions,
  ConfirmPaymentResult,
  UsePaymentIntentReturn
} from '@vue-stripe/vue-stripe'

// usePay.ts: call usePay() in a component's setup
export function usePay() {
  const { confirmPayment }: UsePaymentIntentReturn = usePaymentIntent()

  const pay = async (options: ConfirmPaymentOptions): Promise<ConfirmPaymentResult> => {
    const result = await confirmPayment(options)
    // result.paymentIntent is PaymentIntent | undefined
    // result.error is StripeError | { message: string } | undefined
    return result
  }

  return { pay }
}

See Also ​