Skip to content

Instantly share code, notes, and snippets.

@saranyan
Created December 20, 2011 16:38
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save saranyan/1502215 to your computer and use it in GitHub Desktop.
Save saranyan/1502215 to your computer and use it in GitHub Desktop.
PayPal IPN using Active merchant
if Rails.env.production?
PAYPAL_ACCOUNT = 'production@gmail.com'
else
PAYPAL_ACCOUNT = 'development@gmail.com'
ActiveMerchant::Billing::Base.mode = :test
end
<% payment_service_for @item_number, PAYPAL_ACCOUNT, :amount => @amount, :currency => @currency, :service => :paypal do |service|
service.customer :first_name => @user.first_name, :last_name => @user.last_name, :phone => @user.phone, :email => @user.email
service.billing_address :city => @user.city, :address1 => @user.address1, :state => @user.state, :country => @user.country, :zip => @user.zip
service.item_name "Testing IPN"
service.invoice @invoice
service.tax "0.00"
service.return_url url_for(:only_path => false, :controller => 'pay_pal',:action => 'show')
service.cancel_return_url url_for(:only_path => false, :controller => 'pay_pal', :action => 'cancel') %>
<%= submit_tag 'Make Payment'%>
<% end %>
require 'ostruct'
class PayPalController < ApplicationController
#include active merchant billing
include ActiveMerchant::Billing::Integrations
def create
#create a user object, not required, but ideally this is shown as a demonstration of the user data that comes
#from your database
_user = {:first_name => "saran", :last_name => "v", :email => "some_per@gmail.com", :address1 => "awesome ln", :city => "Austin", :state => "TX", :zip => "78759", :country => "USA", :phone => "5120070070" }
#using openstruct to access the hash by a dot notation
@user = OpenStruct.new _user
@amount = "0.01"
@currency = "USD"
#a random invoice number for test.
@invoice = Integer rand(1000)
#this will also come from your product database
@item_number = "123"
end
def notify
#handle notification here. You can use this response
#to update your database, etc.
#for now lets print the notification raw response
notify = Paypal::Notification.new(request.raw_post)
p "Notification object is #{notify}"
if notify.acknowledge
p "Transaction ID is #{notify.transaction_id}"
p "Notification object is #{notify}"
p "Notification status is #{notify.status}"
end
render :nothing => true
end
def show
#gets redirected here when the transaction is successful
end
end
@tomprats
Copy link

How does paypal know to hit your notify url. It doesn't look like you pass that url anywhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment