Skip to content

Accept a payment with the Payment Element ​

Collect a payment with VueStripePaymentElement and confirm it with usePaymentIntent(). The Payment Element shows every payment method you enable in the Stripe Dashboard that fits the customer's location, currency and device.

Before you begin ​

  • Vue Stripe is installed and its stylesheet imported (Install and set up).
  • Your server has an endpoint that creates a PaymentIntent and returns its client_secret.
  • Your app can reach your server's /api routes during development (Send API requests to your server).

1. Create a PaymentIntent on your server ​

ts
// POST /api/create-payment-intent (Node, stripe-node)
import Stripe from 'stripe'

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!)

export async function createPaymentIntent() {
  const paymentIntent = await stripe.paymentIntents.create({
    amount: 2000,
    currency: 'usd',
    automatic_payment_methods: { enabled: true },
    metadata: { orderId: 'order_123' } // your own order ID, read back by the webhook
  })
  return { clientSecret: paymentIntent.client_secret }
}

The webhook that fulfils the order finds it from metadata.orderId, and skips PaymentIntents without one, such as those that Checkout creates (Fulfil orders with webhooks).

2. Load Stripe and create the Elements group ​

The page fetches the client secret, then renders VueStripeProvider and VueStripeElements. The form goes in a child component, because composables find the Elements group through the providers above them.

vue
<!-- PaymentPage.vue -->
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { VueStripeProvider, VueStripeElements } from '@vue-stripe/vue-stripe'
import '@vue-stripe/vue-stripe/style.css' // once, e.g. in main.ts
import PaymentForm from './PaymentForm.vue'

const publishableKey = import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY
const clientSecret = ref('')
const errorMessage = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/create-payment-intent', {
      method: 'POST',
      signal: AbortSignal.timeout(15000) // give up on a request that never answers
    })
    if (!response.ok) throw new Error(`HTTP ${response.status}`)
    clientSecret.value = (await response.json()).clientSecret
  } catch {
    // An error answer, no answer at all (server down, offline) or no answer in time
    errorMessage.value = 'Could not start the payment. Please reload the page.'
  }
})
</script>

<template>
  <p v-if="errorMessage">{{ errorMessage }}</p>
  <VueStripeProvider v-else :publishable-key="publishableKey">
    <VueStripeElements v-if="clientSecret" :client-secret="clientSecret">
      <PaymentForm :client-secret="clientSecret" />
    </VueStripeElements>
  </VueStripeProvider>
</template>

The catch also covers a request that never answers: after 15 seconds AbortSignal.timeout() aborts it and the page shows its message instead of staying empty (Set a time limit on requests that never answer).

3. Render the Payment Element and confirm ​

confirmPayment() runs elements.submit() for you, then confirms with the injected Elements group.

vue
<!-- PaymentForm.vue: rendered inside VueStripeElements -->
<script setup lang="ts">
import { ref } from 'vue'
import { VueStripePaymentElement, usePaymentIntent } from '@vue-stripe/vue-stripe'

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

const { confirmPayment, loading: processing } = usePaymentIntent()
const errorMessage = ref('')
const isComplete = ref(false)

const handleChange = (event: { complete: boolean }) => {
  isComplete.value = event.complete
}

const handleSubmit = async () => {
  errorMessage.value = ''

  const { error } = await confirmPayment({
    clientSecret: props.clientSecret,
    confirmParams: {
      return_url: `${window.location.origin}/payment-complete`
    },
    redirect: 'always'
  })

  // With redirect: 'always', success leaves the page; only errors return here
  if (error) {
    errorMessage.value = error.message || 'Payment failed'
  }
}
</script>

<template>
  <form @submit.prevent="handleSubmit">
    <VueStripePaymentElement @change="handleChange" />
    <p v-if="errorMessage" class="error">{{ errorMessage }}</p>
    <button type="submit" :disabled="processing || !isComplete">
      {{ processing ? 'Processing...' : 'Pay $20.00' }}
    </button>
  </form>
</template>

The error message stays until the customer clicks Pay again; handleChange only tracks whether the form is complete, because Stripe sends a change event right after a decline. The customer is sent to return_url after a successful payment; step 4 builds that page.

When the element loads, Stripe.js logs "Your Elements integration is using an older API" in the browser console. That is expected for a Payment Element on a PaymentIntent, and needs no change; Troubleshoot common problems explains it.

4. Show the result on the return page ​

With redirect: 'always', every successful payment lands on /payment-complete, with payment_intent_client_secret and redirect_status in the query string. Read the result there with CompletePage.vue from Check the result on the return page: it calls retrievePaymentIntent() from usePaymentIntent() and shows the PaymentIntent's status.

The return page is a route of its own, so the provider in PaymentPage.vue is not above it. Give it its own VueStripeProvider; without one, usePaymentIntent() throws a VueStripeProviderError and the page stays blank:

vue
<!-- PaymentComplete.vue: the page at /payment-complete -->
<script setup lang="ts">
import { VueStripeProvider } from '@vue-stripe/vue-stripe'
import CompletePage from './CompletePage.vue'

const publishableKey = import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY
</script>

<template>
  <VueStripeProvider :publishable-key="publishableKey">
    <CompletePage />
  </VueStripeProvider>
</template>

Serve it at the path of return_url, for example with vue-router:

ts
{ path: '/payment-complete', component: () => import('./PaymentComplete.vue') }

With the plugin, render CompletePage.vue directly: the plugin gives it Stripe without a provider. The page only informs the customer; fulfil the order from a webhook.

Stay on the page when no redirect is needed ​

Pass redirect: 'if_required' (the composable's default) to get the result back without leaving the page. Payment methods that need a redirect, such as bank redirects, still go to return_url, so keep it set.

ts
const { error, paymentIntent } = await confirmPayment({
  clientSecret: props.clientSecret,
  confirmParams: {
    return_url: `${window.location.origin}/payment-complete`
  },
  redirect: 'if_required'
})

if (error) {
  errorMessage.value = error.message || 'Payment failed'
} else if (paymentIntent?.status === 'succeeded') {
  // Show your success state
}

Pre-fill customer details ​

Pass known billing details through defaultValues:

vue
<VueStripePaymentElement
  :options="{
    defaultValues: {
      billingDetails: {
        name: customer.name,
        email: customer.email,
        address: { postal_code: customer.zip, country: customer.country }
      }
    }
  }"
/>

To change colors and fonts, pass an appearance object to VueStripeElements; see Style elements and the Appearance API reference.

Test data: see Test your integration.

See also ​