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 :
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.
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
protect_from_forgery except: [: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: Time.now
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.