Вопрос

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 => "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:

        @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. [] is an array, you would add more members to that MassPayItem array:

    :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 с атрибуция
Не связан с StackOverflow
scroll top