使用PaypalExpressGateway.setup_authorization

时间:2019-03-28 09:06:20

标签: ruby paypal-sandbox activemerchant

我将红宝石客户端与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项不匹配?

1 个答案:

答案 0 :(得分:0)

经过长时间无聊的调试,我发现了以下内容: 内部哈希items应该是[:{name=>"sample", :description=>"desc", :quantity=>1, :amount=>10000}],而不是上面的示例中的[{"name"=>"sample", "description"=>"desc", "quantity"=>1, "amount"=>10000}]

因此,我已将JSON解析器更改为nice_hash,并且使用items = ARGV[12].json时,它就像是一种魅力。