- Get link
- X
- Other Apps
Posted by
Raj Kumar
on
- Get link
- X
- Other Apps
- 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.
- Sign up for paypal sandbox account here.
- Now login to your sandbox account from here and here you will see links:
create a pre-configured account and create an account manually.
- 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.
- Login to merchant account -> Profile -> Website Payment Preferences -> Find radio buttons under PAYMENT DATA TRANSFER and set it to ON. -> click on Save.
- Now come to the application. Go to config/database.yml and configure your database.
- Scaffold for product.
$ rails generate scaffold product name:string unit_price:decimal
- Now Migrate the changes to database (specified in config/database.yml)
$ 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”.
- 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) %>
- In app/models/product.rb
def paypal_url(return_url)
values = {end
: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'
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + 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.
- 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.
- 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.
- Get link
- X
- Other Apps
Comments
Hey man.... Awsum... You rock man...
ReplyDelete