Skip to content

Instantly share code, notes, and snippets.

@saranyan
Created December 27, 2011 18:38
Show Gist options
  • Save saranyan/1524729 to your computer and use it in GitHub Desktop.
Save saranyan/1524729 to your computer and use it in GitHub Desktop.
IPN without active merchant
#from http://www.ruby-forum.com/topic/163331#817338
#this code will also work if you are not using active merchant.
if request.post?
#new
@params = params
@params[:cmd] = "_notify-validate"
@params.delete(:action)
@params.delete(:controller)
url = URI.parse("https://www.paypal.com/cgi-bin/webscr")
req = Net::HTTP::Post.new(url.path)
req.set_form_data(@params)
sock = Net::HTTP.new(url.host, url.port)
sock.use_ssl = true
response = sock.start {|http| http.request(req)}
item_name = params[:item_name]
payment_status = params[:payment_status]
payment_amount = params[:mc_gross]
payment_currency = params[:mc_currency]
payer_email = params[:payer_email]
receiver_email = params[:receiver_email]
txn_id = params[:txn_id]
if response
@user = User.find_by_email(payer_email)
if @user
#find account and update
@account = Account.find_by_user_id(@user.id)
unless @account
@account = Account.new()
end
@account.status = 'Init'
end
if response.body == 'VERIFIED'
if payment_status == 'Completed'
@date = Date.today() + 365
@account.ipn = 'Completed'
@account.status = 'Completed'
@account.amount = payment_amount
@account.paypal_id = txn_id
@account.expires = @date
@account.save
@user.status = "Open"
@user.expires = @date
@user.save(false)
else
@account.status = 'Not Verified'
@account.save
@account.update_attribute(:ipn,'Pending')
end
else
@account.status = 'Not-Passed'
@account.save
end
end
end
render :nothing => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment