Skip to content

Commit

Permalink
user is redirected to the return page first. modified create_by_brick…
Browse files Browse the repository at this point in the history
…_and_justgiving method to correctly read the params, so brick can be marked as purchased if the donation was successful
  • Loading branch information
Natalia Buckley committed Feb 13, 2012
1 parent 95cce06 commit 44fc2a2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Expand Up @@ -2,7 +2,7 @@
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
include ExceptionNotifiable
include ExceptionNotification

helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/payment_notifications_controller.rb
Expand Up @@ -9,9 +9,12 @@ def create

def return
if params[:donationId]
brick = Brick.find_by_url_key!(params[:id])
donation = JustGiving::Donation.new(params[:donationId])
notification = PaymentNotification.create_by_brick_and_justgiving(brick, donation.details)
@brick = Brick.find_by_url_key!(params[:id])
# save the donation id in case you need to further probe any details
@brick.donation_id = params[:donationId]
@brick.save
@donation = JustGiving::Donation.new(params[:donationId])
@notification = PaymentNotification.create_by_brick_and_justgiving(@brick, @donation.details)
# TODO what if the payment failed or was cancelled?
redirect_to thanks_brick_path(@notification.brick)
else
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/bricks_helper.rb
Expand Up @@ -6,7 +6,7 @@ def justgiving_link(brick)
justgiving_path = "http://v3-sandbox.justgiving.com/donation/direct/charity/185222"
end
amount = brick.value/100.0
exit_url = thanks_brick_url(brick) + "?donationId=JUSTGIVING-DONATION-ID"
exit_url = return_payment_notification_url(brick) + "?donationId=JUSTGIVING-DONATION-ID"
return "#{justgiving_path}?amount=#{amount}&frequency=Single&exitUrl=#{CGI.escape(exit_url)}"
end
end
10 changes: 5 additions & 5 deletions app/models/payment_notification.rb
Expand Up @@ -4,7 +4,7 @@ class PaymentNotification < ActiveRecord::Base
after_create :mark_brick_as_purchased

def self.create_by_brick_and_justgiving(brick, params)
status = (params[:status] == 'Accepted' or params[:status] == 'Pending') ? 'OK' : params[:status]
status = (params['status'] == 'Accepted' or params['status'] == 'Pending') ? 'OK' : params['status']
PaymentNotification.create(:brick => brick, :status => status, :params => params)
end

Expand All @@ -24,16 +24,16 @@ def aborted?

def mark_brick_as_purchased
if status == 'OK'
logger.info("Brick #{brick.url_key} purchased successfully")
brick.update_attribute(:purchased_at, Time.now)
logger.info("Brick #{@brick.url_key} purchased successfully")
@brick.update_attribute(:purchased_at, Time.now)

begin
FuKing::Twitter.update(twitter_message)
rescue => e
logger.warn("Error tweeting brick #{brick.url_key}: #{e.message}")
logger.warn("Error tweeting brick #{@brick.url_key}: #{e.message}")
end
else
logger.info("Brick #{brick.url_key} purchase unsuccessful: #{status}")
logger.info("Brick #{@brick.url_key} purchase unsuccessful: #{status}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/bricks/confirm.html.erb
Expand Up @@ -17,7 +17,7 @@
<div id="content" class="col col_copy">
<%= render(:partial => 'brick' ) %>
<% if admin? %>
<% unless admin? %>
<div id="personal_details">
<% form_for(@payment_notification) do |f| %>
<%= f.hidden_field :status %>
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20120213161201_add_donation_id_to_bricks.rb
@@ -0,0 +1,9 @@
class AddDonationIdToBricks < ActiveRecord::Migration
def self.up
add_column :bricks, :donation_id, :string
end

def self.down
remove_column :bricks, :donation_id
end
end

0 comments on commit 44fc2a2

Please sign in to comment.