How to Integrate Paypal into Rails Application?

Paypal is an e-commerce based web application, which transfers the amount between two accounts. Almost all US-based companies use Paypal for receiving and doing their payments.

Lets see how to integrate this service in rails application in different steps.

Note: I have tried these steps in development mode, on my local machine. It works fine but haven’t tested in production.

Step 1

a> In case if one doesn’t have a paypal account, go to and sign up to make an account. Then login using that account.

b> In order to test the application user should have test accounts on paypal.

For example, I made two accounts, one as buyer and another as seller. Both have there secure login and later they can use these accounts to send or receive payments.

c> These pre-configured accounts can be created by going to test accounts tab. Then click on pre-configured option to create test accounts on paypal with little extra information. Please make note of the account id and password generated for both buyer and seller.

d> Now based on requirement check the checkbox of the particular account and click on the link box to enter that particular test account. Clicking on that will redirect you to Using your account information you one can login to that particular account. These accounts and payments are simulated as any original account will behave.

e> Now lets code our rails application to use these accounts. I am using “Add to cart” option to select few items and add them into cart, by clicking on checkout button, it redirects to paypal site.On next page:-

f> In my cart.rb,paypal_url method to redirect to paypal site:
def paypal_url(return_url,notify_url) #1st url to return to ur own app, 2nd url for paypal to send notification values = { :business => ‘’, #account generated by paypal :cmd => ‘cart’, :upload => 1, :return => return_url, :invoice => id, :notify_url => notify_url } line_items.each_with_index do |item, index| values.merge!({ “amount#{index+1}” => item.product.price, “item_name_#{index+1}” => item.product.title, “item_number_#{index+1}” =>, “quantity_#{index+1}” => item.quantity }) end “” + values.to_query end

g> In my show.html.erb,i have placed the checkout button with url to return to:
= button_to “Checkout”, @cart.paypal_url(products_url,payment_notifications_url)

h> You need to be loged in while simulating this service.

i> After clicking on checkout, this will redirect to paypal site with secure connection. This will look like this:-It contains cart details and form for buyer to log in..

j> On this payment page, one needs to give his buyer account id and password to log in. After logging in it will display other user details too with pay-now option. After paying, the buyer can return to his own store by clicking on the link provided on the page.

Notifications from paypal

a>Notifications sent from paypal can be saved in the database.For that we need to have model with proper fields. i.e- rails g model PaymentNotification params:text status:string transaction_id:string cart_id:integer

My notifications controller(create method) will look like this

def create PaymentNotification.create!(:params => params, :cart_id => params[:invoice], :status => params[:payment_status], :transaction_id => params[:txn_id]) render :nothing => true

b>There is no direct of testing this,unless deployed in production but it can be tested in the console using curl command. eg-curl -d “txn_id=9JU83038HS278211W&invoice=1&payment_status=Completed” http://localhost:3000/payment_notifications

c>If this command runs without giving any exceptions,then all is well and values will be stored in the database.

Can start making payment now.

Thanks for reading !!!



One thought on “How to Integrate Paypal into Rails Application?

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s