|
| 1 | +# Vue Use Stripe |
| 2 | + |
| 3 | +This is a thin Vue 3 wrapper (0.7 KB gzipped) for Stripe.js written in TypeScript. It simply provides a function (Vue hook) to create Stripe elements and a component that conveniently emits events. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add Stripe.js to `index.html` as recommended by Stripe: |
| 8 | + |
| 9 | +```html |
| 10 | +<script async src="https://js.stripe.com/v3/"></script> |
| 11 | +``` |
| 12 | + |
| 13 | +Alternatively, [install `@stripe/stripe-js`](https://github.com/stripe/stripe-js) and import it in your project to automatically add the previous script tag as a side effect: |
| 14 | + |
| 15 | +```js |
| 16 | +// main.js |
| 17 | +import '@stripe/stripe-js' |
| 18 | +``` |
| 19 | + |
| 20 | +Install this wrapper: |
| 21 | + |
| 22 | +```bash |
| 23 | +yarn add vue-use-stripe |
| 24 | +``` |
| 25 | + |
| 26 | +If you are using TypeScript, make sure you also install the mentioned `@stripe/stripe-js` library as well to get proper types for Stripe. Note that, if you are adding the script tag direclty to `index.html`, then `@stripe/stripe-js` can be installed as a **dev dependency** (it will only be used for types, not bundled in your app). |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +```ts |
| 31 | +import { defineComponent, ref } from 'vue' |
| 32 | +import { useStripe, StripeElement } from 'vue-use-stripe' |
| 33 | + |
| 34 | +export default defineComponent({ |
| 35 | + components: { StripeElement }, |
| 36 | + setup() { |
| 37 | + const event = ref({}) |
| 38 | + |
| 39 | + const { |
| 40 | + stripe, |
| 41 | + elements: [cardElement], |
| 42 | + } = useStripe({ |
| 43 | + key: process.env.VUE_APP_STRIPE_PUBLIC_KEY || '', |
| 44 | + elements: [{ type: 'card', options: {} }], |
| 45 | + }) |
| 46 | + |
| 47 | + const registerCard = () => { |
| 48 | + if (event.complete) { |
| 49 | + // Refer to the official docs to see all the Stripe instance properties. |
| 50 | + // E.g. https://stripe.com/docs/js/setup_intents/confirm_card_setup |
| 51 | + return stripe.value.confirmCardSetup('<client-secret>', { |
| 52 | + payment_method: { |
| 53 | + card: cardElement.value, |
| 54 | + }, |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return { |
| 60 | + event, |
| 61 | + cardElement, |
| 62 | + registerCard, |
| 63 | + } |
| 64 | + }, |
| 65 | +}) |
| 66 | +``` |
| 67 | + |
| 68 | +```html |
| 69 | +<template> |
| 70 | + <StripeElement :element="cardElement" @change="event = $event" /> |
| 71 | + <button @click="registerCard">Add</button> |
| 72 | + <div v-if="event && event.error">{{ event.error.message }}</div> |
| 73 | +</template> |
| 74 | +``` |
| 75 | + |
| 76 | +### API |
| 77 | + |
| 78 | +```ts |
| 79 | +useStripe(options: StripeOptions): { |
| 80 | + // Reactive reference to the Stripe instance (created using `window.Stripe`) with proper typings |
| 81 | + stripe: Ref<Stripe | null>; |
| 82 | + |
| 83 | + // Reactive reference to the internal elements instance (Stripe.elements(...)). |
| 84 | + // This allows creating Stripe elements manually (optional): |
| 85 | + // stripeElements.create('card', { <options> }) |
| 86 | + stripeElements: Ref<StripeElements | null>; |
| 87 | + |
| 88 | + // Array of elements created out of `StripeOptions.elements` array |
| 89 | + elements: Ref<any>[]; |
| 90 | +} |
| 91 | + |
| 92 | +type StripeOptions = { |
| 93 | + // Stripe API key |
| 94 | + key: string; |
| 95 | + |
| 96 | + // Array of elements to be created |
| 97 | + // See the following link for possible types and options: |
| 98 | + // https://stripe.com/docs/js/elements_object/create_element?type=card |
| 99 | + // E.g. `[{ type: 'card', options: { hidePostalCode: true } }, { type: 'fpxBank' }, ...] |
| 100 | + elements?: { type: string; options: object }[]; |
| 101 | + |
| 102 | + // Stripe constructor options: https://stripe.com/docs/js/initializing |
| 103 | + constructorOptions?: object; |
| 104 | + |
| 105 | + // Elements constructor options: https://stripe.com/docs/js/elements_object/create |
| 106 | + elementsOptions?: object; |
| 107 | +} |
| 108 | +``` |
| 109 | +
|
| 110 | +Note: `StripeOptions.elements` array is optional. Alternatively, create elements manually using the returned `stripeElements`. |
| 111 | +
|
| 112 | +The `<StripeElement />` component will emit any event created by the internal element: `change`, `ready`, `click`, `focus`, `blur`. |
0 commit comments