多次 ajax 调用时出现 Stripe 500 内部服务器错误

Stripe 500 internal server error on multiple ajax call

我有一个应用程序使用 ajax 调用 stripe 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 调用总是失败 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,令牌只能使用一次,所以在我的情况下,这是解决方案:

  • 使用令牌一次创建一个新客户,其中 returns 一个唯一的客户 ID。
  • 传递第一次调用返回的客户 ID 而不是令牌,进行第二次 ajax 调用(向客户收费)。