A Vue 2 component collection for StripeElements

Overview

Vue Stripe Elements

A Vue 2 component collection for stripe elements.

Usage example

Install package:

$ npm i vue-stripe-elements-plus --save

Add Stripe.js library to index.html:

<script src="https://js.stripe.com/v3/"></script>

Build a Vue component using the Card element:

<template>
  <div id='app'>
    <h1>Please give us your payment details:</h1>
    <card class='stripe-card'
      :class='{ complete }'
      stripe='pk_test_XXXXXXXXXXXXXXXXXXXXXXXX'
      :options='stripeOptions'
      @change='complete = $event.complete'
    />
    <button class='pay-with-stripe' @click='pay' :disabled='!complete'>Pay with credit card</button>
  </div>
</template>

<script>
import { stripeKey, stripeOptions } from './stripeConfig.json'
import { Card, createToken } from 'vue-stripe-elements-plus'

export default {
  data () {
    return {
      complete: false,
      stripeOptions: {
        // see https://stripe.com/docs/stripe.js#element-options for details
      }
    }
  },

  components: { Card },

  methods: {
    pay () {
      // createToken returns a Promise which resolves in a result object with
      // either a token or an error key.
      // See https://stripe.com/docs/api#tokens for the token object.
      // See https://stripe.com/docs/api#errors for the error object.
      // More general https://stripe.com/docs/stripe.js#stripe-create-token.
      createToken().then(data => console.log(data.token))
    }
  }
}
</script>

<style>
.stripe-card {
  width: 300px;
  border: 1px solid grey;
}
.stripe-card.complete {
  border-color: green;
}
</style>

Multiple elements

Even if it is recommended to use the unified Card element, single elements for each field are supported. The following example shows a possible use in a credit card component:

<template>
  <div class='credit-card-inputs' :class='{ complete }'>
    <card-number class='stripe-element card-number'
      ref='cardNumber'
      :stripe='stripe'
      :options='options'
      @change='number = $event.complete'
    />
    <card-expiry class='stripe-element card-expiry'
      ref='cardExpiry'
      :stripe='stripe'
      :options='options'
      @change='expiry = $event.complete'
    />
    <card-cvc class='stripe-element card-cvc'
      ref='cardCvc'
      :stripe='stripe'
      :options='options'
      @change='cvc = $event.complete'
    />
  </div>
</template>

<script>
import { CardNumber, CardExpiry, CardCvc } from 'vue-stripe-elements-plus'

export default {
  props: [ 'stripe', 'options' ],
  data () {
    return {
      complete: false,
      number: false,
      expiry: false,
      cvc: false
    }
  },
  components: { CardNumber, CardExpiry, CardCvc },
  methods: {
    update () {
      this.complete = this.number && this.expiry && this.cvc

      // field completed, find field to focus next
      if (this.number) {
        if (!this.expiry) {
          this.$refs.cardExpiry.focus()
        } else if (!this.cvc) {
          this.$refs.cardCvc.focus()
        }
      } else if (this.expiry) {
        if (!this.cvc) {
          this.$refs.cardCvc.focus()
        } else if (!this.number) {
          this.$refs.cardNumber.focus()
        }
      }
      // no focus magic for the CVC field as it gets complete with three
      // numbers, but can also have four
    }
  },
  watch: {
    number () { this.update() },
    expiry () { this.update() },
    cvc () { this.update() }
  }
}
</script>

<style>
.credit-card-inputs.complete {
  border: 2px solid green;
}
</style>

Supported Stripe Elements Functions

Function Reference
createToken() https://stripe.com/docs/stripe-js/reference#stripe-create-token
createSource() https://stripe.com/docs/stripe-js/reference#stripe-create-source
retrieveSource() https://stripe.com/docs/stripe-js/reference#stripe-retrieve-source
paymentRequest() https://stripe.com/docs/stripe-js/reference#stripe-payment-request
redirectToCheckout() https://stripe.com/docs/stripe-js/reference#stripe-redirect-to-checkout
retrievePaymentIntent() https://stripe.com/docs/stripe-js/reference#stripe-retrieve-payment-intent
handleCardPayment() https://stripe.com/docs/stripe-js/reference#stripe-handle-card-payment
handleCardSetup() https://stripe.com/docs/stripe-js/reference#stripe-handle-card-setup
handleCardAction() https://stripe.com/docs/stripe-js/reference#stripe-handle-card-action
confirmPaymentIntent() https://stripe.com/docs/stripe-js/reference#stripe-confirm-payment-intent
createPaymentMethod() https://stripe.com/docs/stripe-js/reference#stripe-create-payment-method
You might also like...
🔥 Vue coverflow component(vue)

Introduction A vue2 hotzone component Demo | 案例 Install npm i vue-hotzone --save # or yarn add vue-hotzone Use // Use in component import hotzone from

Keystone6-documentrenderer-vue - A Vue version of the DocumentReader component for Keystone 6

keystone6-documentreader-vue This is a Vue version of the DocumentReader compone

The @obewds/vue-el modular design system component for Vue.js

The @obewds/vue-el modular design system component for Vue.js

Vue component generator :)

Vue Component Generator vue.js component generator for beginner See Demo Build Setup # install dependencies npm install # serve with hot reload at lo

Yandex.Metrica component for Vue.js

Yandex.Metrica component for Vue.js Vue.js v 2.x used Installation npm i vue-ya-metrica --save Usage import VueYaMetrica from 'vue-ya-metrica' compo

A Vue component to use native sharing mechanism of the device as part of the Web Share API.

vue-navigator-share A Vue component to use native sharing mechanism of the device as part of the Web Share API. Support only https and mobile browser

Vue 3 component that let you embed tweets in your App by only giving the tweet id

vue-tweet Vue 3 component that let you embed tweets in your App by only giving the tweet id This project was developed using Vue 3 + Typescript + Vite

This repo contains a VueJS component for embedding a Twitter Feed on a Vue WebApp
This repo contains a VueJS component for embedding a Twitter Feed on a Vue WebApp

VueJSTwitterFeed This repo contains a VueJS component for embedding a Twitter Feed on a Vue WebApp Installation Add to your node project using: npm in

A responsive and configurable Marmoset Viewer component for Vue.

A responsive and configurable Marmoset Viewer component for Vue.

Releases(v0.3.4)
Owner
null
This helps you accept payment using Korapay’s Collection Modal in your Vue app

Korapay for Vue 2.x This helps you accept payment using Korapay’s Collection Modal in your Vue app Install Vue Install the npm package: npm install --

Oluwole Adebiyi 8 Jun 6, 2021
🌕 Create beutiful docs in markdown and HTML from postman collection

Afterman Export beautiful docs in Markdown and HTML from postman collection ?? No Backend ?? No configuration ?? Customize the output of your document

Albert E. Hidalgo Taveras 41 Dec 14, 2022
A collection of ideas and projects that contain from beginner to advance 🎯🚀

IdeaHub A collection of ideas and projects that contain from beginner to advance ?? ?? Categories ✨ Desktop Application Basic Intermediate Game Basic

Make Contributions 113 Jan 2, 2023
A collection of anime image search engines, based on Cloudflare Pages Function

A collection of anime image search engines, based on Cloudflare Pages Function

Mix Technology 58 Dec 28, 2022
A Directus v9 Interface for generating a human-readable ID for a text or string field in a collection

Directus Extension: dwaring87-interface-human-readable-id A Directus v9 Interface for generating a human-readable ID for a text or string field in a c

David Waring 4 Oct 27, 2022
Collection of super strict configurations for ESLint / StyleLint and other code quality tools.

Common Linting Configurations Collection of shareable configurations for commonly used code quality tools. Available in many different flavours to sup

Michal 9 Jan 24, 2023
A vue component or component wrapper that makes an element movable and its movements can be customized.

v-movable A vue component or component wrapper that makes an element movable and its movements can be customized. Live Demo Installation npm i --save

Ron 20 Jul 9, 2022
Qr-code-component - QR code component Built With Vue.js

Frontend Mentor - QR code component Welcome! ?? Thanks for checking out this fro

Oskar Straszyński 1 Apr 1, 2022
Simple portal component which allows to mount component anywhere.

vue-simple-portal Simple portal component which allows to mount component anywhere. This component only does one thing that already existed in portal-

EGOIST 6 Dec 1, 2019
A beautiful chat rooms component made with Vue.js - compatible with Vue, React & Angular

A beautiful chat rooms component made with Vue.js - compatible with Vue, React & Angular

Antoine Dupont 1.1k Jan 7, 2023