Php 在多个ajax调用中出现Stripe 500内部服务器错误
我有一个应用程序,它使用ajax调用StripeSDK库中的函数 向客户收费的第一个电话没有任何问题 收费客户的服务器端代码:Php 在多个ajax调用中出现Stripe 500内部服务器错误,php,ajax,stripe-payments,Php,Ajax,Stripe Payments,我有一个应用程序,它使用ajax调用StripeSDK库中的函数 向客户收费的第一个电话没有任何问题 收费客户的服务器端代码: <?php require_once 'stripe/init.php'; // See your keys here https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey("xxxxxxxxxxxxxxxxxxxx"); // Get the credit card de
<?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 ;
}
<?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 ;
}
好的,问题是根据stripe API,令牌只能使用一次,所以在我的例子中,这就是解决方案:
- 使用令牌一次可创建一个新客户,该客户返回一个唯一的客户ID
- 通过传递第一次调用返回的客户ID而不是令牌,进行第二次ajax调用(向客户收费)
- 使用令牌一次可创建一个新客户,该客户返回一个唯一的客户ID
- 通过传递第一次调用返回的客户ID而不是令牌,进行第二次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);
}
}
});