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.
$rails generate scaffold product name: string unit_price: decimalMigrate the changes to database
$ rake db:migrateSet 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 endSome 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_queryFor 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
- Paypal Standard Integration Guide : https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_html_wp_standard_overview
- 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
Join Us