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_secretof a Checkout Session created on your server withui_mode: 'elements'. - The provider shows its
#loadingslot until the Stripe instance exists, then callsstripe.initCheckoutElementsSdk(), which returns the SDK synchronously, and renders its children at once.@readyfires when the session actions have loaded (loadActions()), and@changeon 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
<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).
<!-- 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
| Prop | Type | Required | Description |
|---|---|---|---|
clientSecret | string | 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 |
elementsOptions | StripeCheckoutElementsOptions | No | Options for every Checkout element: appearance, fonts, loader, savedPaymentMethod, syncAddressCheckbox |
adaptivePricing | { allowed?: boolean } | No | Adaptive Pricing: let customers pay in their local currency |
defaultValues | object | No | Prefill 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
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
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:
emailon a session created withcustomer_email(or acustomerwith an email), which already sets it.phoneNumberon a session created withoutphone_number_collection: { enabled: true }: "You cannot update the phone number unless phone_number_collection.enabled is set totrue."
Events
| Event | Payload | Description |
|---|---|---|
@ready | StripeCheckoutLoadActionsSuccess | Emitted once loadActions() resolves. Payload is the session actions object |
@change | StripeCheckoutSession | Emitted on every Stripe change event with the new session snapshot |
@error | string | Emitted 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
| Error | Cause | Fix |
|---|---|---|
VueStripeProviderError (thrown) | Not inside VueStripeProvider, in an app without createVueStripe() | Wrap the provider in VueStripeProvider |
requires a clientSecret (string or Promise) or fetchClientSecret prop | Neither prop given | Pass one of them |
| The rejection's message | fetchClientSecret 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 secret | The function or the Promise resolved an empty or non-string value, for example undefined when the route answered 500 with a JSON error body | Resolve the session's client_secret |
| Stripe error message | initCheckoutElementsSdk() threw or loadActions() failed: invalid or expired client secret, a session that is not ui_mode: 'elements', or a default the session refuses | Create 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 throughloadActions()and also as its ownUncaught (in promise) IntegrationError. - A
clientSecretPromise that rejects before the provider exists. UnderVueStripeProvider, the provider mounts only once Stripe.js has loaded, so a request that fails fast rejects while nothing handles it. Passfetch-client-secretinstead: the provider calls it once Stripe.js has loaded.
Slots
| Slot | Props | Rendered 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.
<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 Prop | Type | Description |
|---|---|---|
checkout | StripeCheckoutElementsSdk | The Checkout Elements SDK instance |
actions | StripeCheckoutLoadActionsSuccess | null | Session actions (null until loaded) |
session | StripeCheckoutSession | null | Current session snapshot (null until loaded) |
loading | boolean | true 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():
| Component | Created with | Accepted options |
|---|---|---|
| VueStripePaymentElement | createPaymentElement() | layout, paymentMethodOrder, readOnly, terms, fields, wallets, walletOptions |
| VueStripeAddressElement | createBillingAddressElement() / createShippingAddressElement() (picked by options.mode: 'billing' or 'shipping', default 'shipping') | contacts, display, fields |
| VueStripeExpressCheckoutElement | createExpressCheckoutElement() | buttonHeight, buttonTheme, buttonType, layout, paymentMethods, paymentMethodOrder |
| VueStripeTaxIdElement | createTaxIdElement() | All Tax ID options |
| VueStripeContactDetailsElement | createContactDetailsElement() | None |
| VueStripeCurrencySelectorElement | createCurrencySelectorElement() | None |
| VueStripeTermsElement | createTermsElement() (Stripe beta) | None |
| VueStripeLinkSignupElement | createLinkSignupElement() (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.
const { confirm } = useCheckoutSession()
// Session created with return_url
await confirm()
// Session created without return_url
await confirm({ returnUrl: `${window.location.origin}/complete` })Provides
| Key | Type | Description |
|---|---|---|
checkout | Ref<StripeCheckoutElementsSdk | null> | The Checkout Elements SDK instance |
actions | Ref<StripeCheckoutLoadActionsSuccess | null> | Session actions, once loadActions() resolves |
session | Ref<StripeCheckoutSession | null> | Reactive session snapshot (updates on every Stripe change) |
loading | Ref<boolean> | true until the session actions have loaded |
error | Ref<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.
<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
- useCheckoutSession — drive the session from descendants
- Build your own page on a Checkout Session — how-to guide
- VueStripeProvider — required parent
- VueStripeCheckout — hosted Checkout redirect
- Stripe: Elements with Checkout Sessions — official guide
