Integrating 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 there receiving and doing the 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, you can visit http://developer.paypal.com 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 e,g – 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 http://sandbox.paypal.com. Using your account information u 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 => ‘[email protected]’, #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}” => item.product.id, “quantity_#{index+1}” => item.quantity }) end “https://www.sandbox.paypal.com/cgi-bin/webscr?” + values.to_query end

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

h> You need to be logged 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, 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 Payment Notification 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 end

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.

Start making payment now.

Thanks for reading !!!

Get in touch with us.

Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish