- Get link
- X
- Other Apps
Posted by
Raj Kumar
on
- Get link
- X
- Other Apps
... continued from PayPal Integration Basics
If you want to do some operations after getting payment confirmation i.e. Instant Payment Notification(IPN) which would sent by PayPal after every transaction (either completed or not), then you can follow next steps:
- First of all you need to enable IPN in your paypal account. Login to merchant account -> Profile -> Instant Payment Notification Preferences -> Enter notification url and select radio button "Receive IPN messages" -> Save
Note: You cannot get IPN on your rails local server, as you cannot set local address of your application while enable IPN from your paypal account.
- After getting IPN, you can make any operation according to your application's need. In my app, I am going to make operations on my new model after getting IPN. For this, generate a new model and migrate it to database:
$ rails generate model order product_id:integer quantity:integer amount:decimal status:string
$ rake db:migrate
$ rake db:migrate
- Now time to make associations,
- In app/models/product.rb, add
has_many :orders
- In app/models/order.rb, add
belongs_to :product
- Open config/route.rb, replace
resources :products
with
resources :products do
collection doend
post 'notification'end
- Add new method in app/controllers/products_controller.rb, for handelling IPN
def notification
#handle for old deals.end
if params[:item_number1] && !params[:item_number1].empty?
#paypal sends an IPN even when the transaction is voided.end
if params[:payment_status] != 'Voided'
@product = Product.find(params[:item_number1].to_i) rescue nil @product.orders.build(:quantity => 1, :amount => params[:mc_gross_1], :status => params[:payment_status]).save unless @product.nil?end
render :nothing => true
In app/views/products/index.html.erb,
product.paypal_url(products_url)
with
product.paypal_url(products_url, notification_products_url)
and add a new line after
:return => return_url
in paypal_url method (in app/models/product.rb), for supporting notify_url:
:notify_url => notify_url
also change signature of this method to accept notify_url parameter, it will look like:
def paypal_url(return_url,notify_url)
Done with IPN implementation, you can cross check for your IPN:
Login to merchant account -> History -> IPN History
Go Back to PayPal Basics
Must read about Security while Payments
The fully implemented application is uploaded on heroku: PayPal Integration.
You can checkout application's code here.
You can checkout application's code here.
Go Back to PayPal Basics
Must read about Security while Payments
- Get link
- X
- Other Apps
Comments
There are many reasons why one might need to Accept Payments Online . Maybe you sell two or three items, yet insufficient to invest the energy and money important to set up.
ReplyDelete"Such a valuable post! I always learn something new from your blog."
ReplyDeleteB2B payment tools
Great post! I really appreciate how you explained the online payment integration for Ruby on Rails in a simple and clear way. The focus on PCI-DSS compliance was also very helpful. Thanks for sharing this useful guide!Merchant Services Puerto Rico
ReplyDeleteGreat post! This is a helpful guide for integrating online payments in Ruby on Rails. I liked how you explained the options clearly—perfect for beginners getting started. Thanks for sharing! Clover Payment Processing
ReplyDeleteGreat post! Thanks for sharing a clear and simple guide on integrating online payments with Ruby on Rails. It’s super helpful for beginners like me. Looking forward to more articles like this! Clover POS Systems
ReplyDelete