I'm trying to implement Stripe elements into my rails app, but I can't seem to include the stripe.js correctly. Here is my application.html
我正在尝试将Stripe元素实现到我的rails应用程序中,但我似乎无法正确包含stripe.js。这是我的application.html
<%= tag :meta, name: "stripe-key", content: Figaro.env.stripe_publishable_key %>
<script type="text/javascript" src="https://js.stripe.com/v3/"</script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
JS
var stripe = Stripe($("meta[name='stripe-key']").attr("content"))
var elements = stripe.elements();
var card = elements.create('card', {
style: {
base: {
iconColor: '#999',
color: '#505652',
lineHeight: '40px',
fontWeight: 300,
fontFamily: 'Helvetica Neue',
'::placeholder': {
color: '#CFD7E0',
},
},
}
});
// Add an instance of the card UI component into the `card-element` <div>
card.mount('#card-element');
FORM
<form action="/charge" method="post" id="payment-form">
<div class="form-row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element">
</div>
<div id="card-errors"></div>
</div>
<button>Submit Payment</button>
</form>
Every time I load the page I get this error in the console Uncaught ReferenceError: Stripe is not defined - STRIPE ERROR
. I think it has something the to do with the way I'm loading stripe.js but I'm not sure?
每次我加载页面时,我都会在控制台中收到此错误未捕获的ReferenceError:Stripe未定义 - STRIPE ERROR。我认为它与我加载stripe.js的方式有关,但我不确定?
1 个解决方案
#1
8
I suspect what's happening is that Stripe.js is loading AFTER your own javascript. Try moving Stripe.js above your own javascript in the header.
我怀疑发生了什么是Stripe.js在你自己的javascript之后加载。尝试在标题中将Stripe.js移动到您自己的javascript之上。
#1
8
I suspect what's happening is that Stripe.js is loading AFTER your own javascript. Try moving Stripe.js above your own javascript in the header.
我怀疑发生了什么是Stripe.js在你自己的javascript之后加载。尝试在标题中将Stripe.js移动到您自己的javascript之上。