Core Concepts

Checkout

CheckoutProvider and EmbeddedCheckout components

Checkout

Vue Stripe provides two checkout solutions:

  1. CheckoutProvider - For Elements with Checkout Sessions (Embedded Checkout)
  2. EmbeddedCheckout - A pre-built checkout UI that you embed in your application

CheckoutProvider

The CheckoutProvider allows you to use Element components with Checkout Sessions. This is used for Elements with Checkout Sessions integrations.

Basic Usage

<script setup>
import { loadStripe } from '@stripe/stripe-js'
import { CheckoutProvider, PaymentElement, useCheckout } from 'vue-stripe/checkout'

const stripePromise = loadStripe('pk_test_xxx')

function fetchClientSecret() {
  return fetch('/create-checkout-session', { method: 'POST' })
    .then(response => response.json())
    .then(data => data.clientSecret)
}
</script>

<template>
  <CheckoutProvider
    :stripe="stripePromise"
    :options="{ clientSecret: fetchClientSecret }"
  >
    <CheckoutForm />
  </CheckoutProvider>
</template>

Using useCheckout

<script setup>
import { PaymentElement, useCheckout } from 'vue-stripe/checkout'

const checkout = useCheckout()

async function handleSubmit() {
  if (checkout.value?.type === 'loading') {
    return
  }

  if (checkout.value?.type === 'error') {
    console.error(checkout.value.error)
    return
  }

  // checkout.value.type === 'success'
  const { checkout: checkoutInstance } = checkout.value
  const result = await checkoutInstance.confirm()

  if (result.type === 'error') {
    console.error(result.error.message)
  }
  else {
    // Customer will be redirected to return_url
  }
}
</script>

<template>
  <form @submit.prevent="handleSubmit">
    <PaymentElement />
    <button type="submit">
      Submit
    </button>
  </form>
</template>

Props

PropTypeDescription
stripeStripe | PromiseLike<Stripe | null> | nullA Stripe object or Promise resolving to a Stripe object
optionsCheckoutProviderOptionsConfiguration options. See available options. Must provide clientSecret (string or function returning Promise).

EmbeddedCheckout

The EmbeddedCheckout component provides a pre-built checkout UI that you embed directly in your application. It's simpler to use but offers less customization than Elements.

Basic Usage

<script setup>
import { loadStripe } from '@stripe/stripe-js'
import { EmbeddedCheckout, EmbeddedCheckoutProvider } from 'vue-stripe'

const stripePromise = loadStripe('pk_test_xxx')

function fetchClientSecret() {
  return fetch('/create-checkout-session', { method: 'POST' })
    .then(response => response.json())
    .then(data => data.clientSecret)
}
</script>

<template>
  <EmbeddedCheckoutProvider
    :stripe="stripePromise"
    :options="{ fetchClientSecret }"
  >
    <EmbeddedCheckout />
  </EmbeddedCheckoutProvider>
</template>

With Options

<template>
  <EmbeddedCheckoutProvider
    :stripe="stripePromise"
    :options="{
      fetchClientSecret,
      onComplete: () => {
        console.log('Checkout completed')
      },
    }"
  >
    <EmbeddedCheckout />
  </EmbeddedCheckoutProvider>
</template>

Props

EmbeddedCheckoutProvider

PropTypeDescription
stripeStripe | PromiseLike<Stripe | null> | nullA Stripe object or Promise resolving to a Stripe object
optionsEmbeddedCheckoutOptionsConfiguration options. See available options. Must provide fetchClientSecret function.

EmbeddedCheckout

PropTypeDescription
optionsEmbeddedCheckoutElementOptionsOptional configuration for the embedded checkout element. See available options.

Events

EventDescription
@readyTriggered when Embedded Checkout is fully loaded
@completeTriggered when the checkout is completed
@errorTriggered when there is an error

Example: CheckoutProvider with Express Checkout

<script setup>
import { loadStripe } from '@stripe/stripe-js'
import {
  CheckoutProvider,
  ExpressCheckoutElement,
  PaymentElement,
  useCheckout
} from 'vue-stripe/checkout'

const stripePromise = loadStripe('pk_test_xxx')
const checkout = useCheckout()

function fetchClientSecret() {
  return fetch('/create-checkout-session', { method: 'POST' })
    .then(response => response.json())
    .then(data => data.clientSecret)
}
</script>

<template>
  <CheckoutProvider
    :stripe="stripePromise"
    :options="{ clientSecret: fetchClientSecret }"
  >
    <form>
      <ExpressCheckoutElement />
      <PaymentElement />
      <button type="submit">
        Pay
      </button>
    </form>
  </CheckoutProvider>
</template>

Choosing Between CheckoutProvider and EmbeddedCheckout

  • Use CheckoutProvider when you need:
    • Custom layout and styling
    • Integration with your existing forms
    • More control over the checkout flow
    • Use of individual Element components
  • Use EmbeddedCheckout when you need:
    • Quick integration with minimal code
    • Pre-built checkout UI
    • Less customization requirements

For detailed information about Checkout and its usage, refer to the Stripe Checkout documentation.