ruby - 与 PaypalExpressGateway.setup_authorization 匹配金额时出现问题
问题描述
我正在使用 ruby 客户端activemerchant
来创建对 Paypal 沙盒 API 的调用。脚本是从命令行调用的,所以变量填充了命令行参数。这是代码示例:
login = ARGV[0]
password = ARGV[1]
signature = ARGV[2]
ip = ARGV[3]
subtotal = ARGV[4]
shipping = ARGV[5]
handling = ARGV[6]
tax = ARGV[7]
currency = ARGV[8]
return_url = ARGV[9]
cancel_return_url = ARGV[10]
allow_guest_checkout = ARGV[11]
items = JSON.parse(ARGV[12])
ActiveMerchant::Billing::Base.mode = :test
paypal_options = {
login: login,
password: password,
signature: signature
}
gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
setup_hash = {
ip: ip,
items: items,
subtotal: Integer(subtotal),
shipping: Integer(shipping),
handling: Integer(handling),
tax: Integer(tax),
currency: currency,
return_url: return_url,
cancel_return_url: cancel_return_url,
allow_guest_checkout: allow_guest_checkout
}
amount = subtotal.to_i + shipping.to_i + handling.to_i + tax.to_i
puts "amount: " + amount.to_s
puts "items: " + items.to_s
response = gateway.setup_authorization(amount, setup_hash)
if !(response.success?)
puts response.message.to_s
end
这就是我在控制台中得到的:
amount: 10000
items: [{"name"=>"sample", "description"=>"desc", "quantity"=>1, "amount"=>10000}]
The totals of the cart item amounts do not match order amounts.
那么,为什么 10000 的数量与 10000 的项目不匹配?
解决方案
经过漫长而无聊的调试,我发现了以下内容:内部哈希items
应该是[:{name=>"sample", :description=>"desc", :quantity=>1, :amount=>10000}]
而不是[{"name"=>"sample", "description"=>"desc", "quantity"=>1, "amount"=>10000}]
上面的示例。
因此,我已将 JSON 解析器更改为它nice_hash
,items = ARGV[12].json
它就像一个魅力。
推荐阅读
- c++ - 访问未声明的结构?
- ruby-on-rails - React 中的重复选择(冰块宝石)
- swift - 在 UISplitViewController 中使用 SwiftUI 列表侧边栏
- flutter - Flutter (Dart) 在缺少必需的函数参数期间只给我一个警告而不是一个错误
- python - 如何从级别顺序遍历创建二叉树?
- html - Formik 中使用的“名称”和“类型”属性是什么?
- html - 我的网站主导航中的列表项使用弹性框粘在一起?我想要它们之间的空间
- c - 在结构中使用带有浮点变量的malloc
- reactjs - 我正在尝试在我的项目中使用 redux,但在调用使用 dispatch 的函数时遇到问题
- python - 树表的 Django 复杂分区和排序