[英]Stripe 500 internal server error on multiple ajax call
我有一個應用程序使用ajax調用條帶SDK庫中的函數。
對客戶收費的第一個要求沒有任何問題 。
Serverside Code for Charge Customer:
<?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個內部服務器錯誤 。
Create Customer的Serverside代碼:
<?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);
}
}
});
好的,問題是根據條帶API,令牌只能使用一次,所以在我的情況下這是解決方案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.