我正在 MAMP 堆栈上运行,试图创建 Stripe Checkout with a custom button测试充电。信用卡验证发生,甚至第二次记住我。此外,我的 Stripe 仪表板上会显示 200 个 POST 日志,但没有收费记录。所以也许表单代码没有与服务器代码对话......
具有自定义“注册”按钮的index.html 具有以下代码:
<script src="bower_components/jquery/dist/jquery.min.js"></script> <!-- In the head -->
...
...
<?php require_once('/php/config.php'); ?>
<form action="/php/charge.php" method="post">
<a href="#" id="stripeButton" class="button">Sign Up</a>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_*************************',
image: 'img/logo.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`
}
});
document.getElementById('stripeButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Awesome Subscription',
description: 'Unlimited Awesomeness',
amount: 2000,
panelLabel: '{{amount}} Per Month'
});
e.preventDefault();
});
</script>
</form>
config.php 文件的以下代码几乎取自 stripe's php tutorial :
<?php
require_once('../stripe/lib/Stripe.php');
$stripe = array(
'secret_key' => 'sk_test_************************',
'publishable_key' => 'pk_test_************************'
);
Stripe::setApiKey($stripe['secret_key']);
?>
在同一个/php/文件夹中,charge.php 文件包含:
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$customer = Stripe_Customer::create(array(
'email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2744525453484a425567425f464a574b420944484a" rel="noreferrer noopener nofollow">[email protected]</a>',
'card' => $token
));
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 2000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $20.00!</h1>';
?>
关于我可能做错了什么有什么想法吗? MAMP 是否足以让index.html 中的代码到达.php 文件并创建费用?我还仔细检查了我的 secret 和可发布的测试 key 是否正确。为什么结账代码不与我的设置中的服务器端通信?
---- 更新 ---- 我收到了 Stripe 支持人员的电子邮件回复,建议我添加
echo('Hello world!');
查看我的 PHP 文件,看看代码是否正在运行。我确实将其添加到 config.php 和 charge.php 文件中,但我没有在任何地方显示“Hello World!”。所以看起来 PHP 文件没有运行。 我还查看了 JavaScript 控制台...并且没有错误。
那么如何让我的 PHP 文件“运行”呢?
最佳答案
已解决。
是的,MAMP 足以使 HTML 与 PHP 文件对话。如果 MAMP 成功“运行”,PHP 应该可以正常工作。 PHP 毕竟是 MAMP 的一部分。
问题是Stripe's custom checkout上提供的 token 功能页面没有附带任何代码来告诉它做什么以及将表单提交到您的 PHP 文件。您应该用自己的代码填充该函数。愚蠢的错误,但我没有以这种方式阅读提供的评论:
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
我遇到了this StackOverflow thread虽然代码似乎有点旧,因为它与 Stripe 现在的自定义结帐页面上的内容不完全匹配,但我使用以下代码的 token 函数内容来填充我的 token 函数并添加它的提交功能.
var token = function(res){
var $input = $('<input type=hidden name=stripeToken />').val(res.id);
$('form').append($input).submit();
};
现在我的index.html(我的注册按钮所在的位置)看起来像这样:
<form action="/php/charge.php" method="post">
<a href="#" id="stripeButton" class="special-button">Sign Up</a>
</form>
...
...
<!-- Just before body tag close -->
<script src="bower_components/jquery/dist/jquery.min.js"></script> <!-- jQuery coming from Zurb Foundation install -->
...
<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_************************',
image: 'img/logo.png',
token: function(token) {
var $input = $('<input type=hidden name=stripeToken />').val(token.id);
$('form').append($input).submit(); <!-- Code pulled from other StackOverflow thread -->
}
});
document.getElementById('stripeButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Product Name',
description: 'Product Description',
amount: 2000,
panelLabel: '{{amount}} Per Month'
});
e.preventDefault();
});
</script>
注意*从旧代码中,res.id
被传递到 .val()
,所以我只是将 res.id
替换为token.id
以反射(reflect) Stripe 在评论中的建议。因此,实现上述代码既可以进行卡验证检查,也可以在我的 Stripe 仪表板上发布客户和费用。表单将我带到 charge.php 以显示收费确认页面。
关于javascript - Stripe 通话已记录,但未产生任何测试费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25712203/