Search code examples
ruby-on-railsrubyruby-on-rails-3paypalmasspay

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

I'm using the '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:

@api = PayPal::SDK::Merchant::API.new

# Build request object
@mass_pay = @api.build_mass_pay({
  :ReceiverType => "EmailAddress",
  :MassPayItem => [{
    :ReceiverEmail => "[email protected]",
    :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:

        @api = PayPal::SDK::Merchant::API.new

# Build request object
        @mass_pay = @api.build_mass_pay({


        :ReceiverType0 => "EmailAddress",
        :MassPayItem0 => [{
        :ReceiverEmail => "[email protected]",
        :Amount => {
        :currencyID => "EUR",
        :value => "3.00" } }],

        :ReceiverType1 => "EmailAddress",
        :MassPayItem1 => [{
        :ReceiverEmail => "[email protected]",
        :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])

Solution

  • The syntax is sort of like JSON would be. [] is an array, you would add more members to that MassPayItem array:

        :MassPayItem => [{
            :ReceiverEmail => "[email protected]",
            :Amount => {
                :currencyID => "EUR",
                :value => "3.00"
            }
        },
        {
            :ReceiverEmail => "[email protected]",
            :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 => "[email protected]",
            :Amount => {
                :currencyID => "EUR",
                :value => "3.00"
            }
        },
        {
            :ReceiverEmail => "[email protected]",
            :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