Create Payment Method
Tokenize card details into a reusable PaymentMethod — without confirming a payment — using the useCreatePaymentMethod composable. Useful for collecting a card now and charging it later server-side.
Try the demo
The demo uses the docs' Stripe test-mode key and takes no payment. Enter the test card 4242 4242 4242 4242, any future expiry date and any CVC, then click Create Payment Method. The demo shows the new PaymentMethod ID.
useCreatePaymentMethod() tokenizes card details into a reusable PaymentMethod without confirming a payment. Enter a test card (4242 4242 4242 4242) and create one.
Source code
<script setup lang="ts">
import { defineComponent, h, ref, shallowRef } from 'vue'
import {
VueStripeProvider,
VueStripeElements,
VueStripeCardElement,
useCreatePaymentMethod,
type StripeCardElement
} from '@vue-stripe/vue-stripe'
import { useStripeConfig } from '../../composables/useStripeConfig'
import DocIcon from '../DocIcon.vue'
const { publishableKey } = useStripeConfig()
// Inner component (must be inside the provider to use the composable).
const Demo = defineComponent({
name: 'CreatePmDemo',
setup() {
const { createPaymentMethod, loading, error } = useCreatePaymentMethod()
// The card element instance, handed over by the component's `ready` event.
const card = shallowRef<StripeCardElement | null>(null)
const cardReady = ref(false)
const result = ref<{ ok: boolean; text: string } | null>(null)
async function onCreate() {
result.value = null
const { paymentMethod, error: createError } = await createPaymentMethod(
card.value ? { type: 'card', card: card.value } : {}
)
result.value = paymentMethod
? { ok: true, text: paymentMethod.id }
: { ok: false, text: createError?.message || 'failed' }
}
return () =>
h('div', { class: 'cpm-demo' }, [
h('div', { class: 'card-wrap' }, [
h(VueStripeCardElement, {
onReady: (element) => {
card.value = element
cardReady.value = true
}
})
]),
h(
'button',
{
class: ['btn', { disabled: !cardReady.value || loading.value }],
disabled: !cardReady.value || loading.value,
onClick: onCreate
},
loading.value ? 'Creating…' : 'Create Payment Method'
),
result.value
? h('code', { class: ['result', { ok: result.value.ok }] }, [
h(DocIcon, { name: result.value.ok ? 'circle-check' : 'circle-x' }),
' ',
result.value.text
])
: null,
error.value ? h('p', { class: 'err' }, error.value) : null
])
}
})
</script>
<template>
<div class="cpm-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>
<template v-else>
<p class="intro">
<code>useCreatePaymentMethod()</code> tokenizes card details into a reusable
<code>PaymentMethod</code> without confirming a payment. Enter a test card (<code
>4242 4242 4242 4242</code
>) and create one.
</p>
<div class="stripe-container">
<VueStripeProvider :publishable-key="publishableKey">
<VueStripeElements>
<component :is="Demo" />
</VueStripeElements>
</VueStripeProvider>
</div>
</template>
</div>
</template>
<style scoped>
.cpm-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;
}
.intro {
margin: 0 0 1.25rem;
font-size: 0.9rem;
color: var(--vp-c-text-2);
}
.intro code {
padding: 0.125rem 0.375rem;
background: var(--vp-c-bg-soft);
border-radius: 3px;
font-size: 0.85em;
}
.stripe-container {
padding: 1.5rem;
background: var(--vp-c-bg-soft);
border-radius: 8px;
}
.cpm-demo {
display: flex;
flex-direction: column;
gap: 1rem;
}
/* Every element below is rendered by the inner Demo component, so this SFC's
scoped styles only reach them via :deep(). (--vp-c-bg is white in light /
near-black in dark, replacing the old white + :root.dark override.) */
:deep(.card-wrap) {
padding: 0.75rem;
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
}
:deep(.btn) {
padding: 0.75rem 1.25rem;
background: #635bff;
color: white;
border: none;
border-radius: 6px;
font-size: 0.95rem;
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s;
}
:deep(.btn:hover:not(.disabled)) {
opacity: 0.9;
}
:deep(.btn.disabled) {
opacity: 0.6;
cursor: not-allowed;
}
:deep(.result) {
padding: 0.625rem 0.875rem;
background: var(--vp-c-bg);
border-radius: 6px;
font-size: 0.8rem;
color: var(--vp-c-danger-1);
}
:deep(.result.ok) {
color: var(--vp-c-green-darker);
background: var(--vp-c-green-soft);
}
:deep(.err) {
margin: 0;
color: var(--vp-c-danger-1);
font-size: 0.85rem;
}
</style>How the demo works
- Nothing is sent to the demo backend.
VueStripeProviderloads Stripe.js andVueStripeElementscreates an Elements group without an Intent. VueStripeCardElementmounts and emitsreadywith the element instance. The demo keeps that instance and enables the button.- The button calls
createPaymentMethod({ type: 'card', card })fromuseCreatePaymentMethod(). Stripe.js sends the card details from its iframe to Stripe and returns a PaymentMethod; the card number never reaches the page's own code. - The demo shows the PaymentMethod ID, or Stripe's error message, for example for an incomplete card number. While the request runs, the composable's
loadingkeeps the button disabled.
A server would then attach the PaymentMethod to a Customer or use it in a PaymentIntent. This demo stops after creating it.
See also
How-to guide: Confirm payments on your server. Reference: useCreatePaymentMethod.
