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, '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_success ⇒ String?
When a payment is successful, we redirect to 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_application = 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.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rack-payment/payment.rb', line 184 def initialize rack_application = 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? @app = rack_application @gateway_options = # <---- need to remove *our* options from the gateway options! @gateway_type = ['gateway'] || [:gateway] raise ArgumentError, 'You must pass a valid Rack application' unless @app.nil? or @app.respond_to?(:call) raise ArgumentError, 'You must pass a valid Gateway' unless @gateway_type and gateway.is_a?(ActiveMerchant::Billing::Gateway) 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 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
59 60 61 |
# File 'lib/rack-payment/payment.rb', line 59 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
79 80 81 |
# File 'lib/rack-payment/payment.rb', line 79 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.
103 104 105 |
# File 'lib/rack-payment/payment.rb', line 103 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.
97 98 99 |
# File 'lib/rack-payment/payment.rb', line 97 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.
92 93 94 |
# File 'lib/rack-payment/payment.rb', line 92 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
141 142 143 |
# File 'lib/rack-payment/payment.rb', line 141 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.
87 88 89 |
# File 'lib/rack-payment/payment.rb', line 87 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.
162 163 164 |
# File 'lib/rack-payment/payment.rb', line 162 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
129 130 131 |
# File 'lib/rack-payment/payment.rb', line 129 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
121 122 123 |
# File 'lib/rack-payment/payment.rb', line 121 def gateway_type @gateway_type end |
#logger ⇒ Object
:nodoc:
64 65 66 |
# File 'lib/rack-payment/payment.rb', line 64 def logger @logger end |
#on_success ⇒ String?
When a payment is successful, we redirect to this path, if set. If this is ‘nil`, we display our own confirmation page.
73 74 75 |
# File 'lib/rack-payment/payment.rb', line 73 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)
115 116 117 |
# File 'lib/rack-payment/payment.rb', line 115 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.
110 111 112 |
# File 'lib/rack-payment/payment.rb', line 110 def session_variable @session_variable end |
#use_built_in_form ⇒ Object
TODO implement! NOT IMPLEMENTED YET
82 83 84 |
# File 'lib/rack-payment/payment.rb', line 82 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.
211 212 213 214 215 |
# File 'lib/rack-payment/payment.rb', line 211 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.
155 156 157 |
# File 'lib/rack-payment/payment.rb', line 155 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.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/rack-payment/payment.rb', line 220 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 |