I am trying to use the Stripe API to allow for payment through my website, but I am having issues adding Stripe into my project.
I used the create-react-app
structure for my project, and added Stripe into the index.html
file in /public/index.html
as follows:
<body>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.39.0.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script>
window.AWSCognito = window.AWS
</script>
<script src="https://gitcdn.xyz/repo/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js"></script>
<div id="root"></div>
</body>
However when I test, I get an error:
/src/api/stripe.js
2:5 error 'Stripe' is not defined no-undef
My stripe.js file:
export const getStripeToken = (card) => new Promise((resolve, reject) => {
Stripe.card.createToken(card, (status, {error, id}) => {
if (error) {
reject(error.message);
} else {
resolve(id);
}
});
});
Thanks in advance!
I figured it out! I was being a bit of a dumby.
I changed Stripe
to window.Stripe
in my code, and it works now!
Thanks everyone.
It's only a problem of getting it compiled. The proper way to fix it is to ask compiler to not look for Stripe
and it will be available globally. Add this comment /* global Stripe */
before you initialize Stripe
:
/* global Stripe */
const stripe = Stripe('STRIPE_KEY');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With