javascript 在 Web 上实现 Stripe:未捕获的 ReferenceError:StripeCheckout 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29054049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing Stripe on Web: Uncaught ReferenceError: StripeCheckout is not defined
提问by user1072337
I am trying to implement Stripe payments in my web app. However, when using the sample code I am getting the following javascript error:
我正在尝试在我的网络应用程序中实施 Stripe 支付。但是,在使用示例代码时,我收到以下 javascript 错误:
Uncaught ReferenceError: StripeCheckout is not defined
未捕获的 ReferenceError:StripeCheckout 未定义
The code is like so:
代码是这样的:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script>
Stripe.setPublishableKey('pk_test_HnjFihOWwYTWnnsTLnZTmbgv');
var handler = StripeCheckout.configure({
key: 'pk_test_HnjFihOWwYTWnnsTLnZTmbgv',
image: '/img/documentation/checkout/marketplace.png',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
$('#customButton').on('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Demo Site',
description: '2 widgets',
amount: 2000
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
Any idea why I'm getting the error? Thank you.
知道为什么我收到错误吗?谢谢你。
回答by bhspencer
StripeCheckout is built on top of Stripe. You need to include the js file that defines StripeCheckout.
StripeCheckout 建立在 Stripe 之上。您需要包含定义 StripeCheckout 的 js 文件。
<script src="https://checkout.stripe.com/checkout.js"></script>
See the documentation here: https://stripe.com/docs/checkout#integration-custom
请参阅此处的文档:https: //stripe.com/docs/checkout#integration-custom
回答by Conor
The publishable key line should only be executed once the checkout.js file has been downloaded and your javascript file is compiled, for my project this works
只有在下载了 checkout.js 文件并编译了您的 javascript 文件后,才应执行可发布的关键行,对于我的项目来说这是有效的
<script type="text/javascript" src="https://js.stripe.com/v2/">
$(function(){
Stripe.setPublishableKey('<%= Rails.configuration.stripe[:PUBLISHABLE_KEY] %>');
});
</script>
To test this open a console using developer tools in your browser and after the page has loaded, paste $('#payment-form') if the console returns a reference to the form in your page then its working.
要对此进行测试,请在浏览器中使用开发人员工具打开控制台,并在页面加载后粘贴 $('#payment-form'),如果控制台返回对页面中表单的引用,则其工作正常。
I have put this in the form page in my app and it solved this error, when it was included in my javascript file even wrapped in the on ready event it caused the same error as you experienced
我已经把它放在我的应用程序的表单页面中,它解决了这个错误,当它被包含在我的 javascript 文件中甚至被包裹在准备好的事件中时,它导致了与你遇到的相同的错误