Paypal IPN in Rails with Active Merchant
Active Merchant makes it extremely simple to use Paypal IPN. Here is a simple guide for getting IPN up and running.
Sign up for a Paypal sandbox account
Paypal provides a sandbox environment that mimics their production environment, with the exception that it doesn't actually process the transactions. This is extremely useful for development and testing. It allows you to create multiple fake accounts and generate bank accounts and credit cards. More information can be found on Paypal's Testing Instant Payment Notification page.
Unfortunately, I've signed up for two different developer accounts and I've had trouble logging in with both of them. I've tried resetting my password, but I still can't log in. Fortunately, I already have my sandbox accounts set up and don't really have a need for it (except to write this guide).
Create a Personal account and add a credit card
After you sign up for your developer account, create a personal sandbox account and add a credit card.
Create a Business account and add a checking
Next, create a business sandbox account and add a checking account.
Install the money gem
sudo gem install money
Install the Active Merchant plugin
script/plugin install http://activemerchant.googlecode.com/svn/trunk/active_merchant
Create a form that submits to Paypal
Include ActiveMerchant::Billing::Integrations
in a controller to add Active Merchant's helpers.
class PaymentsController < ApplicationController
include ActiveMerchant::Billing::Integrations
def create
@enrollment = current_user.enrollments.find(params[:id])
end
end
In the view, use payment\_service\_for
to create a form that submits to Paypal to process the payment.
<% payment_service_for @enrollment.id, PAYPAL_ACCOUNT,
:amount => @enrollment.course.deposit, :currency => 'USD',
:service => :paypal do |service|
service.customer :first_name => @enrollment.student.first_name,
:last_name => @enrollment.student.last_name,
:phone => @enrollment.student.phone,
:email => @enrollment.student.email
service.billing_address :city => @enrollment.student.city,
:address1 => @enrollment.student.street,
:state => @enrollment.student.state,
:country => 'USA',
:zip => @enrollment.student.zip
service.item_name "#{@enrollment.course.program} Deposit"
service.invoice @enrollment.invoice.id
service.tax '0.00'
service.notify_url url_for(:only_path => false, :action => 'notify')
service.return_url url_for(:only_path => false,
:controller => 'account', :action => 'show')
service.cancel_return_url url_for(:only_path => false,
:controller => 'account', :action => 'show') %>
<!-- display payment summary here -->
<%= submit_tag 'Make Payment' %>
<% end %>
The code above refers to the constant PAYPAL\_ACCOUNT
, which I define in environment.rb
. I also set Active Merchant to use test mode, which directs it to use Paypal's sandbox:
unless RAILS_ENV == 'production'
PAYPAL_ACCOUNT = 'sandboxaccount@example.com'
ActiveMerchant::Billing::Base.mode = :test
else
PAYPAL_ACCOUNT = 'paypalaccount@example.com'
end
Create an action that processes the IPN
After the above form submits to Paypal and the user makes a payment, Paypal will post data about the transaction to your server. Set up an action to receive the post:
def notify
notify = Paypal::Notification.new(request.raw_post)
enrollment = Enrollment.find(notify.item_id)
if notify.acknowledge
@payment = Payment.find_by_confirmation(notify.transaction_id) ||
enrollment.invoice.payments.create(:amount => notify.amount,
:payment_method => 'paypal', :confirmation => notify.transaction_id,
:description => notify.params['item_name'], :status => notify.status,
:test => notify.test?)
begin
if notify.complete?
@payment.status = notify.status
else
logger.error("Failed to verify Paypal's notification, please investigate")
end
rescue => e
@payment.status = 'Error'
raise
ensure
@payment.save
end
end
render :nothing => true
end
Depending on the model for your application, this action will obviously look different. The important part is that you pass the raw post data from the request to Paypal::Notification.new
, and call notify.acknowledge
to connect back to Paypal to verify the data.
Enable IPN
Lastly, log into the business account that you created above, go to "Instant Payment Notification Preferences" in your profile, and set the URL that Paypal should post back to after payments. (Note: this needs to be a publicly accessible URL.)