我尝试使用Stripe的新结帐功能和自定义按钮,通过AJAX POST将令牌发送到php文件,然后执行收费。不幸的是,我在从POST变量中检索令牌时遇到了一些麻烦。我希望有人能够告诉我我过度复杂的事情,以及是否有更简单的方法可以做到这一点。
在客户端,我有5个不同可能的捐赠"捐款"。这是迄今为止为此编写的js(不包括html):
$(function() {
var donationAmt = '';
var handler = StripeCheckout.configure({
key: 'pk_test_3plF76arhkygGMgwCEerThpa',
image: '/square-image.png',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
console.log(token)
var chargeData = {
donationAmt: donationAmt,
token: token
}
$.ajax({
url: '/link/to/php/stripeDonate.php',
type: 'post',
data: {chargeData: chargeData},
success: function(data) {
if (data == 'success') {
console.log("Card successfully charged!")
}
else {
console.log("Success Error!")
}
},
error: function(data) {
console.log("Ajax Error!");
console.log(data);
}
}); // end ajax call
}
});
$('.donate-button').bind('click', function(e) {
donationAmt = $(this).html().substring(1) + '00';
donationAmt = parseInt(donationAmt); // Grabs the donation amount in the html of the button and store it in a variable
// Open Checkout with further options
handler.open({
name: 'Company Name',
description: 'A donation',
amount: donationAmt
});
e.preventDefault();
});
});
这是我处理AJAX POST调用的php:
<?php
require_once('Stripe.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account
Stripe::setApiKey("sk_test_APIKEYREDACTED");
// Get the credit card details submitted by the form
$token = json_decode($_POST['chargeData']);
$tokenid = $token['id'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = Stripe_Charge::create(array(
"amount" => 2000, // amount in cents, again
"currency" => "usd",
"card" => $tokenid,
"description" => "payinguser@example.com")
);
echo 'success';
} catch(Stripe_CardError $e) {
// The card has been declined
echo $tokenid;
}
?>
php错误日志中提到的此代码的直接结果是令牌的POST变量不能读取&#34;读取&#34;。令牌正在创建好(我看到它在控制台上登录)但是当我通过AJAX发送它时它就消失了。
每个人都说Stripe非常容易实现,所以我真的觉得我在这里遗漏了一些东西。有人能够解决一些问题吗?
谢谢!
答案 0 :(得分:25)
所以经过10个小时的午睡和更清晰的头脑后,我决定以稍微不同的方式解决这个问题。这适用于任何偶然遇到同样问题的人,并希望能够作为条带/ ajax / php教程工作得非常好。结果我一直在考虑POST数据都错了。即使使用AJAX,您也需要一个键和值对来发送任何类型的POST数据。我为此记录了这部分js:
var handler = StripeCheckout.configure({
key: 'PUBLISHABLEKEY',
image: '/square-image.png',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
console.log(token)
$.ajax({
url: 'link/to/php/stripeDonate.php',
type: 'post',
data: {tokenid: token.id, email: token.email, donationAmt: donationAmt},
success: function(data) {
if (data == 'success') {
console.log("Card successfully charged!");
}
else {
console.log("Success Error!");
}
},
error: function(data) {
console.log("Ajax Error!");
console.log(data);
}
}); // end ajax call
}
});
请注意,一个主要更改是ajax方法的data属性。记录令牌对象的控制台显示整个JSON令牌对象,您可以使用该对象提取ID(您的服务器需要发送到条带以收取付款的内容)以及电子邮件(用于记录目的)。由于我有不同的捐赠金额,我也将其作为第三个密钥包括在内。
现在在你的php中,为了获得这些POST变量并将它们放入php变量中,你可以使用它们各自的键来获取它们:
$tokenid = $_POST['tokenid'];
$donation = $_POST['donationAmt'];
$email = $_POST['email'];
然后其他一切都应该是自我解释的(跟几乎与条纹php教程完全相同的例子)。
无论如何,希望这有助于那里的人。祝你好运!