I am currently working on integrating Stripe with my Vue.js 2 application. To comply with PCI-DSS requirements, Stripe mandates that their Javascript must always be loaded from js.stripe.com. I have followed the guidelines provided in:
- How to add external JS scripts to VueJS Components
- How to include a CDN to VueJS CLI without NPM or Webpack?
However, upon implementation, I encounter an error stating 'Stripe' is not defined
when trying to use the library. It seems like these solutions are focused on merely inserting a <script>
tag into the output HTML (e.g., for analytics) rather than actually utilizing the functions and objects within that script.
This is how my component's JavaScript code looks:
<script>
export default {
name: "PaymentPage",
mounted() {
let stripeScript = document.createElement('script');
stripeScript.setAttribute('src', 'https://js.stripe.com/v3/');
document.head.appendChild(stripeScript);
let s = Stripe('pk_test_Fooo');
console.log(s);
}
}
</script>
Even attempting to insert the script tag into my public/index.html
file results in the same issue. Ideally, this would be my preferred method as Stripe recommends developers to import their script on all pages on the site.
<!DOCTYPE html>
<html lang="en">
<head>
// ...
<script src="https://js.stripe.com/v3/"></script>
</head>
Is there a way to fetch a script from an external CDN and effectively incorporate it into my component's JavaScript?
Although I am aware of some libraries that facilitate the integration of Vue.js with Stripe (such as matfish2/vue-stripe and jofftiquez/vue-stripe-checkout), I have encountered difficulties with them - matfish2/vue-stripe does not import correctly due to issue #24, while jofftiquez/vue-stripe-checkout is designed for the older Stripe API, with the new version still in beta.