Home > Rails > Paypal Masspay Ruby Example

Paypal Masspay Ruby Example

The title of this post is a Google query that yielded no good results, but plenty of puzzled users trying to figure out how to make it work. I’ve only been playing with it for about half an hour, but this code is getting Paypal to tell me that the request is successful:

def self.send_money(to_email, how_much_in_cents, options = {})
	credentials = {
	  "USER" => API_USERNAME,
	  "PWD" => API_PASSWORD,
	  "SIGNATURE" => API_SIGNATURE,
	}
 
	params = {
	  "METHOD" => "MassPay",
	  "CURRENCYCODE" => "USD",
	  "RECEIVERTYPE" => "EmailAddress",
	  "L_EMAIL0" => to_email,
	  "L_AMT0" => ((how_much_in_cents.to_i)/100.to_f).to_s,
	  "VERSION" => "51.0"
	}
 
	endpoint = RAILS_ENV == 'production' ? "https://api-3t.paypal.com" : "https://api-3t.sandbox.paypal.com"
	url = URI.parse(endpoint)
	http = Net::HTTP.new(url.host, url.port)
	http.use_ssl = true
	all_params = credentials.merge(params)
	stringified_params = all_params.collect { |tuple| "#{tuple.first}=#{CGI.escape(tuple.last)}" }.join("&")
 
	response = http.post("/nvp", stringified_params)
end

Certainly not the tersest solution, but I’ve kept it a bit verbose to make it clearer what’s happening.

One point of note is that you’ll need separate credentials when submitting to sandbox vs. production. You can sign up for a sandbox account by clicking “Sign up” from this page. After you have your account, login, click on “profile,” then get your API access credentials.

Here is the PHP example I based my code on and here is the Paypal Masspay NVP documentation that was marginally helpful in figuring out what params to pass.

Bill Rails

  1. March 30th, 2011 at 23:41 | #1

    Thanks …this script works fine..

    Thanks
    Brijesh Shah

  2. July 5th, 2011 at 13:51 | #2

    Thank you very much! I have been fiddling with this for about 5 hours, at last my code looked almost exactly like yours, but a few small details lacked. Now it works :D

  3. Dharin
    July 27th, 2011 at 15:35 | #3

    @Gargron
    Hi Gargron,

    I am working with rails 3.0.5 and have sandbox account and want to do mass payment with WPP and soap. Can you help me out with this?

    Thanks,
    Dharin

  4. Mathew vivek
    August 3rd, 2011 at 07:11 | #4

    Thank u very much.. Works fine.. I have been searching for materials for more than a day.

  1. No trackbacks yet.