-
-
Save viruthagiri/9561910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializer/active_merchant.rb | |
if Rails.env.production? | |
PAYPAL_ACCOUNT = '[email protected]' | |
else | |
PAYPAL_ACCOUNT = '[email protected]' | |
ActiveMerchant::Billing::Base.mode = :test | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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 %> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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