Basic Paypal integration with RoR application

Paypal is now a widely accepted payment gateway by the developer community due to its global acceptability and ease of integration with all types of web applications. Here in this blog, we will learn to integrate Paypal Standard with Ruby on Rails(RoR) 3 application.

Sign up for Paypal sandbox account Paypal Sandbox lets you test your Paypal integration without actually charging your credit card or your paypal account. Sandbox is very handy tool to have while your application is in development mode. You can sign up for Paypal Sandbox here. Create Sandbox accounts We will need to create a seller and a buyer account on paypal sandbox for proper testing of payments. When you login to Paypal sandbox, you will see the following options to create accounts:
  • Create a preconfigured account and
  • Create an account manually.
You can use any of above mentioned options for creating accounts. You just need to create two accounts, one for seller and another for buyer. Set the application Scaffold the model. We assume that model is called as Product.
$rails generate scaffold product name: string unit_price: decimal
Migrate the changes to database
$ rake db:migrate
Set root for the application
root :to => 'products#index'
Start rails server ($ rails server) and add some products using link “New Product”. Add a link to buy now in your views(app/views/products/index.html.erb for this example), add following code for every products record We need to define the paypal_url for each product to be sold to communicate appropriate data to paypal. The function defines the various parameters for the paypal and returns them in the form of Query String.  More details about these query string parameters can be found on the paypal developer website. In app/models/product.rb

# This defines the paypal url for a given product sale
def paypal_url(return_url)
values = {
:business => YOUR_MERCHANT_EMAIL,
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => UNIQUE_INTEGER
}

values.merge!({
"amount_1" => unit_price,
"item_name_1" => name,
"item_number_1" => id,
"quantity_1" => '1'
})

# This is a paypal sandbox url and should be changed for production.
# Better define this url in the application configuration setting on environment
# basis.
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query

end
Some notes about the parameters to be sent to Paypal:
  • : Invoice should be unique every time you make transaction.
  • : Cmd i.e. ‘_cart’ specifies that it’s for shopping cart purchases.
  • : upload = ‘1’ allows third party carts.
For making actual payments, you have to replace last line of above method to:
 "https://www.paypal.com/cgi-bin/webscr?" + values.to_query 
For actual payments you need merchant account on PayPal. 13.In app/controllers/products_controller.rb, add following code:
 flash[:notice] = "Your Transaction is #{params[:st]} for amount of $#{params[:amt]}. Thank You for shopping." if params[:st] 
That’s it!! Now start rails server and click on “Buy Now” link. It will land you to paypal’s payment page. Enter your buyer test account credentials and click login. Now you will see a page with some details, check them and click Pay Now. It will make transaction and will redirect you to return_url you sent from paypal_url method. Notes You will need to enable PAYMENT DATA TRANSFER in the merchant account. To do so, Login to merchant account -> Profile  -> Website Payment Preferences -> Find radio buttons under PAYMENT DATA TRANSFER and set it to ON. -> Click on Save. References
  1. Paypal Standard Integration Guide : https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_html_wp_standard_overview
  2. HTML Variables for Paypal Standard Integration: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_Appx_websitestandard_htmlvariables
  • Pingback: Paypal Standard integration with RoR Application – Integrating IPN | Enbake Consulting

  • Johhan Santana Canales

    The part where it says ‘Click on “Buy Now” link’. I think there is no ‘Buy Now’ link in this tutorial?

  • Abhishek Pandey

    How to proceed if buyer does not have paypal account.?

  • Hortense Harrod

    Fantastic blog post , For what it’s worth , others want a MA DoR 1 , my business partner came across a fillable form here http://goo.gl/RoRZeW