使用Rails和'paypal-sdk-merchant'gem将多个接收器添加到Paypal Mass Pay

时间:2014-05-13 00:14:29

标签: ruby-on-rails ruby ruby-on-rails-3 paypal masspay

我正在创建一个必须使用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])

2 个答案:

答案 0 :(得分:1)

语法有点像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

答案 1 :(得分:0)

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