Paypal Mass Pay API

When I started working on the Paypal Mass API I found very few resources on it. So Im guessing this might serve as a useful guide for people starting off.

This post only describes the setting up of the payment module and sending the data to paypal. Hope this is useful.

Please correct me if there are any glaring mistakes in my sample code.



require 'net/http'
require 'open-uri'
require "system_timer"

module PayExpert
  PAYPAL_END_POINT_URL_TEST = "https://api-3t.sandbox.paypal.com/nvp"
  LOGIN = URI.escape("XXXXXXXXXX")
  PASSWORD = URI.escape("XXXXXXXX")
  SIGNATURE = URI.escape("xXXXXXXXXXXXXXXXXXXXXXXXXX")

  # needs an array of hashes to be passed as the users list
  # ------------------------Example------------------------------------------------
  #user_1 = {:email=>"buyer_1254479426_per_1257321861_per@XXXXX.com",
  #          :amount=>10, :unique_id=>"020484", :note=>"Heres your money"}
  #user_2={:email=>"buyer2_1254493228_per@XXXXX.com", :amount=>10,
  #         :unique_id=>"270484", :note=>"more money for you"}
  #user_list = [user_1, user_2]
  #setup_payment (user_list)
  #----------------------------------------------------------------------------------

  def setup_payment(user_list)
    response = nil
    header = {"Content-Type"=>"application/x-www-form-urlencoded"}
    email_subject = URI.escape("Payment Notification")
    receiver_type =URI.escape("EmailAddress")
    currency = URI.escape("USD")
    version = URI.escape("56")
    method =URI.escape("MassPay")
    end_point_url = paypal_end_point
    user_information =  payable_expert_info(user_list)
     final_url_string = "#{end_point_url}?METHOD=#{method}" +
          "&PWD=#{PASSWORD}" +
          "&USER=#{LOGIN}" +
           "&SIGNATURE=#{SIGNATURE}"+
            "&VERSION=#{version}"+
            "&EMAILSUBJECT=#{email_subject}"+
             "&CURRENCYCODE=#{currency}"+
             "&RECEIVERTYPE=#{receiver_type}"+
              "#{user_information}"

        SystemTimer.timeout_after(20.seconds) do
          response = open(final_url_string)
        end

  rescue OpenURI::HTTPError => error
   status_code = error.io.status[0]
   RAILS_DEFAULT_LOGGER.debug "[ERROR][Paypal] #{error.message }  :  #{error.backtrace} "
  rescue Timeout::Error=>time_out_error
   RAILS_DEFAULT_LOGGER.debug "[ERROR][Timeout Error] #{time_out_error.message} : #{time_out_error.backtrace}"
  rescue =>err
   RAILS_DEFAULT_LOGGER.debug "[ERROR][Something went wrong] #{err.message} : #{err.backtrace}"
  end

  #accepts an array of hash
  def payable_info(user_values)
    final_url_string=""
    array_size = user_values.size - 1
      0.upto(array_size) do  |index|
       final_url_string += "&L_EMAIL" + index.to_s + "=#{URI.escape(user_values[index][:email])}"
       final_url_string += "&L_AMT" + index.to_s + "=#{(user_values[index][:amount])}"
       final_url_string += "&L_UNIQUEID" + index.to_s + "=#{URI.escape(user_values[index][:unique_id])}"
       final_url_string += "&L_NOTE" + index.to_s + "=#{URI.escape(user_values[index][:note])}"
     end
     final_url_string
  end

  # defines the paypal_end_point
  def paypal_end_point
    (ENV['RAILS_ENV'] == 'development') ? PAYPAL_END_POINT_URL_TEST : "something else"
  end

end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s