Class: Rack::Payment
- Inherits:
-
Object
- Object
- Rack::Payment
- Defined in:
- lib/rack-payment/helper.rb,
lib/rack-payment/methods.rb,
lib/rack-payment/payment.rb,
lib/rack-payment/request.rb,
lib/rack-payment/response.rb,
lib/rack-payment/credit_card.rb,
lib/rack-payment/billing_address.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Methods Classes: BillingAddress, CreditCard, Helper, Request, Response
Constant Summary collapse
- YML_FILE_NAMES =
Default file names that we used to look for yml configuration. You can change yml_file_names to override.
%w( .rack-payment.yml rack-payment.yml config/rack-payment.yml ../config/rack-payment payment.yml ../payment.yml config/payment.yml )
- DEFAULT_OPTIONS =
{ 'on_success' => nil, 'on_error' => nil, 'built_in_form_path' => '/rack.payment/process', 'express_ok_path' => '/rack.payment/express.callback/ok', 'express_cancel_path' => '/rack.payment/express.callback/cancel', 'env_instance_variable' => 'rack.payment', 'env_helper_variable' => 'rack.payment.helper', 'session_variable' => 'rack.payment', 'rack_session_variable' => 'rack.session' }
Class Attribute Summary collapse
-
.logger ⇒ Logger
A standard logger.
-
.yml_file_names ⇒ Array(String)
A string of file names that we use to look for options, if options are not passes to the Rack::Payment constructor.
Instance Attribute Summary collapse
-
#app ⇒ #call
The Rack application that this middleware was instantiated with.
-
#built_in_form_path ⇒ String
This is the path that the built-in form POSTs to when submitting Credit Card data.
-
#env_helper_variable ⇒ String
The name of the Rack env variable to use to access data about the purchase being made.
-
#env_instance_variable ⇒ String
The name of the Rack env variable to use to access the instance of Rack::Payment that your application is using as middleware.
-
#express_cancel_path ⇒ String
This is the path that we have express gateways (Paypal Express) redirect to if the user cancels their purchase.
-
#express_gateway ⇒ ActiveMerchant::Billing::Gateway
Uses the #gateway_options to instantiate a [paypal] express gateway.
-
#express_ok_path ⇒ String
This is the path that we have express gateways (Paypal Express) redirect to, after a purchase has been made.
-
#gateway ⇒ ActiveMerchant::Billing::Gateway
The actual instance of ActiveMerchant::Billing::Gateway object to use.
- #gateway_options ⇒ Hash
-
#gateway_type ⇒ String
The name of a type of ActiveMerchant::Billing::Gateway that we want to use, eg.
-
#logger ⇒ Object
:nodoc:.
-
#on_error ⇒ String?
When a payment is unsuccessful, we render this path, if set.
-
#on_success ⇒ String?
When a payment is successful, we render this path, if set.
-
#rack_session_variable ⇒ String
The name of the Rack env variable used for the Rack::Session, eg.
-
#session_variable ⇒ String
The name of the variable we put into the Rack::Session to store anything that Payment needs to keep track of between requests, eg.
-
#use_built_in_form ⇒ Object
TODO implement! NOT IMPLEMENTED YET.
Instance Method Summary collapse
-
#call(env) ⇒ Object
The main Rack #call method required by every Rack application / middleware.
-
#express_gateway_type ⇒ String
The name of the gateway to use for an express gateway.
-
#initialize(rack_app = nil, options = nil) ⇒ Payment
constructor
A new instance of Payment.
-
#look_for_options_in_a_yml_file ⇒ Hash
Looks for options in a yml file with a conventional name (using Rack::Payment.yml_file_names) Returns an empty Hash, if no options are found from a yml file.
-
#payment ⇒ Rack::Payment::Helper
Returns a new Helper instance which can be used to fire off single payments (without needing to make web requests).
Constructor Details
#initialize(rack_application) ⇒ Payment #initialize(rack_application, options) ⇒ Payment
Returns a new instance of Payment.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/rack-payment/payment.rb', line 190 def initialize rack_app = nil, = nil ||= {} = .merge() unless ['yml_config'] == false or [:yml_config] == false raise ArgumentError, "You must pass options (or put them in a yml file)." if .empty? raise ArgumentError, 'You must pass a valid Rack application' unless rack_app.nil? or rack_app.respond_to?(:call) @app = rack_app @gateway_options = @gateway_type = ['gateway'] || [:gateway] # Before trying to instantiate a gateway using these options, let's remove *our* options so we # don't end up passing them to the gateway's constructor, possibly blowing it up! @test_mode = @gateway_options.delete(:test_mode) if @gateway_options.keys.include?(:test_mode) @test_mode = @gateway_options.delete('test_mode') if @gateway_options.keys.include?('test_mode') ActiveMerchant::Billing::Base.mode = (@test_mode == false) ? :production : :test unless @test_mode.nil? DEFAULT_OPTIONS.each do |name, value| # set the default send "#{name}=", value # override the value from options, if passed if @gateway_options[name.to_s] send "#{name.to_s}=", @gateway_options.delete(name.to_s) elsif @gateway_options[name.to_s.to_sym] send "#{name.to_s.to_sym}=", @gateway_options.delete(name.to_s.to_sym) end end # Now we check to see if the gateway is OK raise ArgumentError, 'You must pass a valid Gateway' unless @gateway_type and gateway.is_a?(ActiveMerchant::Billing::Gateway) end |
Class Attribute Details
.logger ⇒ Logger
A standard logger. Defaults to nil. We assume that this has methods like #info that accept a String or a block.
If this is set, new instances of Rack::Payment will use this logger by default.
34 35 36 |
# File 'lib/rack-payment/payment.rb', line 34 def logger @logger end |
.yml_file_names ⇒ Array(String)
A string of file names that we use to look for options, if options are not passes to the Rack::Payment constructor.
26 27 28 |
# File 'lib/rack-payment/payment.rb', line 26 def yml_file_names @yml_file_names end |
Instance Attribute Details
#app ⇒ #call
The Rack application that this middleware was instantiated with
60 61 62 |
# File 'lib/rack-payment/payment.rb', line 60 def app @app end |
#built_in_form_path ⇒ String
This is the path that the built-in form POSTs to when submitting Credit Card data. This is only used if you use the built_in_form. See #use_built_in_form to enable/disable using the default form
85 86 87 |
# File 'lib/rack-payment/payment.rb', line 85 def built_in_form_path @built_in_form_path end |
#env_helper_variable ⇒ String
The name of the Rack env variable to use to access data about the purchase being made. Getting this out of the Rack env gives you a Helper object.
109 110 111 |
# File 'lib/rack-payment/payment.rb', line 109 def env_helper_variable @env_helper_variable end |
#env_instance_variable ⇒ String
The name of the Rack env variable to use to access the instance of Rack::Payment that your application is using as middleware.
103 104 105 |
# File 'lib/rack-payment/payment.rb', line 103 def env_instance_variable @env_instance_variable end |
#express_cancel_path ⇒ String
This is the path that we have express gateways (Paypal Express) redirect to if the user cancels their purchase.
98 99 100 |
# File 'lib/rack-payment/payment.rb', line 98 def express_cancel_path @express_cancel_path end |
#express_gateway ⇒ ActiveMerchant::Billing::Gateway
Uses the #gateway_options to instantiate a [paypal] express gateway
If your main gateway is a PaypayGateway, we’ll make a PaypalExpressGateway If your main gateway is a BogusGateway, we’ll make a BogusExpressGateway
For any gateway, we’ll try to make a *ExpressGateway
This ONLY works for classes underneath ActiveMerchant::Billing
147 148 149 |
# File 'lib/rack-payment/payment.rb', line 147 def express_gateway @express_gateway end |
#express_ok_path ⇒ String
This is the path that we have express gateways (Paypal Express) redirect to, after a purchase has been made.
93 94 95 |
# File 'lib/rack-payment/payment.rb', line 93 def express_ok_path @express_ok_path end |
#gateway ⇒ ActiveMerchant::Billing::Gateway
The actual instance of ActiveMerchant::Billing::Gateway object to use. Uses the #gateway_type and #gateway_options to instantiate a gateway.
168 169 170 |
# File 'lib/rack-payment/payment.rb', line 168 def gateway @gateway end |
#gateway_options ⇒ Hash
The options that are passed to Rack::Payment when you include it as a middleware, minus the options that Rack::Payment uses.
For example, if you instantiate a Rack::Payment middleware with Paypal, this will probably include :login, :password, and :signature
135 136 137 |
# File 'lib/rack-payment/payment.rb', line 135 def @gateway_options end |
#gateway_type ⇒ String
The name of a type of ActiveMerchant::Billing::Gateway that we want to use, eg. ‘paypal’. We use this to get the actual ActiveMerchant::Billing::Gateway class, eg. ActiveMerchant::Billing::Paypal
127 128 129 |
# File 'lib/rack-payment/payment.rb', line 127 def gateway_type @gateway_type end |
#logger ⇒ Object
:nodoc:
65 66 67 |
# File 'lib/rack-payment/payment.rb', line 65 def logger @logger end |
#on_error ⇒ String?
When a payment is unsuccessful, we render this path, if set. If this is ‘nil`, we render the URL that the POST came from.
79 80 81 |
# File 'lib/rack-payment/payment.rb', line 79 def on_error @on_error end |
#on_success ⇒ String?
When a payment is successful, we render this path, if set. If this is ‘nil`, we display our own confirmation page.
74 75 76 |
# File 'lib/rack-payment/payment.rb', line 74 def on_success @on_success end |
#rack_session_variable ⇒ String
The name of the Rack env variable used for the Rack::Session, eg. ‘rack.session` (the default for Rack::Session::Cookie)
121 122 123 |
# File 'lib/rack-payment/payment.rb', line 121 def rack_session_variable @rack_session_variable end |
#session_variable ⇒ String
The name of the variable we put into the Rack::Session to store anything that Rack::Payment needs to keep track of between requests, eg. the amount that the user is trying to spend.
116 117 118 |
# File 'lib/rack-payment/payment.rb', line 116 def session_variable @session_variable end |
#use_built_in_form ⇒ Object
TODO implement! NOT IMPLEMENTED YET
88 89 90 |
# File 'lib/rack-payment/payment.rb', line 88 def use_built_in_form @use_built_in_form end |
Instance Method Details
#call(env) ⇒ Object
The main Rack #call method required by every Rack application / middleware.
225 226 227 228 229 |
# File 'lib/rack-payment/payment.rb', line 225 def call env raise "Rack::Payment was not initialized with a Rack application and cannot be #call'd" unless @app env[env_instance_variable] ||= self # make this instance available return Request.new(env, self).finish # a Request object actually returns the response end |
#express_gateway_type ⇒ String
The name of the gateway to use for an express gateway.
If our #gateway is a ActiveMerchant::Billing::PaypalGateway, this will return ‘paypal_express`
Uses the class of #gateway to determine.
161 162 163 |
# File 'lib/rack-payment/payment.rb', line 161 def express_gateway_type gateway.class.to_s.split('::').last.sub(/(\w+)Gateway$/, '\1_express') end |
#look_for_options_in_a_yml_file ⇒ Hash
Looks for options in a yml file with a conventional name (using Rack::Payment.yml_file_names) Returns an empty Hash, if no options are found from a yml file.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/rack-payment/payment.rb', line 234 def Rack::Payment.yml_file_names.each do |filename| if ::File.file?(filename) = YAML.load_file(filename) # if the YAML loaded something and it's a Hash if and .is_a?(Hash) # handle RACK_ENV / RAILS_ENV so you can put your test/development/etc in the same file if ENV['RACK_ENV'] and [ENV['RACK_ENV']].is_a?(Hash) = [ENV['RACK_ENV']] elsif ENV['RAILS_ENV'] and [ENV['RAILS_ENV']].is_a?(Hash) = [ENV['RAILS_ENV']] end return end end end return {} end |