javascript - Stripe 付款问题 - 网络错误,您尚未被收取费用

标签 javascript php jquery stripe-payments

我在使用 Stripe Connect 处理付款时遇到了一些麻烦。出于某种原因,我在提交表单后立即收到此错误:

发生网络错误,您未被收取费用。请重试

他们设置我的系统的方式是用户可以使用 Stripe 登录,这会从 Stripe 发回以下详细信息,我将这些详细信息连同用户 ID 一起保存到数据库中。

  • 访问 token
  • 刷新 token
  • 可发布的 key

在我的支付页面上,我有这个脚本:

Stripe.setPublishableKey('<?= $publishable_key; ?>');
                var stripeResponseHandler = function(status, response) { 
                    var $form = $('#payment-form'); 
                    $form.find('.form-error').text("") 
                    $form.find('.error').removeClass("error") 
                    validate = validateFields(); 

                    if (response.error) { 
                        error = 0; 
                        // Show the errors on the form  
                        if (response.error.message == "This card number looks invalid"){ 
                            error = error + 1; 
                            $form.find('.card_num').text(response.error.message); 
                            $('#dcard_num').addClass("error"); 
                        } 

                        if (response.error.message == "Your card number is incorrect."){ 
                            error = error + 1; 
                            $form.find('.card_num').text(response.error.message); 
                            $('#dcard_num').addClass("error"); 
                        } 

                        if (response.error.message == "Your card's expiration year is invalid."){ 
                            error = error + 1; 
                            $form.find('.exp').text(response.error.message); 
                            $('#dexp').addClass("error"); 
                        } 

                        if (response.error.message == "Your card's expiration month is invalid."){ 
                            error = error + 1; 
                            $form.find('.exp').text(response.error.message); 
                            $('#dexp').addClass("error"); 
                        } 

                        if (response.error.message == "Your card's security code is invalid."){ 
                            error = error + 1; 
                            $form.find('.cvc').text(response.error.message); 
                            $('#dcvc').addClass("error"); 
                        } 

                        if (error == 0){ 
                            $form.find('.payment-errors').text(response.error.message); 

                        } 
                        $form.find('button').prop('disabled', false); 
                    } else { 
                        if (validate == 1){ 
                            // token contains id, last4, and card type 
                            var token = response.id; 
                            // Insert the token into the form so it gets submitted to the server 
                            $form.append($('<input type="hidden" name="stripeToken" />').val(token)); 
                            // and re-submit 
                            $form.get(0).submit(); 
                        } 
                    } 
                }; 

出于某种原因,验证从未发生,而且我没有获得卡详细信息的 token 。结果,我的代码的下一部分,即我实际向用户收费的部分没有运行:

global $wpdb;
        $author_id = get_the_author_meta('id');
        $stripe_connect_account = $wpdb->get_row("SELECT * FROM wp_stripe_connect WHERE wp_user_id = $author_id", ARRAY_A); 
        if($stripe_connect_account != null){
            $publishable_key = $stripe_connect_account['stripe_publishable_key'];
            $secret_key = $stripe_connect_account['stripe_access_token'];
        }
                    $charging = chargeWithCustomer($secret_key, $amountToDonate, $currency_stripe, $stripe_usr_id);

这是 chargeWithCustomer 函数:

function chargeWithCustomer($secret_key, $amountToDonate, $currency, $customer) {
    require_once('plugin/Stripe.php');
    Stripe::setApiKey($secret_key);
    $charging = Stripe_Charge::create(array("amount" => $amountToDonate,
                "currency" => $currency,
                "customer" => $customer,
                "description" => ""));

    return $charging;
}

如果有人可以帮助我解决这个问题,我将不胜感激。我对我哪里出错了感到困惑,我无法在 Stripes 文档中找到答案。

最佳答案

如果您没有阅读整个系列,或者不知道秘诀,Stripe.js 会将支付信息直接发送到 Stripe 并获得关联的唯一 token 作为返回。然后该 token 会被提交到您的服务器并用于实际向客户收费。

如果您想知道充电尝试为何仍然会失败,那么说实话,您应该知道 Stripe.js 进程实际上只做了两件事:

1) 以安全的方式获取 Stripe 的付款信息(限制您的责任) 2)验证支付信息是否可用

**处理被拒绝的卡有点复杂,因为您需要找出卡被拒绝的原因并将该信息提供给客户,以便他或她可以纠正问题。目的是从异常中得到下降的具体原因。这是一个多步骤过程:

1)从异常中获取JSON格式的总响应

2)从响应中获取错误正文

3)从错误正文中获取具体信息**

    require_once('path/to/lib/Stripe.php');
try {
    Stripe::setApiKey(STRIPE_PRIVATE_KEY);
    $charge = Stripe_Charge::create(array(
        'amount' => $amount, // Amount in cents!
        'currency' => 'usd',
        'card' => $token,
        'description' => $email
    ));
} catch (Stripe_CardError $e) {
}
Knowing what kinds of exceptions might occur, you can expand this to watch for the various types, from the most common (Stripe_CardError) to a catch-all (Stripe_Error):

require_once('path/to/lib/Stripe.php');
try {
    Stripe::setApiKey(STRIPE_PRIVATE_KEY);
    $charge = Stripe_Charge::create(array(
        'amount' => $amount, // Amount in cents!
        'currency' => 'usd',
        'card' => $token,
        'description' => $email
    ));
} catch (Stripe_ApiConnectionError $e) {
    // Network problem, perhaps try again.
} catch (Stripe_InvalidRequestError $e) {
    // You screwed up in your programming. Shouldn't happen!
} catch (Stripe_ApiError $e) {
    // Stripe's servers are down!
} catch (Stripe_CardError $e) {
    // Card was declined.
}

希望这有助于...!

关于javascript - Stripe 付款问题 - 网络错误,您尚未被收取费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058997/

相关文章:

javascript - 将 JQuery Mobile 1.7.1 框架升级到新的 1.1

javascript - 使用 Waypoint 和 React 在滚动后执行一个函数

php - 在函数参数中转换数据类型?

php - mysql_fetch_array 在 while 循环内不起作用

javascript - 如何使用 jquery 从 radio 输入中获取数据值?

javascript - 我可以在JavaScript中使用closest()函数吗?

jquery - 基于屏幕宽度的表格左边距,带滚动条

javascript - 提交时更改输入背景

javascript - 当加载的内容具有特殊字符时,使用 html() 加载动态数据会破坏脚本

php - 使用 MySQL 查询生成的下拉列表中的选定值不会传递给变量