结合使用 Stripe 和 ActiveMerchant 有没有更安全的方法? Ruby 在 Rails

Is there a safer way to use Stripe and ActiveMerchant together? Ruby on Rails

我正在尝试在我的 rails 应用中使用 Stripe 进行支付。我记得在我的一次实习中,我们使用 active merchant gem 通过使用网关来抽象流程。虽然,在实习的时候,我们用的是Authorize.net。我们没有使用 Stripe。对于这个特定的应用程序,我想同时使用 Stripe 和 ActiveMerchant。

查看 Active Merchant GitHub 页面上的文档,我发现我可以使用 Active Merchant gem 提供的 StripeGateway 连接到 Stripe。我是这样做的:

ActiveMerchant::Billing::Base.mode = :test

# Create a new credit card object
credit_card = ActiveMerchant::Billing::CreditCard.new(
  :number     => '4242424242424242',
  :month      => '8',
  :year       => '2022',
  :first_name => 'Tobias',
  :last_name  => 'Luetke',
  :verification_value  => '123'
)

if credit_card.valid?
  gateway = ActiveMerchant::Billing::StripeGateway.new(
    login: Rails.application.credentials.development[:stripe_private_key]
  )

  # Authorize for  dollars (1000 cents)
  response = gateway.authorize(1000, credit_card)

  if response.success?
    # Capture the money
    gateway.capture(1000, response.authorization)
  else
    raise StandardError, response.message
  end
end

但是,这是一个问题。每当我 运行 这个时,我都会得到一个奇怪的错误:

StandardError (Sending credit card numbers directly to the Stripe API is generally unsafe. We suggest you use test tokens that map to the test card you are using, see https://stripe.com/docs/testing.)

我知道这是一个安全问题,但我不知道如何使用 Active Merchant 修复它。我尝试在 rails 上使用 Stripe 的 ruby 文档,但形式非常简单。它只有信用卡号、到期数据、CVC 条目以及电子邮件。但我也需要一个账单地址。这就是我使用 Active Merchant 的原因。它使用起来非常简单,并且在仍然能够创建自定义表单的同时抽象出很多绒毛。但是,我一直在使用 Stripe 时遇到此错误,而且我不知道如何解决。

感谢任何帮助!

使用 Stripe 网关,ActiveMerchant 的 purchaseauthorize 方法应该采用上面传递的卡对象或令牌值(字符串)

#   purchase(money, card_hash_or_token, { ... })

https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/gateways/stripe.rb#L96

对于 PCI compliance reasons, you could create a token on your client-side using Stripe's Checkout or Elements 库,不是传递原始卡详细信息,而是将 source/token id 传递到您的后端(它应该看起来像 tok_xxxyyyzzz 或 src_xxxyyyyz),并且然后将该值传递给授权请求的第二个 card_hash_or_token 参数。

response = gateway.authorize(1000, params[:stripeToken])

https://dashboard.stripe.com/account/integration/settings

Handle card information directly We strongly discourage passing card information directly to Stripe’s API, as it means your integration is directly handling sensitive card information. Learn more. (https://stripe.com/docs/security#validating-pci-compliance)

我不是建议您这样做,但这是让 ActiveMerchant 按您希望的方式工作并消除该错误的方法。它大大提高了您需要处理的 PCI 合规性。