Skip to content

Instantly share code, notes, and snippets.

@franciscoj
Created January 23, 2011 22:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save franciscoj/792543 to your computer and use it in GitHub Desktop.
Save franciscoj/792543 to your computer and use it in GitHub Desktop.
Some snippets needed to set up PayPal WPS with ActiveMerchant on Rails3
# config/initializer/active_merchant.rb
if Rails.env.production?
PAYPAL_ACCOUNT = 'production.paypal.account@domain.com'
else
PAYPAL_ACCOUNT = 'test.paypal.account@domain.com'
ActiveMerchant::Billing::Base.mode = :test
end
<!-- app/views/payment/new.html.erb -->
<% payment_service_for @order.id, PAYPAL_ACCOUNT,
:amount => @order.price, :currency => 'EUR',
:service => :paypal do |service|
service.customer :first_name => current_user.name,
:last_name => current_user.surname,
:email => current_user.email
service.item_name @order.items_summary
# PayPal will POST a callback here when the payment is done
service.notify_url notifications_url(@order)
# PayPal will take your user here with a POST after he pays
service.return_url paypal_return_notifications_url(@order)
# PayPal will redirect your user here if he cancels the payment
service.cancel_return_url paypal_cancel_notifications_url(@order) %>
<%= submit_tag 'Pay this order' %>
<% end %>
# app/controllers/notifications_controller.rb
class NotificationsController < ApplicationController
include ActiveMerchant::Billing::Integrations
protect_from_forgery :except => [:create, :paypal_return]
# This action is for when the buyer returns to your site from PayPal
def paypal_return
flash[:notice] = "Thanks for buying this!"
redirect_to root_path
end
# This action is for when the buyer cancels
def paypal_cancel
flash[:notice] = "We're sorry you didn't buy :("
redirect_to root_path
end
# This is what will receive the IPN from PayPal
def create
# You maybe want to log this notification
notify = Paypal::Notification.new request.raw_post
@order = Order.unpaid.find(notify.item_id)
if notify.acknowledge
# Make sure you received the expected payment!
if notify.complete? and @order.price == BigDecimal.new( params[:mc_gross] )
# All your bussiness logic goes here
@order.update_attributes(:paid => true)
render :nothing => true
end
rescue
#Make sure you log the exceptions you have.
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment