Stripe Checkout (URL Redirect)
Accept one-time payments by redirecting customers to Stripe's hosted checkout page. The server creates the session and returns its URL; Stripe.js removed redirectToCheckout() in Clover (8.0), so the redirect is a plain URL change.
Try the demo
The demo runs in Stripe test mode against the demo backend at backend.vuestripe.com, so nothing is charged. Pick a product, start Checkout, and pay on Stripe's hosted page with the test card 4242 4242 4242 4242, any future expiry date and any CVC. Stripe sends you back to this page, where a banner confirms the payment.
Select a Product
Source code
<script setup lang="ts">
import { ref } from 'vue'
import { VueStripeProvider, VueStripeCheckout, useStripeCheckout } from '@vue-stripe/vue-stripe'
import ProductSelector from './ProductSelector.vue'
import PaymentStatus from './PaymentStatus.vue'
import {
useBackendApi,
type Product,
type CheckoutSessionResult
} from '../../composables/useBackendApi'
import { useStripeConfig } from '../../composables/useStripeConfig'
import DocIcon from '../DocIcon.vue'
const { publishableKey } = useStripeConfig()
const { createCheckoutSession, formatPrice, loading, error } = useBackendApi()
const { redirectToUrl } = useStripeCheckout()
// Step management
const step = ref<'select' | 'checkout' | 'complete'>('select')
// State
const selectedProduct = ref<Product | null>(null)
const sessionResult = ref<CheckoutSessionResult | null>(null)
const redirecting = ref(false)
const sessionError = ref<string | null>(null)
const useComponent = ref(true) // Toggle between direct redirect and VueStripeCheckout component
// Reset function
function reset() {
step.value = 'select'
selectedProduct.value = null
sessionResult.value = null
redirecting.value = false
sessionError.value = null
}
// Handlers
function onProductSelected(product: Product) {
selectedProduct.value = product
sessionResult.value = null
sessionError.value = null
step.value = 'checkout'
}
async function createSession() {
if (!selectedProduct.value) return
try {
sessionError.value = null
// Create checkout session
const result = await createCheckoutSession({
priceId: selectedProduct.value.price.id,
mode: 'payment',
successUrl: `${window.location.href}?result=success`,
cancelUrl: `${window.location.href}?result=cancel`
})
sessionResult.value = result
return result
} catch (err) {
sessionError.value = err instanceof Error ? err.message : 'Failed to create checkout session'
throw err
}
}
// Direct redirect approach: useStripeCheckout() needs no provider or component
async function startCheckoutDirect() {
redirecting.value = true
try {
const result = await createSession()
if (result?.url) {
redirectToUrl(result.url)
}
} catch {
redirecting.value = false
}
}
// For VueStripeCheckout component - create session and let component handle redirect
async function prepareCheckout() {
redirecting.value = true
try {
await createSession()
} catch {
redirecting.value = false
}
}
function onCheckoutError(err: Error) {
sessionError.value = err.message
redirecting.value = false
}
function onBeforeRedirect() {
redirecting.value = true
}
</script>
<template>
<div class="checkout-redirect-example">
<!-- Check for publishable key -->
<div v-if="!publishableKey" class="warning-box">
<strong><DocIcon name="warning" /> Missing Configuration</strong>
<p>Please set <code>VITE_STRIPE_PUBLISHABLE_KEY</code> in your <code>.env</code> file.</p>
</div>
<template v-else>
<!-- Payment Status (shows after redirect from Checkout) -->
<PaymentStatus />
<!-- Progress Steps -->
<div class="progress-steps">
<div
class="progress-step"
:class="{ active: step === 'select', completed: step !== 'select' }"
>
<span class="step-dot">1</span>
<span class="step-label">Product</span>
</div>
<div class="progress-line" :class="{ completed: step !== 'select' }"></div>
<div
class="progress-step"
:class="{ active: step === 'checkout', completed: step === 'complete' }"
>
<span class="step-dot">2</span>
<span class="step-label">Checkout</span>
</div>
<div class="progress-line" :class="{ completed: step === 'complete' }"></div>
<div class="progress-step" :class="{ active: step === 'complete' }">
<span class="step-dot"><DocIcon name="check" :size="16" :stroke-width="3" /></span>
<span class="step-label">Complete</span>
</div>
</div>
<!-- Step 1: Select Product -->
<div v-if="step === 'select'" class="step-content">
<ProductSelector filter="one_time" @select="onProductSelected" />
</div>
<!-- Step 2: Checkout -->
<div v-else-if="step === 'checkout'" class="step-content">
<div class="step-section">
<div class="step-header">
<h3>Checkout with Stripe</h3>
<button class="change-btn" @click="step = 'select'">Change Product</button>
</div>
<!-- Toggle between approaches -->
<div class="approach-toggle">
<button :class="['toggle-btn', { active: useComponent }]" @click="useComponent = true">
VueStripeCheckout Component
</button>
<button
:class="['toggle-btn', { active: !useComponent }]"
@click="useComponent = false"
>
Direct URL Redirect
</button>
</div>
<div class="checkout-content">
<div class="product-summary">
<div class="product-info">
<h4>{{ selectedProduct?.name }}</h4>
<p v-if="selectedProduct?.description">{{ selectedProduct.description }}</p>
</div>
<div class="product-price">
{{
formatPrice(
selectedProduct?.price.amount || 0,
selectedProduct?.price.currency || 'usd'
)
}}
</div>
</div>
<!-- VueStripeCheckout Component Approach -->
<template v-if="useComponent">
<VueStripeProvider :publishable-key="publishableKey">
<!-- First create session, then use VueStripeCheckout -->
<div v-if="!sessionResult">
<button
class="checkout-btn secondary"
:disabled="loading"
@click="prepareCheckout"
>
<span v-if="loading" class="spinner"></span>
{{ loading ? 'Creating Session...' : 'Create Checkout Session' }}
</button>
</div>
<div v-else class="session-ready">
<div class="session-info">
<DocIcon name="circle-check" />
<span
>Session created:
<code>{{ sessionResult.sessionId.slice(0, 20) }}...</code></span
>
</div>
<VueStripeCheckout
:session-url="sessionResult.url ?? undefined"
button-text="Pay with Stripe Checkout"
loading-text="Redirecting..."
button-class="checkout-btn"
@checkout="onBeforeRedirect"
@error="onCheckoutError"
/>
</div>
</VueStripeProvider>
</template>
<!-- Direct Redirect Approach -->
<template v-else>
<button
class="checkout-btn"
:disabled="loading || redirecting"
@click="startCheckoutDirect"
>
<span v-if="loading || redirecting" class="spinner"></span>
{{ redirecting ? 'Redirecting to Stripe...' : 'Pay with Stripe Checkout' }}
</button>
</template>
</div>
<div v-if="sessionError || error" class="error-box">
<strong>Error:</strong> {{ sessionError || error }}
</div>
</div>
</div>
<!-- Step 3: Complete -->
<div v-else-if="step === 'complete'" class="step-content">
<div class="success-state">
<div class="success-icon"><DocIcon name="check" :size="32" :stroke-width="2.5" /></div>
<h3>Checkout Session Created!</h3>
<p v-if="sessionResult">Session ID: {{ sessionResult.sessionId }}</p>
<button class="reset-btn" @click="reset">Start Over</button>
</div>
</div>
</template>
</div>
</template>
<style scoped>
.checkout-redirect-example {
padding: 1.5rem;
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 12px;
}
.warning-box {
padding: 1rem;
background: var(--vp-c-yellow-soft);
border: 1px solid var(--vp-c-yellow-1);
border-radius: 8px;
color: var(--vp-c-yellow-darker);
}
.warning-box strong {
display: block;
margin-bottom: 0.5rem;
}
.warning-box p {
margin: 0;
font-size: 0.875rem;
}
.warning-box code {
padding: 0.125rem 0.375rem;
background: rgba(0, 0, 0, 0.1);
border-radius: 3px;
}
/* Progress Steps */
.progress-steps {
display: flex;
align-items: center;
justify-content: center;
gap: 0;
margin-bottom: 2rem;
padding: 1rem;
}
.progress-step {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
}
.step-dot {
display: flex;
align-items: center;
justify-content: center;
width: 2rem;
height: 2rem;
border-radius: 50%;
background: var(--vp-c-bg-soft);
border: 2px solid var(--vp-c-divider);
font-size: 0.875rem;
font-weight: 600;
color: var(--vp-c-text-2);
transition: all 0.3s;
}
.progress-step.active .step-dot {
background: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
color: white;
}
.progress-step.completed .step-dot {
background: var(--vp-c-green-1);
border-color: var(--vp-c-green-1);
color: white;
}
.step-label {
font-size: 0.75rem;
color: var(--vp-c-text-2);
font-weight: 500;
}
.progress-step.active .step-label {
color: var(--vp-c-brand-1);
}
.progress-step.completed .step-label {
color: var(--vp-c-green-1);
}
.progress-line {
width: 3rem;
height: 2px;
background: var(--vp-c-divider);
margin: 0 0.5rem;
margin-bottom: 1.5rem;
transition: background 0.3s;
}
.progress-line.completed {
background: var(--vp-c-green-1);
}
/* Step Content */
.step-content {
animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.step-section {
padding: 1.5rem;
background: var(--vp-c-bg-soft);
border-radius: 8px;
}
.step-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
}
.step-header h3 {
margin: 0;
font-size: 1rem;
font-weight: 600;
}
.change-btn {
padding: 0.25rem 0.75rem;
background: transparent;
border: 1px solid var(--vp-c-divider);
border-radius: 4px;
font-size: 0.75rem;
color: var(--vp-c-text-2);
cursor: pointer;
transition: all 0.2s;
}
.change-btn:hover {
border-color: var(--vp-c-brand-1);
color: var(--vp-c-brand-1);
}
.approach-toggle {
display: flex;
gap: 0.5rem;
margin-bottom: 1rem;
}
.toggle-btn {
flex: 1;
padding: 0.5rem 1rem;
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
font-size: 0.8125rem;
font-weight: 500;
color: var(--vp-c-text-2);
cursor: pointer;
transition: all 0.2s;
}
.toggle-btn:hover {
border-color: var(--vp-c-brand-1);
}
.toggle-btn.active {
background: var(--vp-c-brand-soft);
border-color: var(--vp-c-brand-1);
color: var(--vp-c-brand-1);
}
.checkout-content {
padding: 1rem;
background: var(--vp-c-bg);
border-radius: 8px;
}
.product-summary {
display: flex;
justify-content: space-between;
align-items: center;
padding-bottom: 1rem;
margin-bottom: 1rem;
border-bottom: 1px solid var(--vp-c-divider);
}
.product-info h4 {
margin: 0 0 0.25rem 0;
font-size: 1rem;
}
.product-info p {
margin: 0;
font-size: 0.875rem;
color: var(--vp-c-text-2);
}
.product-price {
font-size: 1.25rem;
font-weight: 700;
color: var(--vp-c-brand-1);
}
.session-ready {
display: flex;
flex-direction: column;
gap: 1rem;
}
.session-info {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 0.75rem;
background: var(--vp-c-green-soft);
border-radius: 6px;
font-size: 0.875rem;
color: var(--vp-c-green-darker);
}
.session-info code {
padding: 0.125rem 0.375rem;
background: rgba(0, 0, 0, 0.1);
border-radius: 3px;
font-size: 0.75rem;
}
.checkout-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
width: 100%;
padding: 0.875rem 1.5rem;
background: #635bff;
color: white;
border: none;
border-radius: 6px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s;
}
.checkout-btn:hover:not(:disabled) {
opacity: 0.9;
}
.checkout-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.checkout-btn.secondary {
background: var(--vp-c-brand-soft);
color: var(--vp-c-brand-1);
border: 1px solid var(--vp-c-brand-1);
}
.checkout-btn.secondary:hover:not(:disabled) {
background: var(--vp-c-brand-1);
color: white;
}
.spinner {
width: 1rem;
height: 1rem;
border: 2px solid rgba(255, 255, 255, 0.3);
border-top-color: white;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
.error-box {
margin-top: 1rem;
padding: 0.75rem 1rem;
background: var(--vp-c-danger-soft);
border: 1px solid var(--vp-c-danger-1);
border-radius: 6px;
color: var(--vp-c-danger-1);
font-size: 0.875rem;
}
/* Success State */
.success-state {
text-align: center;
padding: 2rem;
}
.success-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 4rem;
height: 4rem;
background: var(--vp-c-green-soft);
border-radius: 50%;
font-size: 2rem;
color: var(--vp-c-green-1);
margin-bottom: 1rem;
}
.success-state h3 {
margin: 0 0 0.5rem 0;
font-size: 1.25rem;
}
.success-state p {
margin: 0 0 1.5rem 0;
color: var(--vp-c-text-2);
font-size: 0.875rem;
}
.reset-btn {
padding: 0.75rem 1.5rem;
background: var(--vp-c-brand-1);
color: white;
border: none;
border-radius: 6px;
font-size: 0.875rem;
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s;
}
.reset-btn:hover {
opacity: 0.9;
}
</style>import { ref, readonly } from 'vue'
import { useStripeConfig } from './useStripeConfig'
// Types
export interface ProductPrice {
id: string
amount: number | null
currency: string
interval: string | null
intervalCount: number | null
}
export interface Product {
id: string
name: string
description: string | null
images: string[]
type: 'one_time' | 'recurring'
price: ProductPrice
metadata: Record<string, string>
}
export interface PaymentIntentResult {
clientSecret: string
paymentIntentId: string
amount: number
currency: string
}
export interface CheckoutSessionResult {
/** Stripe-hosted page URL (`uiMode: 'hosted'`), otherwise null. */
url: string | null
sessionId: string
/** Client secret for `elements`, `embedded_page` and `form` sessions, otherwise null. */
clientSecret: string | null
}
export interface SubscriptionResult {
subscriptionId: string
clientSecret: string | null
status: string
customerId: string
}
export interface SetupIntentResult {
clientSecret: string
setupIntentId: string
}
export interface IdentitySessionResult {
clientSecret: string
verificationSessionId: string
}
/** Error body returned by the backend on non-2xx responses. */
interface ApiErrorResponse {
error?: string
}
interface ProductsResponse {
products?: Product[]
}
/** Parse a JSON response body as the expected backend response shape. */
async function parseJson<T>(response: Response): Promise<T> {
return (await response.json()) as T
}
/** Throw an Error carrying the backend's error message (or the HTTP status). */
async function throwApiError(response: Response): Promise<never> {
const data = await parseJson<ApiErrorResponse>(response).catch((): ApiErrorResponse => ({}))
throw new Error(data.error || `API Error: ${response.status}`)
}
export function useBackendApi() {
const { apiUrl } = useStripeConfig()
const loading = ref(false)
const error = ref<string | null>(null)
/**
* Fetch all available products from the backend
*/
async function getProducts(): Promise<Product[]> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/products`)
if (!response.ok) {
await throwApiError(response)
}
const data = await parseJson<ProductsResponse>(response)
return data.products || []
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to fetch products'
throw err
} finally {
loading.value = false
}
}
/**
* Create a payment intent for a one-time payment
*/
async function createPaymentIntent(
options: {
/** Amount in cents; null/omitted uses the backend default. */
amount?: number | null
currency?: string
priceId?: string
description?: string
metadata?: Record<string, string>
} = {}
): Promise<PaymentIntentResult> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/payment-intent`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options)
})
if (!response.ok) {
await throwApiError(response)
}
return await parseJson<PaymentIntentResult>(response)
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to create payment intent'
throw err
} finally {
loading.value = false
}
}
/**
* Create a regional PaymentIntent (SEPA Direct Debit / BECS / ACH Direct Debit).
* The backend routes share the same request/response shape.
*/
async function createRegionalIntent(
path: 'sepa-debit-intent' | 'becs-intent' | 'ach-debit-intent',
options: {
amount?: number
currency?: string
description?: string
metadata?: Record<string, string>
} = {}
): Promise<PaymentIntentResult> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/${path}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options)
})
if (!response.ok) {
await throwApiError(response)
}
return await parseJson<PaymentIntentResult>(response)
} catch (err) {
error.value = err instanceof Error ? err.message : `Failed to create ${path}`
throw err
} finally {
loading.value = false
}
}
const createSepaIntent = (options: { amount?: number; metadata?: Record<string, string> } = {}) =>
createRegionalIntent('sepa-debit-intent', options)
// BECS Direct Debit (AUD only - Australia)
const createBecsIntent = (
options: { amount?: number; description?: string; metadata?: Record<string, string> } = {}
) => createRegionalIntent('becs-intent', options)
// ACH Direct Debit (USD only - United States), bank account linked with Financial Connections
const createAchIntent = (
options: { amount?: number; description?: string; metadata?: Record<string, string> } = {}
) => createRegionalIntent('ach-debit-intent', options)
/**
* Create a checkout session for Stripe hosted checkout
*/
async function createCheckoutSession(
options: {
amount?: number
currency?: string
priceId?: string
productName?: string
productDescription?: string
quantity?: number
mode?: 'payment' | 'subscription' | 'setup'
successUrl?: string
cancelUrl?: string
customerEmail?: string
metadata?: Record<string, string>
/** `hosted` (default) returns `url`; the other modes return `clientSecret`. */
uiMode?: 'hosted' | 'elements' | 'embedded_page' | 'form'
/** Where Stripe sends the customer after a non-hosted session completes. */
returnUrl?: string
taxIdCollection?: boolean
} = {}
): Promise<CheckoutSessionResult> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/checkout-session`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options)
})
if (!response.ok) {
await throwApiError(response)
}
return await parseJson<CheckoutSessionResult>(response)
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to create checkout session'
throw err
} finally {
loading.value = false
}
}
/**
* Create a subscription
*/
async function createSubscription(options: {
priceId: string
customerEmail: string
paymentMethodId?: string
name?: string
metadata?: Record<string, string>
}): Promise<SubscriptionResult> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/subscription`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options)
})
if (!response.ok) {
await throwApiError(response)
}
return await parseJson<SubscriptionResult>(response)
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to create subscription'
throw err
} finally {
loading.value = false
}
}
/**
* Create a setup intent for saving payment methods
*/
async function createSetupIntent(
options: {
customerId?: string
description?: string
metadata?: Record<string, string>
} = {}
): Promise<SetupIntentResult> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/setup-intent`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options)
})
if (!response.ok) {
await throwApiError(response)
}
return await parseJson<SetupIntentResult>(response)
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to create setup intent'
throw err
} finally {
loading.value = false
}
}
/**
* Create a Stripe Identity VerificationSession (document check)
*/
async function createIdentitySession(): Promise<IdentitySessionResult> {
loading.value = true
error.value = null
try {
const response = await fetch(`${apiUrl.value}/api/identity-session`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({})
})
if (!response.ok) {
await throwApiError(response)
}
return await parseJson<IdentitySessionResult>(response)
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to create verification session'
throw err
} finally {
loading.value = false
}
}
/**
* Format price for display
*/
function formatPrice(amount: number | null, currency: string): string {
if (amount === null) return 'Price not available'
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency.toUpperCase()
}).format(amount / 100)
}
return {
// State
loading: readonly(loading),
error: readonly(error),
apiUrl,
// Methods
getProducts,
createPaymentIntent,
createSepaIntent,
createBecsIntent,
createAchIntent,
createCheckoutSession,
createSubscription,
createSetupIntent,
createIdentitySession,
// Helpers
formatPrice
}
}How the demo works
- The product step loads the demo account's one-time products from the backend. Choosing one shows a summary and a toggle between two ways to redirect.
- Both ways ask the backend to create a Checkout Session with
mode: 'payment'for the product's price. The success and cancel URLs point back to this page with?result=successor?result=cancel. The backend returns the session ID and its URL. - VueStripeCheckout Component: Create Checkout Session creates the session, then
VueStripeCheckoutrenders a button for itssession-url. Clicking the button emitscheckoutand sends the browser to the URL. Errors arrive through@error. - Direct URL Redirect: one click creates the session and calls
redirectToUrl(url)fromuseStripeCheckout(), which needs no provider and no component. - The customer pays on Stripe's page, and Stripe redirects to the success URL. A banner on this page reads
?result=success(or?result=cancelafter canceling).
The redirect does not prove that the payment succeeded. A server confirms the result from Stripe, for example with the checkout.session.completed webhook.
See also
How-to guide: Redirect to Stripe Checkout. Reference: VueStripeCheckout.
Starter app: examples/checkout is a complete Vite + Vue app with hosted and embedded Checkout and its Node server (open in StackBlitz).
