Skip to content

Tokens Example ​

Turn what the customer entered into something your server can use, without confirming anything in the browser:

  • ConfirmationToken: useCreateConfirmationToken() submits the Payment Element and creates a token; the server then creates and confirms the PaymentIntent with confirmation_token (finalize payments on the server).
  • Token (legacy): useCreateToken() creates a single-use card Token from the Card Element, for Charges and Connect integrations.
  • Radar Session: useRadarSession() captures device signals for Radar when payment details are collected outside Stripe.js.

Try the demo ​

The demo runs in Stripe test mode with the docs' test-mode key. Use the test card 4242 4242 4242 4242, any future expiry date and any CVC. Nothing is charged: each step only creates an object and shows its ID.

Loading Stripe...

Source code ​

The code the demo above runs. Open it in StackBlitz to run and fork it with every file it imports, in Stripe test mode against the demo backend.

View source on GitHub
vue
<script setup lang="ts">
import { VueStripeProvider, VueStripeElements } from '@vue-stripe/vue-stripe'
import ConfirmationTokenPanel from './ConfirmationTokenPanel.vue'
import CardTokenPanel from './CardTokenPanel.vue'
import RadarSessionPanel from './RadarSessionPanel.vue'
import { useStripeConfig } from '../../composables/useStripeConfig'
import DocIcon from '../DocIcon.vue'

const { publishableKey } = useStripeConfig()
</script>

<template>
  <div class="tokens-example">
    <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>

    <VueStripeProvider v-else :publishable-key="publishableKey">
      <section class="demo-section">
        <h3>ConfirmationToken <span class="tag">useCreateConfirmationToken</span></h3>
        <p class="intro">
          The Payment Element runs without an Intent (<code>mode</code>, <code>amount</code> and
          <code>currency</code> on <code>VueStripeElements</code>). The token carries what the
          customer entered; your server creates and confirms the PaymentIntent with it.
        </p>
        <VueStripeElements mode="payment" :amount="1099" currency="usd">
          <ConfirmationTokenPanel />
        </VueStripeElements>
      </section>

      <section class="demo-section">
        <h3>Card Token (legacy) <span class="tag">useCreateToken</span></h3>
        <p class="intro">
          A single-use Token from the Card Element, for Charges or Connect integrations. New
          integrations use a PaymentMethod or a ConfirmationToken instead.
        </p>
        <VueStripeElements>
          <CardTokenPanel />
        </VueStripeElements>
      </section>

      <section class="demo-section">
        <h3>Radar Session <span class="tag">useRadarSession</span></h3>
        <p class="intro">
          Device signals for Radar when you collect payment details outside Stripe.js. Pass the ID
          as <code>radar_options.session</code> on your server.
        </p>
        <RadarSessionPanel />
      </section>
    </VueStripeProvider>
  </div>
</template>

<style scoped>
.tokens-example {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  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;
}
.demo-section {
  padding: 1.5rem;
  background: var(--vp-c-bg-soft);
  border-radius: 8px;
}
.demo-section + .demo-section {
  margin-top: 1.5rem;
}
.demo-section h3 {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-weight: 600;
}
.tag {
  padding: 0.125rem 0.5rem;
  background: var(--vp-c-bg);
  border: 1px solid var(--vp-c-divider);
  border-radius: 4px;
  font-family: var(--vp-font-family-mono);
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--vp-c-text-2);
}
.intro {
  margin: 0 0 1rem;
  font-size: 0.875rem;
  color: var(--vp-c-text-2);
}
.intro code {
  padding: 0.125rem 0.375rem;
  background: var(--vp-c-bg);
  border-radius: 3px;
  font-size: 0.85em;
}
</style>
vue
<script setup lang="ts">
// Rendered inside VueStripeElements (deferred intent: mode, amount, currency) so
// useCreateConfirmationToken can submit the Payment Element and read it.
import { ref } from 'vue'
import { VueStripePaymentElement, useCreateConfirmationToken } from '@vue-stripe/vue-stripe'

const { createConfirmationToken, loading, error } = useCreateConfirmationToken()

const ready = ref(false)
const result = ref<{ id: string; type: string; detail: string } | null>(null)

async function create() {
  result.value = null
  // Runs elements.submit() first, so validation errors land on `error` without a token.
  const { confirmationToken } = await createConfirmationToken({
    params: { return_url: window.location.href }
  })
  if (!confirmationToken) return
  const preview = confirmationToken.payment_method_preview
  result.value = {
    id: confirmationToken.id,
    type: preview.type,
    detail: preview.card ? `${preview.card.brand} ending ${preview.card.last4}` : ''
  }
}
</script>

<template>
  <div class="token-panel">
    <VueStripePaymentElement @ready="ready = true" />
    <button
      :class="['action-btn', { disabled: !ready || loading }]"
      :disabled="!ready || loading"
      data-test="create-confirmation-token"
      @click="create"
    >
      {{ loading ? 'Creating...' : 'Create ConfirmationToken' }}
    </button>
    <dl v-if="result" class="result" data-test="confirmation-token-result">
      <dt>ConfirmationToken</dt>
      <dd>
        <code>{{ result.id }}</code>
      </dd>
      <dt>Payment method</dt>
      <dd>
        {{ result.type }}<template v-if="result.detail">, {{ result.detail }}</template>
      </dd>
    </dl>
    <p v-if="error" class="error-text">{{ error }}</p>
  </div>
</template>

<style scoped src="./token-panel.css"></style>
vue
<script setup lang="ts">
// Rendered inside VueStripeElements so VueStripeCardElement can mount; useCreateToken
// only needs the Stripe instance from VueStripeProvider.
import { ref } from 'vue'
import { VueStripeCardElement, useCreateToken } from '@vue-stripe/vue-stripe'

const cardRef = ref<InstanceType<typeof VueStripeCardElement> | null>(null)
const { createToken, loading, error } = useCreateToken()

const ready = ref(false)
const complete = ref(false)
const result = ref<{ id: string; detail: string } | null>(null)

async function create() {
  const card = cardRef.value?.element
  if (!card) return
  result.value = null
  const { token } = await createToken(card, { name: 'Jenny Rosen' })
  if (!token) return
  result.value = {
    id: token.id,
    detail: token.card ? `${token.card.brand} ending ${token.card.last4}` : token.type
  }
}
</script>

<template>
  <div class="token-panel">
    <VueStripeCardElement
      ref="cardRef"
      @ready="ready = true"
      @change="(event) => (complete = event.complete)"
    />
    <button
      :class="['action-btn', { disabled: !ready || !complete || loading }]"
      :disabled="!ready || !complete || loading"
      data-test="create-token"
      @click="create"
    >
      {{ loading ? 'Creating...' : 'Create Token' }}
    </button>
    <dl v-if="result" class="result" data-test="token-result">
      <dt>Token</dt>
      <dd>
        <code>{{ result.id }}</code>
      </dd>
      <dt>Card</dt>
      <dd>{{ result.detail }}</dd>
    </dl>
    <p v-if="error" class="error-text">{{ error }}</p>
  </div>
</template>

<style scoped src="./token-panel.css"></style>
vue
<script setup lang="ts">
// Rendered inside VueStripeProvider so useRadarSession can inject the Stripe instance.
import { ref } from 'vue'
import { useRadarSession } from '@vue-stripe/vue-stripe'

const { createRadarSession, loading, error } = useRadarSession()
const sessionId = ref<string | null>(null)

async function create() {
  sessionId.value = null
  const { radarSession } = await createRadarSession()
  // Stripe.js types RadarSession as an open record, so read the ID by key.
  if (radarSession) sessionId.value = String(radarSession['id'])
}
</script>

<template>
  <div class="token-panel">
    <button
      :class="['action-btn', { disabled: loading }]"
      :disabled="loading"
      data-test="create-radar-session"
      @click="create"
    >
      {{ loading ? 'Creating...' : 'Create Radar Session' }}
    </button>
    <dl v-if="sessionId" class="result" data-test="radar-session-result">
      <dt>Radar Session</dt>
      <dd>
        <code>{{ sessionId }}</code>
      </dd>
    </dl>
    <p v-if="error" class="error-text">{{ error }}</p>
  </div>
</template>

<style scoped src="./token-panel.css"></style>

How the demo works ​

One VueStripeProvider wraps three independent panels. None of them calls the demo backend.

  1. ConfirmationToken: VueStripeElements gets mode="payment", :amount="1099" and currency="usd" instead of an Intent, and holds VueStripePaymentElement. Create ConfirmationToken calls createConfirmationToken({ params: { return_url } }) from useCreateConfirmationToken(), which runs elements.submit() first and then creates the token. The demo shows the token ID and the payment method type, brand and last four digits from payment_method_preview.
  2. Card Token: a second VueStripeElements group holds VueStripeCardElement. Create Token calls createToken(cardElement, { name: 'Jenny Rosen' }) from useCreateToken(), and the demo shows the Token ID with the card brand and last four digits.
  3. Radar Session: Create Radar Session calls createRadarSession() from useRadarSession(), which only needs the Stripe instance. The demo shows the Radar Session ID.

A server would use these IDs: the ConfirmationToken as confirmation_token when it creates and confirms a PaymentIntent, the Token as the payment source of a charge, and the Radar Session as radar_options.session.