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
    • 668
    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 https://ngrok.com/download 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 => 'abcdef-facilitator@gmail.com',
          :cmd => '_cart',
          :upload => 1,
          :return => "https://9c85dbb4.ngrok.io/order_receipt",
          :invoice => self.id,
          :address_override => 1,
          :first_name => cart.user.first_name,
          :last_name => cart.user.last_name,
          :address1 => cart.address.address,
          :city => cart.address.city,
          :state => cart.address.state,
          :zip => cart.address.zip,
          :country => cart.address.country,
          :notify_url => "https://9c85dbb4.ngrok.io/hook"
        }
        cart.cart_items.each_with_index do |item, index|
          if item.product.discounted_price.present?
            item_price = item.product.discounted_price
          else
            item_price = item.product.price
          end
          values.merge!({
            "amount_#{index+1}" => item_price,
            "item_name_#{index+1}" => item.product.name,
            "quantity_#{index+1}" => item.product_quantity
          })
        end
        "https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
      end
    end

     

    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
    
        ...
    
      end
    
      protect_from_forgery except: [:hook]
    
      def hook
        params.permit!
        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: Time.now
        end
        render nothing: true
      end
    end

     

    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: