Ruby on Rails PayPal IPN Code Example
I thought I’d share the skeleton of some code I had to write for PayPal’s backend IPN API.
- POST this data http = Net::HTTP.start(PAYPAL_URL, 80) response = http.post(’/cgi-bin/webscr’, @query) http.finish
PayPal values
item_name = @params[:item_name]
item_number = @params[:item_number]
payment_status = @params[:payment_status]
payment_amount = @params[:mc_gross]
payment_currency = @params[:mc_currency]
txn_id = @params[:txn_id]
receiver_email = @params[:receiver_email]
payer_email = @params[:payer_email]<br/><br/> if response
if response.body.chomp 'VERIFIED'
# check the payment status
if payment_status ‘Completed’
# check to see if the txn_id already exists
# your logic here
end
end
else
# <span class="caps">GET</span> request, wtf
@text = ‘I do not speak <span class="caps">GET</span>’
end
rescue Net::HTTPError @text = ‘HTTP error’ end
This method exists inside of a controller class. I’m certainly not saying that this is the best or most elegant way to handle this, just that I couldn’t find any examples to rip-off myself. :)
Let me know if you have any suggestions. WARNING: this code does not communicate with PayPal’s servers over SSL.
About Me
Hey there. My name is Carter Rabasa and I am a husband and father of two beautiful daughters Catherine and Emily. I live in Seattle, WA.
- @cubanlinks on Twitter