使用 Rails 和“paypal-sdk-merchant”gem 将多个接收者添加到 Paypal Mass Pay
Posted
技术标签:
【中文标题】使用 Rails 和“paypal-sdk-merchant”gem 将多个接收者添加到 Paypal Mass Pay【英文标题】:Add multiplie receivers to Paypal Mass Pay using Rails and 'paypal-sdk-merchant' gem 【发布时间】:2014-06-30 12:24:14 【问题描述】:我正在创建一个必须使用 Paypal MassPayment API 的 Rails 应用程序(在我们的例子中,自适应支付不是一个选项)。
我正在使用“paypal-sdk-merchant”https://github.com/paypal/merchant-sdk-ruby
按照中建议的示例 https://paypal-sdk-samples.herokuapp.com/merchant/mass_pay 我可以使用一个接收器创建“大规模支付”:
@api = PayPal::SDK::Merchant::API.new
# Build request object
@mass_pay = @api.build_mass_pay(
:ReceiverType => "EmailAddress",
:MassPayItem => [
:ReceiverEmail => "enduser_biz@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "3.00" ] )
# Make API call & get response
@mass_pay_response = @api.mass_pay(@mass_pay)
# Access Response
if @mass_pay_response.success?
else
@mass_pay_response.Errors
end
问题是:如何构建一个有多个接收者的大规模支付对象?
按照文档,我尝试了以下代码并进行了多种变体,但 Paypal 似乎只考虑了最后一项:
@api = PayPal::SDK::Merchant::API.new
# Build request object
@mass_pay = @api.build_mass_pay(
:ReceiverType0 => "EmailAddress",
:MassPayItem0 => [
:ReceiverEmail => "enduser_biz@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "3.00" ],
:ReceiverType1 => "EmailAddress",
:MassPayItem1 => [
:ReceiverEmail => "enduser_biz1@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "5.00" ]
)
(...)
另外,我有一堆邮件和值,所以我需要把它们全部都批量支付,怎么办?
理想情况下,我想要一些东西:
@mass_pay = build_mass_pay_with_array_of_email_and_values([ARRAY_OF_EMAILS_AND_VALUES_HERE])
【问题讨论】:
【参考方案1】:require 'paypal-sdk-merchant'
@api = PayPal::SDK::Merchant::API.new
#make an array of members
member_list = []
member_list <<
:ReceiverEmail => 'someone@example.com',
:Amount =>
:currencyID => "USD",
:value => 1.6
member_list <<
:ReceiverEmail => 'someone2@example.com',
:Amount =>
:currencyID => "USD",
:value => 1.6
# Build request object
@mass_pay = @api.build_mass_pay(member_list)
# Make API call & get response
@mass_pay_response = @api.mass_pay(@mass_pay)
# Access Response
if @mass_pay_response.success?
else
@mass_pay_response.Errors
end
【讨论】:
解决了上一个解决方案的问题,但感谢您在此答案中详细说明:)【参考方案2】:语法有点像 JSON。 []
是一个数组,您可以向该 MassPayItem
数组添加更多成员:
:MassPayItem => [
:ReceiverEmail => "enduser_biz@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "3.00"
,
:ReceiverEmail => "enduser_biz2@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "1.00"
]
结束:
require 'paypal-sdk-merchant'
@api = PayPal::SDK::Merchant::API.new
# Build request object
@mass_pay = @api.build_mass_pay(
:ReceiverType => "EmailAddress",
:MassPayItem => [
:ReceiverEmail => "enduser_biz@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "3.00"
,
:ReceiverEmail => "enduser_biz2@gmail.com",
:Amount =>
:currencyID => "EUR",
:value => "1.00"
]
)
# Make API call & get response
@mass_pay_response = @api.mass_pay(@mass_pay)
# Access Response
if @mass_pay_response.success?
else
@mass_pay_response.Errors
end
【讨论】:
以上是关于使用 Rails 和“paypal-sdk-merchant”gem 将多个接收者添加到 Paypal Mass Pay的主要内容,如果未能解决你的问题,请参考以下文章
Rails 6 和 Google Maps for Rails
使用 Ember 项目运行 Yeoman 和 Rails 的缺点/优点和可行性?
ruby Rails Rspec模型使用rspec-rails,shoulda-matcher,shoulda-callbacks和factory_girl_rails测试骨架和备忘单。差不多
ruby Rails Rspec模型使用rspec-rails,shoulda-matcher,shoulda-callbacks和factory_girl_rails测试骨架和备忘单。差不多