Skip to content

VueStripeProvider

The root component that loads Stripe.js and provides the Stripe instance to all descendant components.

What is StripeProvider?

StripeProvider is the foundation of any Vue Stripe integration. It handles:

CapabilityDescription
Stripe.js LoadingAsynchronously loads Stripe.js from Stripe's CDN
Instance ManagementCreates and manages the Stripe instance lifecycle
Context ProvisionMakes Stripe available to all child components via Vue's provide/inject
Error HandlingCatches and exposes loading errors for graceful degradation

How It Works

┌─────────────────────────────────────────────────────────────┐
│  StripeProvider mounts with publishableKey                  │
└─────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│  Calls loadStripe() from @stripe/stripe-js                  │
│  (Shows #loading slot while waiting)                        │
└─────────────────────────────────────────────────────────────┘

              ┌───────────────┴───────────────┐
              ▼                               ▼
┌─────────────────────────┐     ┌─────────────────────────────┐
│  SUCCESS                │     │  FAILURE                    │
│                         │     │                             │
│  1. Store Stripe in ref │     │  1. Store error message     │
│  2. provide() to tree   │     │  2. Emit @error event       │
│  3. Emit @load event    │     │  3. Show #error slot        │
│  4. Render #default     │     │                             │
└─────────────────────────┘     └─────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│  Child components access Stripe via useStripe() composable  │
│  or inject stripeInjectionKey directly                      │
└─────────────────────────────────────────────────────────────┘

Usage

vue
<template>
  <VueStripeProvider
    :publishable-key="publishableKey"
    @load="onLoad"
    @error="onError"
  >
    <!-- Your payment components -->
  </VueStripeProvider>
</template>

<script setup>
import { VueStripeProvider } from '@vue-stripe/vue-stripe'

const publishableKey = import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY

const onLoad = (stripe) => {
  console.log('Stripe loaded!', stripe)
}

const onError = (error) => {
  console.error('Failed to load Stripe:', error)
}
</script>

Props

PropTypeRequiredDescription
publishableKeystringYes*Your Stripe publishable key
stripeKeystringYes*Alias for publishableKey (backwards compatibility)
stripeAccountstringNoConnected account ID for Stripe Connect
apiVersionstringNoOverride Stripe API version
localestringNoLocale for Stripe.js (e.g., 'en', 'es', 'fr')
optionsobjectNoAdditional Stripe initialization options

* Either publishableKey or stripeKey is required.

Options Object

ts
interface StripeProviderOptions {
  stripeAccount?: string
  apiVersion?: string
  locale?: string
}

Events

EventPayloadDescription
@loadStripeEmitted when Stripe.js loads successfully
@errorErrorEmitted when Stripe.js fails to load

Slots

Default Slot

Rendered when Stripe is ready:

vue
<VueStripeProvider :publishable-key="key">
  <MyPaymentForm />
</VueStripeProvider>

Loading Slot

Rendered while Stripe.js is loading:

vue
<VueStripeProvider :publishable-key="key">
  <template #loading>
    <div class="spinner">Loading payment form...</div>
  </template>

  <MyPaymentForm />
</VueStripeProvider>

Error Slot

Rendered when Stripe.js fails to load:

vue
<VueStripeProvider :publishable-key="key">
  <template #error="{ error }">
    <div class="error">
      Failed to load payment form: {{ error }}
    </div>
  </template>

  <MyPaymentForm />
</VueStripeProvider>

Provides

StripeProvider uses Vue's provide to make these values available to descendants:

KeyTypeDescription
stripeRef<Stripe | null>The Stripe instance
loadingRef<boolean>Whether Stripe is loading
errorRef<string | null>Error message if loading failed

Access these values using the useStripe composable.

Examples

Basic Usage

vue
<VueStripeProvider :publishable-key="pk_test_...">
  <VueStripeElements :client-secret="secret">
    <VueStripePaymentElement />
  </VueStripeElements>
</VueStripeProvider>

With Stripe Connect

vue
<VueStripeProvider
  :publishable-key="pk_test_..."
  stripe-account="acct_1234567890"
>
  <!-- Payments go to connected account -->
</VueStripeProvider>

With Custom Locale

vue
<VueStripeProvider
  :publishable-key="pk_test_..."
  locale="es"
>
  <!-- Stripe Elements will be in Spanish -->
</VueStripeProvider>

With All Slots

vue
<VueStripeProvider
  :publishable-key="publishableKey"
  @load="onStripeLoad"
  @error="onStripeError"
>
  <template #loading>
    <LoadingSpinner message="Connecting to payment provider..." />
  </template>

  <template #error="{ error }">
    <ErrorMessage
      :message="error"
      @retry="retryLoad"
    />
  </template>

  <PaymentForm />
</VueStripeProvider>

Error Handling

StripeProvider handles these error cases:

ErrorCauseSolution
Missing keyNo publishableKey or stripeKey providedProvide a valid key
Invalid keyKey doesn't start with pk_Check your API keys in Stripe Dashboard
Network errorFailed to load Stripe.js from CDNCheck network, use @error to show retry option
vue
<script setup>
const handleError = (error) => {
  if (error.message.includes('network')) {
    // Show retry button
  } else if (error.message.includes('Invalid')) {
    // Configuration error - log to monitoring
  }
}
</script>

TypeScript

ts
import { VueStripeProvider } from '@vue-stripe/vue-stripe'
import type { Stripe } from '@stripe/stripe-js'

const onLoad = (stripe: Stripe) => {
  // stripe is fully typed
}

See Also

Released under the MIT License.