As we know that these days many web applications have the functionality and requirements of money transactions. For example today we will take an example, of an e-commerce website or we can say an online shopping web application. In this application, there would be products which user can add to his cart and then proceed to checkout after selecting his shipment address from the existing addresses or by adding an all together new address.
The process would be as follows :
Firstly we need to create an account also known as merchant or facilitator account in the PayPal developer website on this link https://developer.paypal.com/ . This will create two dummy test accounts, one for merchant or the facilitator and the other one for buyer like this
$ rails g model order cart_id:integer,user_id:integer,transaction_id:integer,notification_params:text,status:string,purchased_at:datetime
And after that run rake db:migrate now creating order controller with methods create and hook. Create method will be used to create order and hook method will be used to update the attributes of order table once the transaction is completed with PayPal.
$ rails g controller order create hook
After adding the product to the cart and selecting the address for the shipment we will redirect the user to the create method of order controller passing in our cart_id because our cart stores all the products we added.
In the create method of order controller an order will be created with user id and cart id and after that we will redirect the user to model file of order i.e order.rb through the url by passing in the method name defined in the order.rb file like this.
class OrderController < ApplicationController
@order = Order.new(myCart_id:params[:myCart_id],user_id:current_user.id)
//In above url we are redirecting the user to the paypal_url which
is the method defined in the model file order.rb
When the user is redirected to the method paypal_url , defined in the model file 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.
So this was part 1 of this blog. Part 2 is defined in the next blog. Click on this link to go to part two of Paypal Integration in Rails.