Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 4365 Bytes
1 | ######################################################################## |
2 | # File:: payment_gateway_controller.rb |
3 | # (C):: Hipposoft 2010, 2011 |
4 | # |
5 | # Purpose:: Manage external payment gateways to process order payments. |
6 | # ---------------------------------------------------------------------- |
7 | # 17-Feb-2010 (ADH): Created. |
8 | # 30-Jan-2011 (ADH): Imported from Artisan. |
9 | ######################################################################## |
10 | |
11 | class PaymentGatewayController < ApplicationController |
12 | |
13 | include ActiveMerchant::Billing |
14 | |
15 | before_filter :ensure_user_is_valid_and_find_donation |
16 | before_filter :ensure_donation_is_valid_and_set_variables, :except => [ :edit, :delete ] |
17 | |
18 | # Cancel payment. |
19 | # |
20 | def delete |
21 | flash[ :notice ] = t( |
22 | :'uk.org.pond.canvass.controllers.payment_gateway_offsite.view_cancelled' |
23 | ) |
24 | |
25 | # Redirection to the cart items list should destroy the initial state |
26 | # donation and associated objects anyway, but do it explicitly here just |
27 | # to make absolutely sure. |
28 | |
29 | Donation.safely_destroy_initial_state_donations_for( current_user ) |
30 | redirect_to( root_path() ) |
31 | end |
32 | |
33 | protected |
34 | |
35 | # Set @notes to notes-for-sellers based on the current logged in user, |
36 | # donation found in filter "ensure_user_is_valid_and_find_donation" and, if |
37 | # provided, an external gateway response which includes an invoice ID |
38 | # containing the expected donation ID. Pass no parameters to rely on the |
39 | # current user always (@notes will then be "nil"). |
40 | # |
41 | # Returns 'true' if successful or 'false' on failure, in which case an |
42 | # error message has been set in the Flash and redirection has been requested. |
43 | # |
44 | # Called by a before_filter for most actions, so only really needed to be |
45 | # invoked directly if there's a gateway response to consider. |
46 | # |
47 | def ensure_donation_is_valid_and_set_variables( gateway_response = nil ) |
48 | @notes = nil |
49 | |
50 | # If the gateway gave us a response hash we can try to find a "special |
51 | # note to the seller" field and also make sure that the invoice ID given |
52 | # in the "new" method has been returned and matches our expected donation. |
53 | # Again set the donation value to nil to indicate an error if necessary. |
54 | |
55 | if ( ! @donation.nil? && ! gateway_response.nil? && gateway_response.params.is_a?( Hash ) ) |
56 | @notes = gateway_response.params[ "note" ] |
57 | iid = gateway_response.params[ "invoice_id" ] |
58 | |
59 | @donation = nil if ( ! iid.blank? && iid.to_i != @donation.id ) |
60 | end |
61 | |
62 | # Handle errors with early exit. |
63 | |
64 | if ( @donation.nil? ) |
65 | flash[ :error ] = t( |
66 | :'uk.org.pond.canvass.controllers.payment_gateway_offsite.error_donation_details_wrong' |
67 | ) |
68 | |
69 | redirect_to( root_path() ) |
70 | return false |
71 | end |
72 | |
73 | return true |
74 | end |
75 | |
76 | private |
77 | |
78 | # Call as a before_filter. Ensures that a user is logged in and sets |
79 | # "@donation" to the user's initial state Donation object. If this is |
80 | # "nil" afterwards, something went wrong; abort the process. |
81 | # |
82 | # This is used rather than a Hub permissions hash because we don't want to |
83 | # be asked to, say, log in if not already; all of that should be set up |
84 | # correctly already and if anything looks out of the ordinary, when it comes |
85 | # to payment and orders it is safest to just give up and redirect back to |
86 | # the root path (possibilities: User reloaded old page, auto-URL completion |
87 | # when typing in address directed them to unexpected location, bogus history |
88 | # choice, etc. etc.). |
89 | # |
90 | def ensure_user_is_valid_and_find_donation |
91 | redirect_to( root_path() ) and return unless ( logged_in? ) |
92 | |
93 | @donation = Donation.find( |
94 | :all, |
95 | :conditions => { |
96 | :user_id => current_user.id, |
97 | :workflow_state => Donation::STATE_INITIAL.to_s |
98 | } |
99 | ) |
100 | |
101 | # If we seem to get the wrong number of donations back - for a given user, |
102 | # exactly 1 should be in initial state, no more, no less - then reset the |
103 | # donation value to nil to indicate an error condition, else pick out the |
104 | # single array item. |
105 | |
106 | @donation = ( @donation.size != 1 ) ? nil : @donation[ 0 ] |
107 | end |
108 | |
109 | # Lazy-initialise and return a payment gateway instance based on the |
110 | # "PAYMENT_*" constants. |
111 | # |
112 | # For "PAYMENT_*", see "config/environments/<environment_in_use>.rb". |
113 | # |
114 | def gateway |
115 | @gateway ||= PaymentGateway.instance.gateway() |
116 | end |
117 | end |