Online Payment Solution for RoR Application: PayPal - Basic

The idea behind PayPal is simple: Use encryption software to allow people to make financial transfers between computers. That simple idea has turned into one of the world's primary methods of online payment. Despite its occasionally troubled history, including fraud, lawsuits and zealous government regulators, PayPal now boasts over 100 million active accounts in 190 markets worldwide 
PayPal is an online payment service that allows individuals and businesses to transfer funds electronically. Here are some of the things you might use PayPal for:
  • Send or receive payments for online auctions at eBay and other Web sites
  • Purchase or sell goods and services
  • Make or receive donations
  • Exchange cash with someone

Now as a developer, you need to integrate paypal in your rails application for get paid. Follow the step given below for painless integration of paypal.

  1. Sign up for paypal sandbox account here.
  1. Now login to your sandbox account from here and here you will see links:
  • create a pre-configured account and
  • create an account manually.
  1. You can use any of above mentioned links for creating accounts. You just need to create two accounts, one for seller and another for buyer.
  1. Login to merchant account -> Profile -> Website Payment Preferences -> Find radio buttons under PAYMENT DATA TRANSFER and set it to ON. -> click on Save.
  1. Now come to the application. Go to config/database.yml and configure your database.
  1. Scaffold for product.
$ rails generate scaffold product name:string unit_price:decimal
  1. Now Migrate the changes to database (specified in config/database.yml)
$ rake db:migrate
  1. Set root for the application
root :to => 'products#index'
  1. Start rails server ($ rails server) and add some products using link “New Product”.
  1. In app/views/products/index.html.erb, add following code for every products record (In 'each' iteration block)
<%= link_to 'Buy Now!!', product.paypal_url(products_url) %>
  1. In app/models/product.rb
def paypal_url(return_url)
values = {
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => UNIQUE_INTEGER

"amount_1" => unit_price,
"item_name_1" => name,
"item_number_1" => id,
"quantity_1" => '1'

"" + values.to_query

  • :invoice should be unique everytime you make transaction.
  • :cmd i.e. '_cart' specifies that its for shopping cart purchases.
  • :upload = '1' allows third party carts.
  1. For making actual payments, you have to replace last line of above method to:
"" + values.to_query

For actual payments you need merchant account on paypal.
  1. 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]

Thats 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.

The fully implemented application is uploaded on heroku: PayPal Integration.
You can checkout application's code here.

Must Read IPN Implementation.


Post a Comment