【发布时间】:2016-09-25 11:39:35
【问题描述】:
我有一个应用程序使用 ajax 调用条带 SDK 库中的函数。
第一次向客户收费的电话没有任何问题。
收费客户的服务器端代码:
<?php
require_once 'stripe/init.php';
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("xxxxxxxxxxxxxxxxxxxx");
// Get the credit card details submitted by the form
$token = $_POST["token"];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array("amount" => 1000, // amount in cents, again
"currency" => "usd", "source" => $token, "description" => "Example charge"));
echo json_encode(array("msg" => "success"));
exit ;
} catch(\Stripe\Error\Card $e) {
echo json_encode(array("msg" => $e -> getMessage()));
exit ;
}
创建新客户的第二次调用出现 500 内部服务器错误。
创建客户的服务器端代码:
<?php
require_once 'stripe/init.php';
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("xxxxxxxxxxxxxxxxx");
// Get the credit card details submitted by the form
$token = $_POST["token"];
// Create the charge on Stripe's servers - this will charge the user's card
try {
// add customer
$email = "example@yahoo.com";
\Stripe\Customer::create(array("description" => "Customer for " . $email, "source" => $token // obtained with Stripe.js
));
echo json_encode(array("msg" => "success"));
exit ;
} catch(\Stripe\Error\Card $e) {
echo json_encode(array("msg" => $e -> getMessage()));
exit ;
}
这是我尝试过的:
我尝试单独(一个接一个)运行 ajax 调用,它们都工作正常,但是当我尝试按顺序(一个接一个)进行多个 ajax 调用时,第二个 ajax 调用总是失败。
我也尝试交换这些调用的顺序,即我尝试先调用添加客户,然后作为第二次调用向客户收费,在这种情况下,客户添加成功但无论什么顺序,第二次调用都会总是失败并给出 500 错误。
因此,我认为我的服务器端代码对于“Charge::create”和“Customer::create”这两个函数都是正确的,但我需要在第一次 ajax 调用之后重新设置一些东西,这样第二次 ajax 调用也会成功。
客户端代码
jQuery.ajax({
url : 'sp/stripeProcess.php',
method : "POST",
data : {
token : token
},
dataType : 'json',
cache : false,
success : function(response) {
console.log(response);
if (response.msg == "success") {
jQuery.ajax({
url : 'sp/createCustomer.php',
method : "POST",
data : {
token : token
},
dataType : 'json',
cache : false,
error : function(jqxhr, textstatus, errorThrown) {
console.log(jqxhr.status);
},
success : function(response) {
console.log(response);
if (response.msg == "success") {
console.log('customer added');
} else {
jQuery(".payment-errors").text(response.msg);
}
}
});
} else {
jQuery(".payment-errors").text(response.msg);
}
}
});
【问题讨论】:
-
客户端可能有问题?可以提供js代码吗?
-
@aleksv 你可以看看客户端代码,我已经更新了问题
-
这可能是出于函数或方法的原因。您能否记录令牌变量并查看它在多次请求后是否发生变化?
-
@aleksv 我刚刚记录了 token 变量,在第一次 ajax 调用后它是相同的并且不会改变
-
设置 $token = "your token" 然后直接在浏览器中通过输入 url(不带 ajax)调用页面。真正的错误应该是可见的
标签: php ajax stripe-payments