rack-payment 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,14 @@ module Rack #:nodoc:
30
30
 
31
31
  attr_accessor :rack_payment, :amount, :credit_card, :billing_address, :errors, :use_express, :response
32
32
 
33
+ # Specifies what page to render if payment fails (for this specific request).
34
+ # To set this option globally, see {Rack::Payment#on_error}.
35
+ attr_accessor :on_error
36
+
37
+ # Specifies what page to render if payment succeeds (for this specific request).
38
+ # To set this option globally, see {Rack::Payment#on_success}.
39
+ attr_accessor :on_success
40
+
33
41
  # @param [Rack::Payment]
34
42
  def initialize rack_payment
35
43
  @rack_payment = rack_payment
@@ -45,6 +45,7 @@ module Rack #:nodoc:
45
45
  #
46
46
  DEFAULT_OPTIONS = {
47
47
  'on_success' => nil,
48
+ 'on_error' => nil,
48
49
  'built_in_form_path' => '/rack.payment/process',
49
50
  'express_ok_path' => '/rack.payment/express.callback/ok',
50
51
  'express_cancel_path' => '/rack.payment/express.callback/cancel',
@@ -67,11 +68,16 @@ module Rack #:nodoc:
67
68
  @logger ||= Rack::Payment.logger
68
69
  end
69
70
 
70
- # When a payment is successful, we redirect to this path, if set.
71
+ # When a payment is successful, we render this path, if set.
71
72
  # If this is `nil`, we display our own confirmation page.
72
73
  # @return [String, nil] (nil)
73
74
  attr_accessor :on_success
74
75
 
76
+ # When a payment is unsuccessful, we render this path, if set.
77
+ # If this is `nil`, we render the URL that the POST came from.
78
+ # @return [String, nil] (nil)
79
+ attr_accessor :on_error
80
+
75
81
  # This is the path that the built-in form POSTs to when submitting
76
82
  # Credit Card data. This is only used if you use the built_in_form.
77
83
  # See {#use_built_in_form} to enable/disable using the default form
@@ -9,10 +9,9 @@ module Rack #:nodoc:
9
9
  class Request
10
10
  extend Forwardable
11
11
 
12
- def_delegators :payment_instance, :app, :gateway, :express_gateway, :on_success, :built_in_form_path,
12
+ def_delegators :payment_instance, :app, :gateway, :express_gateway, :built_in_form_path,
13
13
  :env_instance_variable, :env_helper_variable, :session_variable,
14
- :rack_session_variable, :express_ok_path, :express_cancel_path,
15
- :built_in_form_path
14
+ :rack_session_variable, :express_ok_path, :express_cancel_path
16
15
 
17
16
  def_delegators :request, :params
18
17
 
@@ -94,6 +93,7 @@ module Rack #:nodoc:
94
93
  #
95
94
  # @return [Array] A Rack response, eg. `[200, {}, ["Hello World"]]`
96
95
  def finish
96
+ update_credit_card_and_billing_address_from_params
97
97
 
98
98
  # The application returned a 402 ('Payment Required')
99
99
  if app_response.status == 402
@@ -123,11 +123,12 @@ module Rack #:nodoc:
123
123
  app_response.finish
124
124
  end
125
125
 
126
- # Gets parameters, attempts an #authorize call, attempts a #capture call,
127
- # and renders the results.
128
- def process_credit_card
129
- payment.amount ||= amount_in_session
130
-
126
+ # If the params include fields that start with credit_card_ or
127
+ # the params have a hash of credit card variables, we use them
128
+ # to update our credit card information.
129
+ #
130
+ # We do the same with the billing address.
131
+ def update_credit_card_and_billing_address_from_params
131
132
  # The params *should* be set on the payment data object, but we accept
132
133
  # POST requests too, so we check the POST variables for credit_card
133
134
  # or billing_address fields
@@ -149,6 +150,14 @@ module Rack #:nodoc:
149
150
  if params['billing_address'] and params['billing_address'].respond_to?(:each)
150
151
  payment.billing_address.update params['billing_address']
151
152
  end
153
+ end
154
+
155
+ # Gets parameters, attempts an #authorize call, attempts a #capture call,
156
+ # and renders the results.
157
+ def process_credit_card
158
+ payment.amount ||= amount_in_session
159
+
160
+ update_credit_card_and_billing_address_from_params
152
161
 
153
162
  # Purchase!
154
163
  if payment.purchase(:ip => request.ip)
@@ -183,34 +192,42 @@ module Rack #:nodoc:
183
192
  # so we can actually just re-call the same env (should display the form) using a GET
184
193
  payment.errors = errors
185
194
  new_env = env.clone
195
+ cleanup_env_for_rails(new_env)
186
196
  new_env['REQUEST_METHOD'] = 'GET'
187
197
 
188
- # Rails keeps track of its own Request/Response.
189
- #
190
- # If we're using Rails, we need to delete these variables
191
- # to trick Rails into thinking that this is a new request.
192
- #
193
- # Kind of icky!
194
- new_env.delete 'action_controller.rescue.request'
195
- new_env.delete 'action_controller.rescue.response'
198
+ if request.path_info == express_ok_path # if express, we render on_success
199
+ new_env['PATH_INFO'] = (payment.on_success || payment_instance.on_success)
200
+ elsif payment.on_error
201
+ new_env['PATH_INFO'] = payment.on_error # Specific to this request
202
+ elsif payment_instance.on_error
203
+ new_env['PATH_INFO'] = payment_instance.on_error # Global
204
+ end
196
205
 
197
- new_env['PATH_INFO'] = on_success if request.path_info == express_ok_path # if express, we render on_success
198
206
  app.call(new_env)
199
207
  end
200
208
  end
201
209
 
210
+ # Rails keeps track of its own Request/Response.
211
+ #
212
+ # If we're using Rails, we need to delete these variables
213
+ # to trick Rails into thinking that this is a new request.
214
+ #
215
+ # Kind of icky!
216
+ def cleanup_env_for_rails new_env
217
+ new_env.delete 'action_controller.rescue.request'
218
+ new_env.delete 'action_controller.rescue.response'
219
+ new_env.delete 'REQUEST_URI'
220
+ end
221
+
202
222
  def render_on_success
223
+ on_success = (payment.on_success || payment_instance.on_success)
224
+
203
225
  if on_success
204
226
  # on_success is overriden ... we #call the main application using the on_success path
205
227
  new_env = env.clone
228
+ cleanup_env_for_rails(new_env)
206
229
  new_env['PATH_INFO'] = on_success
207
230
  new_env['REQUEST_METHOD'] = 'GET'
208
-
209
- # For Rails
210
- new_env.delete 'action_controller.rescue.request'
211
- new_env.delete 'action_controller.rescue.response'
212
- new_env.delete 'REQUEST_URI'
213
-
214
231
  app.call new_env
215
232
  else
216
233
  # on_success has not been overriden ... let's just display out own info
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-payment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - remi