簡體   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