javascript - 用条纹充电
问题描述
我在使用 Stripe 进行充电时遇到了一些问题。Composer 不会安装,所以我手动加载了它。没有PHP
错误,我的令牌创建工作正常。我看不到任何明显的错误或语法错误,有人能指出我正确的方向吗?
谢谢
index.html 代码:
<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
var handler = StripeCheckout.configure({
key: 'xxxxxxxxxx',
image: 'https://stripe.com/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`
Pass along various parameters you get from the token response
and your form.*/
var myData = {
token: token.id,
email: token.email,
amount: 500,
message: $("#message").val()
};
/* Make an AJAX post request using JQuery,
change the first parameter to your charge script*/
$.post("charge2.php", myData,function (data) {
// if you get some results back update results
$(".results").html("Your charge was successful");
}).fail(function () {
// if things fail, tell us
$(".results").html("I'm sorry something went wrong");
})
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'GUTIC',
description: 'Join today!',
amount: 500
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function () {
handler.close();
});
</script>
收费2.php
<?php echo // Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
require_once('stripe-php/init.php');
\Stripe\Stripe::setApiKey("pk_test_DapJwVUCol6JDjJ4jsqEr6S7");
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['token'];
$charge = \Stripe\Charge::create([
'amount' => 999,
'currency' => 'usd',
'description' => 'Example charge',
'source' => $token,
print_r($_POST)
]);; ?>
解决方案
看起来问题在于您pk_test_xxx
在调用中使用了可发布的密钥()\Stripe\Stripe::setApiKey
但是,您应该在此处使用您的密钥(sk_test_xxx
),因为创建费用需要使用密钥。您可以在https://dashboard.stripe.com/account/apikeys获取您的密钥
使用错误的密钥应该会给您一个带有 403 响应代码的 API 错误。您可以在 Stripe仪表板日志中看到这一点。您可能需要在测试和实时模式之间切换(使用左侧导航栏中的开关)才能看到相关请求。
此外,在代码中,print_r
语句在\Stripe\Charge::create
调用的参数内,这也会给你一个错误——相反,它应该在外面,很可能就setApiKey
在行之后。
推荐阅读
- node.js - 在 Koa 中检测客户端断开连接
- reactjs - React Contenteditable 不关注 Ref
- cookies - Chrome 或任何其他浏览器如何使用公共后缀列表?
- python - 熊猫使用数据框中的两列过滤器为变量赋值
- javascript - 三个js点击HTML叠加后如何取消鼠标点击
- javascript - 在 Bootstrap 中从列表中选择多个项目
- python-3.x - 如何裁剪矩形或正方形内的图像区域?
- swift - Realm Swift - JSON 中的嵌套列表
- r - 我正在尝试使用 xlsx 包将命名范围从 Excel 工作表带到 R
- visual-studio - 如何限制打开文档的数量并自动关闭 Visual Studio 中最旧的文档?