ruby-on-rails - 有没有更安全的方法可以同时使用 Stripe 和 ActiveMerchant?Ruby on Rails
问题描述
我正在尝试在我的 rails 应用程序中使用 Stripe 进行付款。我记得在我的一次实习中,我们使用了active merchant
gem 通过网关来抽象流程。虽然,在实习期间,我们使用了 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 时不断收到此错误,我不知道如何修复它。
任何帮助表示赞赏!
解决方案
使用 Stripe 网关,ActiveMerchant 的purchase
和authorize
方法应该采用您在上面传递的卡片对象或令牌值(字符串)
# purchase(money, card_hash_or_token, { ... })
出于PCI 合规性的原因,您可以在客户端使用 Stripe 的Checkout或Elements库创建一个令牌,而不是传递原始卡详细信息,将源/令牌 ID 传递给您的后端(它应该看起来像 tok_xxxyyyzzz 或 src_xxxyyyyz),然后将该值传递给card_hash_or_token
您的授权请求的第二个参数。
response = gateway.authorize(1000, params[:stripeToken])
推荐阅读
- javascript - 将新的键和值附加到 Javascript 对象数组中的每个元素
- python - 具有高斯函数的傅里叶空间带通滤波器(一维信号)
- javascript - MongoDB - 从数组中获取所有元素
- javascript - 验证数字输入总是返回一个数字作为值
- google-chrome-devtools - 检查元素时,Chrome DevTools 中的斜线区域是什么意思?
- node.js - Slack web-api 继续响应单个消息
- blockchain - 我应该使用 elrond devnet 而不是 testnet 进行开发吗?
- reactjs - Linter:“在 React + TS + 样式化组件上插入 `··` eslint(prettier/prettier)”
- iis - 在哪里可以找到有关如何将 Blazor 应用部署到在 Windows Server 2019 上运行的 IIS10 的具体详细信息
- python - 如何将 github 仓库中的“tesnorflow.model”文件的路径指定为同一仓库中存在的 py 文件代码