【问题标题】:How can I invoke an error in my Stripe.card.createToken function?如何在我的 Stripe.card.createToken 函数中调用错误?
【发布时间】:2017-06-06 22:45:09
【问题描述】:

我在 React 中使用条带并通过 AJAX 调用处理费用。 我试图将 Stripe.card.createToken 函数剥离为这个问题的基本要素。

if else 语句检查响应 是否有任何错误。

我使用 4100000000000019 卡号来确保卡被拒绝错误,但无论输入哪个卡号都会触发 else 语句(成功收费)。

输入 4100000000000019 卡号会导致 Stripe 仪表板中的扣款被冻结。肯定会产生错误:

{
  "error": {
    "message": "Your card was declined.",
    "type": "card_error",
    "code": "card_declined",
    "decline_code": "generic_decline",
    "charge": "ch_19gMBaIWHxnqld7LCdbCtdNz"
  }
} 

但是 if(response.error) 被忽略并运行 else 语句。

    Stripe.card.createToken({
    number: $('.card-number').val(),
    cvc: $('.card-cvc').val(),
    exp_month: $('.card-expiry-month').val(),
    exp_year: $('.card-expiry-year').val(),
    name: $('.first-name').val()
}, function(status, response){
    if (response.error) {
        this.reportError(response.error.message);
    } else { // No errors, submit the form.
      var token = response.id;
        $.ajax({
           type: 'POST',
           url: 'components/charge.php',
           data : {
             stripeToken: token
           },
           success: function(data,response) {
             paymentSuccessful();
           },
           error: function(data,textStatus) {
             console.log("Ajax Error!");
           }
         });//$.ajax
    }//else
});//Stripe.card.createToken

非常感谢任何帮助。

更新:感谢 Larry Ullman 的这篇很棒的教程以及它关于条带错误处理的部分,我想出了一个相当不错的解决方案。 http://www.larryullman.com/2013/01/30/handling-stripe-errors/

所以我在 AJAX 成功函数中添加了 if else 语句。

success: function(data,response) {
                 if(data == "success"){
                   $('#payment-error-copy').text("Your payment was successful. Thank you for ordering!");
                   paymentSuccessful();
                 } else {
                   $('#payment-error-copy').text(data);
                 }
    };//success function

在charge中,我们可以返回错误响应和错误的确切原因。

我的charge.php文件

<?php
require_once('vendor/autoload.php');

// Get the payment token submitted by the form:
$token = $_POST['stripeToken'];
try{
  \Stripe\Stripe::setApiKey("<secret_KEY>");

  $customer = \Stripe\Customer::create(array(
    "source" => $token,
    "email" => $email
    )
  );
  // Charge the Customer instead of the card
  \Stripe\Charge::create(array(
      "amount" => $price, // amount in cents, again
      "currency" => "aud",
      "description" => $email." ".$first_name,
      "customer" => $customer->id
    )
  );
  echo "success";
} catch (\Stripe\Error\ApiConnection $e) {
    // Network problem, perhaps try again.
    $e_json = $e->getJsonBody();
    $error = $e_json['error'];
    echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
} catch (\Stripe\Error\InvalidRequest $e) {
    // You screwed up in your programming. Shouldn't happen!
    $e_json = $e->getJsonBody();
    $error = $e_json['error'];
    echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
} catch (\Stripe\Error\Api $e) {
    // Stripe's servers are down!
    $e_json = $e->getJsonBody();
    $error = $e_json['error'];
    echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
} catch (\Stripe\Error\Card $e) {
  // Card was declined.
  $e_json = $e->getJsonBody();
  $error = $e_json['error'];
  echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
}

?>

try catch 语句返回有关收费错误的精确消息并将其返回给我们的成功函数,因此如果收费不是成功,它就会运行错误函数。

尽管我的问题仍未得到解答,但构建工作按预期进行,并且用户输入账单信息仍然是安全的。

感谢其他 SO 用户的意见,非常感谢。

【问题讨论】:

    标签: ajax reactjs webpack stripe-payments


    【解决方案1】:

    我认为这个特定的卡号只有在 Stripe 实际尝试使用它时才会出错。

    刚刚为我的 SaaS 构建了一个更改卡功能,并注意到当我使用收到的令牌更新我的 StripeCustomer 默认源时,它失败了。

    【讨论】:

    • 我的问题中的代码,这是根据 Stripe 文档处理卡错误的唯一方法。当我控制台记录 response.error 时,它说错误未定义,但仪表板肯定会显示被阻止/失败的费用。我错过了什么吗?
    • 抱歉 - 我把测试卡号弄混了。我指的卡的卡号为 4000 0000 0000 0341。该卡通过了令牌创建,但未通过实际收费。无论如何 - 我也尝试了 4000 0000 0000 0119,我得到了一个令牌。然后,当我尝试创建客户时它失败了。
    • 我明白了。卡号确实会创建一个客户并导致收费失败。问题仍然是 response.error 被忽略,就好像它不存在一样,但它肯定会生成。
    • 那些“隐藏”的数据属性... :-) 我使用了大致相同的解决方案和链式承诺。很高兴它成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多