[Vuejs]-Importing stripe to a Vue.js component

3👍

Late to the party, but here is a solution that works for me

Your index.html file looks fine:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script src="https://js.stripe.com/v3/"></script>
    <title>vue-app</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

Then in the component where you want to use it, you can access stripe with the window object, and set the publishable key. This can be done locally in a method, or globally for the component, like this:

<script>    
const stripe = window.Stripe(process.env.VUE_APP_STRIPE_PK)

export default {
  data() {
    return {
      // ...
    }
  },
  methods: {
    async confirmPayment() {
      const paymentResult = await stripe.confirmCardPayment(this.clientSecret, {
        receipt_email: 'email@example.com',
      })
      console.log('paymentResult:', paymentResult)
    },
  },
}
</script>

1👍

I think you should move the script tag of Stripe before the rest of your JavaScript code. The code is probably trying to access the Stripe object before it’s been loaded.

Leave a comment