首页 > 解决方案 > Stripe.js 是否应该捕获所有可能发生的错误?

问题描述

用户可以通过Stripe订阅我的 Rails 应用:

class Subscription::CreditCardController < Subscription::PaymentController

  def new
    stripe_customer = Customers::FindOrCreate.call(current_account)
    if stripe_customer
      @stripe_subscription = Subscriptions::Create.call(current_account, @price, :payment_behavior => "default_incomplete")
      @client_secret = @stripe_subscription.latest_invoice.payment_intent.client_secret
    else
      flash[:notice] = "An error occurred."
      redirect_to plans_path
    end
  end

  def create
    flash[:success] = "Subscription created."
    redirect_to plans_path
  end

end

我依靠 Stripe.js 来捕捉可能发生的任何错误:

  form.addEventListener('submit', function(e) {
    e.preventDefault();
    handlePayment();
  });

  function handlePayment() {
    stripe.confirmCardPayment(clientSecret.value, {
      payment_method: {
        card: card,
        billing_details: {
          name: name.value,
          email: email.value
        }
      }
    }).then(function(result) {
      if (result.error) {
        displayError(result.error);
      } else {
        form.submit();
      }
    });
  }

到目前为止,这适用于所有测试用例的 99%。Stripe.js 捕获了几乎所有可能发生的错误。但在极少数情况下它不会,因此当 Stripe 尝试创建订阅时,服务器上会发生错误,使其处于某种incomplete状态。

这是一个主要问题,因为我现在没有在我的create控制器操作中处理这些类型的错误。

那么,为什么 Stripe 会捕获大多数这些错误而不是全部?作为开发人员,我是否应该在服务器上使用 Stripe.js 验证用户输入?还是两者中的一个就足够了?阅读 Stripe 的文档后,我的印象是 Stripe.js 将为我处理所有事情,而且我不必在服务器级别添加额外的检查。

这不正确吗?

标签: ruby-on-railsstripe-payments

解决方案


推荐阅读