Rails PayPal Chained Payment Fees

552 Views Asked by At

I have a ruby on rails app; where I wish to have one user be able to pay another user, less 10% 'commission' to the app; My client wants the fees to come out of the 10% the app keeps, for two reasons 1) not nickel/diming their customer 2) after a certain amount of transactions (per month), the percentage apparently gets lower

So, for example, if User 1 pays User 2 $100, I want it to show up as:

User 1 sends $100 to app -> App receives $97.09 ($100 less the fees) -> App sends 90.00 (90%) to User 2 -> User 2 receives the full $90 (no fees on his side)

However, despite setting the app as the primary receiver, it posted the fees on the secondary receiver, making User 2 pay the fees. I also attempted to set User 2 as the primary, and only send 10% forward to the app afterwards, but then it moved the fees to the primary receiver. The only thing I changed in the code was the percentage being charged, and the primary/secondary emails. My code looks like this:

<!-- app/lib/pay_pal_api.rb -->
require "pp-adaptive"

class PayPalAPI

def self.payment_url(submission)
  amount = submission.amount_in_cents.to_f / 100.0
  recipient_cut = amount * 0.9
  recipient_email = submission.submitter.paypal_email

  client.execute(:Pay,
    :action_type     => "PAY",
    :currency_code   => "USD",
    :cancel_url      => "http://localhost:3000/my-studio",
    :return_url      => "http://localhost:3000/submissions/#{submission.id}",
    :receivers       => [
      { :email => recipient_email, :amount => recipient_cut, :primary => false },
      { :email => "[email protected]", :amount => amount, :primary => true }
    ]
  ) do |response|

    if response.success?
      puts "Pay key: #{response.pay_key}"

      # send the user to PayPal to make the payment
      # e.g. https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=abc
      return client.payment_url(response)
    else
      puts "#{response.ack_code}: #{response.error_message}"
    end

  end
  return nil
end
2

There are 2 best solutions below

2
On BEST ANSWER

Use the feesPayer field and set it to either PRIMARYRECEIVER or SECONDARYONLY depending on who is receiving the payment first. The ruby SDK version of this is fees_payer -- from the API Reference:

feesPayer   xs:string (Optional) The payer of PayPal fees. Allowable values are: 
        SENDER – Sender pays all fees (for personal, implicit simple/parallel payments; do not use for chained or unilateral payments)
        PRIMARYRECEIVER – Primary receiver pays all fees (chained payments only)
        EACHRECEIVER – Each receiver pays their own fee (default, personal and unilateral payments)
        SECONDARYONLY – Secondary receivers pay all fees (use only for chained payments with one secondary receiver)

EG:

client.execute(:Pay,
    :action_type     => "PAY",
    :currency_code   => "USD",
    :cancel_url      => "http://localhost:3000/my-studio",
    :return_url      => "http://localhost:3000/submissions/#{submission.id}",
    :fees_payer      => "SECONDARYONLY",
    :receivers       => [
      { :email => recipient_email, :amount => recipient_cut, :primary => false },
      { :email => "[email protected]", :amount => amount, :primary => true }
    ]
  )
1
On

I figured out that in the pp-adaptive gem, it requires this syntax:

:fees_payer       => "PRIMARYRECEIVER",

Now it is working perfectly.