简体   繁体   English

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

[英]Add multiplie receivers to Paypal Mass Pay using Rails and 'paypal-sdk-merchant' gem

I'm creating an Rails application that must make use Paypal MassPayment API (adaptive payments is Not a option in our case). 我正在创建一个必须使用Paypal MassPayment API的Rails应用程序(在我们的情况下, 不支持自适应付款)。

I'm using the 'paypal-sdk-merchant' https://github.com/paypal/merchant-sdk-ruby 我正在使用'paypal-sdk-merchant'https: //github.com/paypal/merchant-sdk-ruby

Following the sample suggested in https://paypal-sdk-samples.herokuapp.com/merchant/mass_pay I'm able to create a "mass payment" with ONE receiver: 按照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

The problem is: how can I build a mass pay object with multiple receiver? 问题是: 如何构建具有多个接收方的批量支付对象?

Following the documentation, I tried the following code with a number of variations, but Paypal seems to considers only the last item: 根据文档,我尝试了以下代码,并进行了多种修改,但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" } }]
  }

)

(...)

Also, I have an array of emails and values, so I need to all them in the mass pay, how can it be done? 另外,我有一系列的电子邮件和值,所以我需要全部支付这些费用,怎么办?

Ideally, I would like something: 理想情况下,我想要一些东西:

@mass_pay = build_mass_pay_with_array_of_email_and_values([ARRAY_OF_EMAILS_AND_VALUES_HERE])

The syntax is sort of like JSON would be. 语法有点像JSON。 [] is an array, you would add more members to that MassPayItem array: []是一个数组,您将向该MassPayItem数组添加更多成员:

    :MassPayItem => [{
        :ReceiverEmail => "enduser_biz@gmail.com",
        :Amount => {
            :currencyID => "EUR",
            :value => "3.00"
        }
    },
    {
        :ReceiverEmail => "enduser_biz2@gmail.com",
        :Amount => {
            :currencyID => "EUR",
            :value => "1.00" 
        }
    }]

ending up with: 最终以:

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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM