Rubyology artwork

Rubyology 46: PayPal on Rails

Rubyology

English - September 30, 2007 15:19 - ★★★★★ - 7 ratings
Technology Education How To Homepage Apple Podcasts Google Podcasts Overcast Castro Pocket Casts RSS feed

Previous Episode: Rubyology 45: Advanced Rails
Next Episode: Rubyology 47: Wuby Intro

I will demo to you VoteSpin's campaign donation engine powered by PayPal. I borrowed the original PayPal logic from Cuban Links blog (see URL below)
http://cubanlinks.org/articles/2005/8/3/ruby-on-rails-paypal-ipn-code-example

My code is below:
if @request.method == :post
## use the POSTed information to create a call back URL to PayPal
@query = 'cmd=_notify-validate'
@request.params.each_pair {|key, value| @query = @query + '&' + key + '=' + value.first if key != 'register/pay_pal_ipn.html/pay_pal_ipn' }

##http = Net::HTTP.start(PAYPAL_URL, 80)
http = Net::HTTP.start("www.paypal.com", 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] #if response

if response.body.chomp == "VERIFIED"
## check the payment status
if payment_status == "Completed"
# check to see if the txn_id already exists

@donation = Donation.find(item_number)
if @donation
if @donation.transamount == 0
@paypalipn = item_name + "-" + item_number + "-" + payment_status + "-" + payment_amount + "-" + payment_currency + "-" + txn_id + "-" + receiver_email + "-" + txn_id + "-" + payer_email
if @donation.update_attributes(:transamount => payment_amount, :paypalipn => @paypalipn)
@voter = Voter.find(:first, :conditions => [ "id = ?", @donation.voter_id])
@message = "Hi " + @voter.userid.capitalize + ",\r\n\r\nYou have just received a $" + payment_amount + " donation at VoteSpin (http://www.votespin.com/votespin/signin?deeplink=/donations/list). \r\n\r\nRegards,\r\nTeam VoteSpin"
Notifier::deliver_email_voter(@voter.email, '[email protected]', 'Donation Received at VoteSpin!', @message)
render :layout => false
end
end
end

end
end