Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Integrating Paypal in Rails Application (Part-2)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 612
    Comment on it

    Paypal Integration in Rails (continued)

    • Now, When the user is redirected to the method paypal_url , defined in the model file (order.rb) there we will give the html variables in which we will connect our application to the paypal sandbox site for dummy transaction.

    In this method we will pass in the required parameters by paypal to handle our transactions.

    But before that we need to know about NGROK


    NGROK allows us to expose the localhost web server running on our machine to the internet. Its like making our web application live. Now to redirect the users back to our website with the payment notifications generated by PayPal, we need to tunnel our localhost web server with ngrok. NGROK creates a URL for our web application which we can use to launch our website. Steps to use NGROK are as follows :

    • Go to this link and download NGROK for your OS. Mine is linux.
    • After that go to the directory from your console where you have installed NGROK and run this command
    $ ./ngrok http 3000


    This will start your NGROK server and it will provide you with a URL. Now we will use this URL further Now we are done with installing NGROK and will move further to the payment_url method defined in order.rb model file.

    Here we will send some parameters to the paypal which includes the name,address,invoice of the buyer, return and notify URLs where the user will be redirected after successful transaction and a hash which contains the array of products present in the cart.


    The method is define as follows : 

    class Order < ActiveRecord::Base
      belongs_to :my_cart, foreign_key: "myCart_id"
      serialize :params
      def paypal_url
        cart = MyCart.find(self.myCart_id)
        values = {
          :business => '',
          :cmd => '_cart',
          :upload => 1,
          :return => "",
          :invoice =>,
          :address_override => 1,
          :first_name => cart.user.first_name,
          :last_name => cart.user.last_name,
          :address1 => cart.address.address,
          :city =>,
          :state => cart.address.state,
          :zip =>,
          :country =>,
          :notify_url => ""
        cart.cart_items.each_with_index do |item, index|
          if item.product.discounted_price.present?
            item_price = item.product.discounted_price
            item_price = item.product.price
            "amount_#{index+1}" => item_price,
            "item_name_#{index+1}" =>,
            "quantity_#{index+1}" => item.product_quantity
        "" + values.to_query


    Here return_url contains our NGROK url where the users will be redirected where you may display the order receipt of the buyer's order. And notify_url is the URL where paypal will send all the payment notifications in  the form of parameters which contains several useful things like transaction ID, status etc. In this hook method we will update our order table with transaction ID, status and notification params like this :

    class OrderController < ApplicationController
      def create
      protect_from_forgery except: [:hook]
      def hook
        status = params[:payment_status]
        if status == "Completed"
          @order = Order.find(params[:invoice])
          @order.update_attributes notification_params: params, status: status, transaction_id: params[:txn_id], purchased_at:
        render nothing: true


    Here your order table's attributes will get updated and you have successfully save all the transaction parameters to your order table which you can use in your order receipt page to display all the order information. Hope these blogs will help you to integrate paypal successfully with your rails application.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: