Bit*_*ise 7 javascript ruby-on-rails stripe-payments
我正在尝试将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>
Run Code Online (Sandbox Code Playgroud)
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');
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
每次加载页面时,我都会在控制台中收到此错误消息Uncaught ReferenceError: Stripe is not defined - STRIPE ERROR.我认为它与我加载stripe.js的方式有关,但我不确定?
Stripe.js 现在是在所有其他脚本之后加载的,这意味着它不再立即可用。我已将脚本从 body 标签移至 head 标签。
<head>
<script src="https://js.stripe.com/v3/"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
可能会迟到,但如果其他人也遇到同样的问题,只需在您的中添加以下内容即可
<HEAD></HEAD>
<script src="https://js.stripe.com/v2/"></script>
Run Code Online (Sandbox Code Playgroud)
了解他们建议尽快迁移到 v3。
让我困惑的是他们的文档中有教程,其中他们跳过了 Stripe 库的初始化。因此,如果您从这些教程之一开始并尝试他们的示例代码,它是行不通的。
相反,您需要在脚本中的某个位置添加此行:
var stripe = Stripe("your_stripe_publishable_key");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10823 次 |
| 最近记录: |