Php 在多个ajax调用中出现Stripe 500内部服务器错误

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

我有一个应用程序,它使用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 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调用(向客户收费)

好的,问题是根据stripe API,令牌只能使用一次,因此在我的情况下,这就是解决方案:

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

也许客户端有问题?你能提供js代码吗?@aleksv你可以看一下客户端代码,我已经更新了问题这可能是函数或方法的问题。您可以记录令牌变量并查看它在多次请求后是否会发生变化吗?@aleksv我刚刚记录了令牌变量,在第一次ajax调用后它是相同的,不会更改设置$token=“your token”,然后通过键入url(不带ajax)在浏览器中直接调用页面。那么,真正的错误应该是可见的。客户端是否有问题?你能提供js代码吗?@aleksv你可以看一下客户端代码,我已经更新了问题这可能是函数或方法的问题。您可以记录令牌变量并查看它在多次请求后是否会发生变化吗?@aleksv我刚刚记录了令牌变量,在第一次ajax调用后它是相同的,不会更改设置$token=“your token”,然后通过键入url(不带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);
        }
    }
});