繁体   English   中英

多个ajax调用上的条带500内部服务器错误

[英]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,令牌只能使用一次,所以在我的情况下这是解决方案:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM