Skip to content

VueStripeCheckoutElementsProvider ​

Initializes Elements with Checkout Sessions via stripe.initCheckoutElementsSdk() and provides the Checkout SDK to descendants. Place element components inside it to build your own checkout page on top of a Checkout Session, and drive the session with the useCheckoutSession composable. Choosing an integration compares it with the other Checkout options, and Build your own page on a Checkout Session shows the server and client code.

  • Use it inside VueStripeProvider.
  • Pass the client_secret of a Checkout Session created on your server with ui_mode: 'elements'.
  • The provider shows its #loading slot until the Stripe instance exists, then calls stripe.initCheckoutElementsSdk(), which returns the SDK synchronously, and renders its children at once. @ready fires when the session actions have loaded (loadActions()), and @change on every session change.

VueStripeCheckoutProvider is deprecated

VueStripeCheckoutProvider is a deprecated alias of this same component (it mirrored the old stripe.initCheckout() name). It keeps working in v6 but may be removed in a future major release, so import VueStripeCheckoutElementsProvider instead.

Usage ​

vue
<template>
  <VueStripeProvider :publishable-key="publishableKey">
    <VueStripeCheckoutElementsProvider :fetch-client-secret="fetchClientSecret">
      <CheckoutForm />
    </VueStripeCheckoutElementsProvider>
  </VueStripeProvider>
</template>

<script setup lang="ts">
import { VueStripeProvider, VueStripeCheckoutElementsProvider } from '@vue-stripe/vue-stripe'
import '@vue-stripe/vue-stripe/style.css' // once, e.g. in main.ts
import CheckoutForm from './CheckoutForm.vue'

const publishableKey = import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY

// From your backend: stripe.checkout.sessions.create({ ui_mode: 'elements', ... })
// Called by the provider in the browser only, once Stripe.js has loaded
const fetchClientSecret = () =>
  fetch('/api/checkout-elements-session', { method: 'POST' })
    .then((res) => res.json())
    .then((data: { clientSecret: string }) => data.clientSecret)
</script>

A Promise in client-secret works too, with two limits: a request that fails before the provider mounts is reported as an uncaught rejection (see Errors), and on a server-rendered page the request would start on the server (see Fetch client secrets in the browser).

vue
<!-- CheckoutForm.vue -->
<template>
  <VueStripeContactDetailsElement />
  <VueStripePaymentElement :options="{ layout: 'accordion' }" />
  <p v-if="session">Total: {{ session.total.total.amount }}</p>
  <button :disabled="loading || !session?.canConfirm" @click="pay">Pay</button>
  <p v-if="message">{{ message }}</p>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import {
  VueStripeContactDetailsElement,
  VueStripePaymentElement,
  useCheckoutSession
} from '@vue-stripe/vue-stripe'

const { session, confirm, loading } = useCheckoutSession()
const message = ref('')

const pay = async () => {
  // The session was created with return_url, so confirm() takes no returnUrl.
  const result = await confirm()
  if (result.type === 'error') message.value = result.error.message
}
</script>

session.canConfirm stays false until the session has the customer's email. VueStripeContactDetailsElement collects it and writes it to the session. If your server creates the session with customer_email, you can leave it out; the custom checkout guide lists the other ways to set the email.

Props ​

PropTypeRequiredDescription
clientSecretstring | Promise<string>No*Client secret of a ui_mode: 'elements' Checkout Session, or a Promise resolving to it. A Promise is checked before Stripe gets its value, see Errors
fetchClientSecret() => Promise<string>No*Function returning the client secret (v5 compatible). The provider calls it once Stripe.js has loaded and checks the result before Stripe gets it. Takes precedence over clientSecret
elementsOptionsStripeCheckoutElementsOptionsNoOptions for every Checkout element: appearance, fonts, loader, savedPaymentMethod, syncAddressCheckbox
adaptivePricing{ allowed?: boolean }NoAdaptive Pricing: let customers pay in their local currency
defaultValuesobjectNoPrefill email, phoneNumber, billingAddress, shippingAddress

* Provide either clientSecret or fetchClientSecret. If neither is set, the error slot renders and @error fires.

The props are read once, when the SDK is initialized. To use another session, re-mount the provider (for example with a :key).

elementsOptions ​

ts
interface StripeCheckoutElementsOptions {
  appearance?: Appearance
  loader?: 'auto' | 'always' | 'never'
  fonts?: Array<CssFontSource | CustomFontSource>
  savedPaymentMethod?: {
    enableSave?: 'auto' | 'never'
    enableRedisplay?: 'auto' | 'never'
  }
  syncAddressCheckbox?: 'billing' | 'shipping' | 'none'
}

Saved payment methods

Since Stripe.js Clover, saved payment methods are on by default on the client. Use elementsOptions.savedPaymentMethod (enableSave, enableRedisplay: 'never') to turn the save checkbox or the redisplay of saved methods off.

defaultValues ​

ts
interface DefaultValues {
  email?: string
  phoneNumber?: string
  billingAddress?: StripeCheckoutContact
  shippingAddress?: StripeCheckoutContact
}

Stripe refuses a default the session cannot take, and loadActions() fails; the provider reports it through @error and the #error slot, and the checkout does not render:

  • email on a session created with customer_email (or a customer with an email), which already sets it.
  • phoneNumber on a session created without phone_number_collection: { enabled: true }: "You cannot update the phone number unless phone_number_collection.enabled is set to true."

Events ​

EventPayloadDescription
@readyStripeCheckoutLoadActionsSuccessEmitted once loadActions() resolves. Payload is the session actions object
@changeStripeCheckoutSessionEmitted on every Stripe change event with the new session snapshot
@errorstringEmitted if fetchClientSecret or a clientSecret Promise rejects or resolves no client secret, initCheckoutElementsSdk() throws, loadActions() returns an error or rejects, or no client secret is given; also when Stripe.js fails to load under the plugin without a VueStripeProvider (the plugin logs that failure itself). loading turns false. See Errors

Errors ​

ErrorCauseFix
VueStripeProviderError (thrown)Not inside VueStripeProvider, in an app without createVueStripe()Wrap the provider in VueStripeProvider
requires a clientSecret (string or Promise) or fetchClientSecret propNeither prop givenPass one of them
The rejection's messagefetchClientSecret or the clientSecret Promise rejected, for example Failed to fetch (connection refused) or a JSON SyntaxError (the route answered an HTML error page)Fix the route, then re-mount the provider
fetchClientSecret did not resolve to a client secret, clientSecret did not resolve to a client secretThe function or the Promise resolved an empty or non-string value, for example undefined when the route answered 500 with a JSON error bodyResolve the session's client_secret
Stripe error messageinitCheckoutElementsSdk() threw or loadActions() failed: invalid or expired client secret, a session that is not ui_mode: 'elements', or a default the session refusesCreate a new session on your server, or drop the default

The provider checks the secret before Stripe.js gets it, so these failures reach only @error and #error. Two cases still log an uncaught error in the browser console:

  • A client secret string Stripe.js cannot parse (not of the form {cs_id}_secret_{secret}): Stripe.js reports it through loadActions() and also as its own Uncaught (in promise) IntegrationError.
  • A clientSecret Promise that rejects before the provider exists. Under VueStripeProvider, the provider mounts only once Stripe.js has loaded, so a request that fails fast rejects while nothing handles it. Pass fetch-client-secret instead: the provider calls it once Stripe.js has loaded.

Slots ​

SlotPropsRendered when
default{ checkout, actions, session, loading }As soon as the Checkout SDK exists (before the actions load)
loading—Until the Checkout SDK exists (Stripe.js still loading)
error{ error }Initialization failed

The default slot renders early so element components can mount while the session actions are still loading. Its loading prop (and useCheckoutSession().loading) stays true until loadActions() resolves; actions and session are null until then. Their types include null, so test them, not loading, before reading a member.

vue
<VueStripeCheckoutElementsProvider :client-secret="clientSecret">
  <template #loading><div>Preparing checkout...</div></template>
  <template #error="{ error }"><div class="error">{{ error }}</div></template>

  <template #default="{ session }">
    <VueStripePaymentElement />
    <p v-if="session">Total: {{ session.total.total.amount }}</p>
  </template>
</VueStripeCheckoutElementsProvider>
Slot PropTypeDescription
checkoutStripeCheckoutElementsSdkThe Checkout Elements SDK instance
actionsStripeCheckoutLoadActionsSuccess | nullSession actions (null until loaded)
sessionStripeCheckoutSession | nullCurrent session snapshot (null until loaded)
loadingbooleantrue until the actions have loaded

Elements inside the provider ​

Element components placed inside the provider are created with the Checkout SDK's creators instead of elements.create():

ComponentCreated withAccepted options
VueStripePaymentElementcreatePaymentElement()layout, paymentMethodOrder, readOnly, terms, fields, wallets, walletOptions
VueStripeAddressElementcreateBillingAddressElement() / createShippingAddressElement() (picked by options.mode: 'billing' or 'shipping', default 'shipping')contacts, display, fields
VueStripeExpressCheckoutElementcreateExpressCheckoutElement()buttonHeight, buttonTheme, buttonType, layout, paymentMethods, paymentMethodOrder
VueStripeTaxIdElementcreateTaxIdElement()All Tax ID options
VueStripeContactDetailsElementcreateContactDetailsElement()None
VueStripeCurrencySelectorElementcreateCurrencySelectorElement()None
VueStripeTermsElementcreateTermsElement() (Stripe beta)None
VueStripeLinkSignupElementcreateLinkSignupElement() (Stripe beta)defaultValues

The Checkout SDK accepts only these option subsets; other keys you pass are dropped. Appearance and fonts are not element options here: set them with elementsOptions, or change them later with useCheckoutSession().changeAppearance() / loadFonts().

The Card, split card (number / expiry / CVC), IBAN, AU bank account, Link Authentication, Payment Method Messaging and Issuing elements are not available with Checkout Sessions; rendering one inside this provider sets its error state and logs an error. A VueStripeElements nested inside the provider takes over for its own children.

Express Checkout events under Checkout Sessions

With Checkout Sessions, Express Checkout's @click, @shippingaddresschange and @shippingratechange never fire: Stripe handles them from the session. Confirm from @confirm with useCheckoutSession().confirm({ expressCheckoutConfirmEvent: event }).

Confirming the payment ​

Call confirm() from useCheckoutSession() (or the actions slot prop). When the session was created with return_url, call confirm() without returnUrl: Stripe rejects a confirm that sets both. Pass returnUrl only when the session has no return_url.

ts
const { confirm } = useCheckoutSession()

// Session created with return_url
await confirm()

// Session created without return_url
await confirm({ returnUrl: `${window.location.origin}/complete` })

Provides ​

KeyTypeDescription
checkoutRef<StripeCheckoutElementsSdk | null>The Checkout Elements SDK instance
actionsRef<StripeCheckoutLoadActionsSuccess | null>Session actions, once loadActions() resolves
sessionRef<StripeCheckoutSession | null>Reactive session snapshot (updates on every Stripe change)
loadingRef<boolean>true until the session actions have loaded
errorRef<string | null>Initialization error, if any

Access these via useCheckoutSession().

TypeScript ​

The component's props, event payloads and slot props are typed on Vue 2.7 and Vue 3: vue-tsc checks a handler's parameter against the payload in Events, and a slot prop against Slots. The props type is exported as VueStripeCheckoutElementsProviderProps.

vue
<script setup lang="ts">
import { VueStripeCheckoutElementsProvider, VueStripePaymentElement } from '@vue-stripe/vue-stripe'
import type {
  StripeCheckoutElementsOptions,
  StripeCheckoutLoadActionsSuccess,
  StripeCheckoutSession
} from '@vue-stripe/vue-stripe'

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

const elementsOptions: StripeCheckoutElementsOptions = {
  appearance: { theme: 'stripe' },
  savedPaymentMethod: { enableSave: 'never' }
}

const onReady = (actions: StripeCheckoutLoadActionsSuccess) => {
  console.log(actions.getSession().total.total.amount)
}

const onChange = (session: StripeCheckoutSession) => {
  console.log('Can confirm:', session.canConfirm)
}
</script>

<template>
  <VueStripeCheckoutElementsProvider
    :client-secret="props.clientSecret"
    :elements-options="elementsOptions"
    @ready="onReady"
    @change="onChange"
  >
    <VueStripePaymentElement />
  </VueStripeCheckoutElementsProvider>
</template>

See Also ​