Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PayPal IPN using Active merchant
if Rails.env.production?
ActiveMerchant::Billing::Base.mode = :test
<% 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 =>, :email =>
service.billing_address :city =>, :address1 => @user.address1, :state => @user.state, :country =>, :zip =>
service.item_name "Testing IPN"
service.invoice @invoice "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 => "", :address1 => "awesome ln", :city => "Austin", :state => "TX", :zip => "78759", :country => "USA", :phone => "5120070070" }
#using openstruct to access the hash by a dot notation
@user = _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"
def notify
#handle notification here. You can use this response
#to update your database, etc.
#for now lets print the notification raw response
notify =
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}"
render :nothing => true
def show
#gets redirected here when the transaction is successful

This comment has been minimized.

Copy link

@tomprats tomprats commented Apr 12, 2014

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