首页 > 解决方案 > 有没有更安全的方法可以同时使用 Stripe 和 ActiveMerchant?Ruby on Rails

问题描述

我正在尝试在我的 rails 应用程序中使用 Stripe 进行付款。我记得在我的一次实习中,我们使用了active merchantgem 通过网关来抽象流程。虽然,在实习期间,我们使用了 Authorize.net。我们没有使用条纹。对于这个特定的应用程序,我想同时使用 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 $10 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 修复它。我尝试使用 Stripe 的 ruby​​ on rails 文档,但形式非常简单。它只有信用卡号、到期数据、CVC 条目以及电子邮件。但我也需要一个帐单地址。这是我使用 Active Merchant 的原因。它使用起来非常简单,并且在仍然能够创建自定义表单的同时抽象出很多绒毛。但是,我在使用 Stripe 时不断收到此错误,我不知道如何修复它。

任何帮助表示赞赏!

标签: ruby-on-railsstripe-paymentsactivemerchant

解决方案


使用 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 合规性的原因,您可以在客户端使用 Stripe 的CheckoutElements库创建一个令牌,而不是传递原始卡详细信息,将源/令牌 ID 传递给您的后端(它应该看起来像 tok_xxxyyyzzz 或 src_xxxyyyyz),然后将该值传递给card_hash_or_token您的授权请求的第二个参数。

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

推荐阅读