Class: Rack::Payment

Inherits:
Object
  • Object
show all
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 =

These are the default values that we use to set the Rack::Payment attributes.

These can all be overriden by passing the attribute name and new value to the Rack::Payment constructor:

use Rack::Payment, :on_success => '/my-custom-page'
{
  '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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rack_application) ⇒ Payment #initialize(rack_application, options) ⇒ Payment

Returns a new instance of Payment.

Overloads:

  • #initialize(rack_application) ⇒ Payment

    Not yet implemented. This will search for a YML file or ENV variable to set options.

    Parameters:

    • The (#call)

      Rack application for this middleware

  • #initialize(rack_application, options) ⇒ Payment

    Accepts a Hash of options where the :gateway option is used as the #gateway_type

    Parameters:

    • The (#call)

      Rack application for this middleware

    • Options (Hash)

      for the gateway and for Rack::Payment

Raises:

  • (ArgumentError)


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, options = nil
  options ||= {}
  options = look_for_options_in_a_yml_file.merge(options) unless options['yml_config'] == false or options[:yml_config] == false
  raise ArgumentError, "You must pass options (or put them in a yml file)." if options.empty?

  @app             = rack_application
  @gateway_options = options             # <---- need to remove *our* options from the gateway options!
  @gateway_type    = options['gateway'] || options[: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

.loggerLogger

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.

Returns:

  • (Logger)


34
35
36
# File 'lib/rack-payment/payment.rb', line 34

def logger
  @logger
end

.yml_file_namesArray(String)

A string of file names that we use to look for options, if options are not passes to the Rack::Payment constructor.

Returns:

  • (Array(String))


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

Returns:



59
60
61
# File 'lib/rack-payment/payment.rb', line 59

def app
  @app
end

#built_in_form_pathString

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

Returns:

  • (String)


79
80
81
# File 'lib/rack-payment/payment.rb', line 79

def built_in_form_path
  @built_in_form_path
end

#env_helper_variableString

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.

Returns:

  • (String)


103
104
105
# File 'lib/rack-payment/payment.rb', line 103

def env_helper_variable
  @env_helper_variable
end

#env_instance_variableString

The name of the Rack env variable to use to access the instance of Rack::Payment that your application is using as middleware.

Returns:

  • (String)


97
98
99
# File 'lib/rack-payment/payment.rb', line 97

def env_instance_variable
  @env_instance_variable
end

#express_cancel_pathString

This is the path that we have express gateways (Paypal Express) redirect to if the user cancels their purchase.

Returns:

  • (String)


92
93
94
# File 'lib/rack-payment/payment.rb', line 92

def express_cancel_path
  @express_cancel_path
end

#express_gatewayActiveMerchant::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

Returns:

  • (ActiveMerchant::Billing::Gateway)


141
142
143
# File 'lib/rack-payment/payment.rb', line 141

def express_gateway
  @express_gateway
end

#express_ok_pathString

This is the path that we have express gateways (Paypal Express) redirect to, after a purchase has been made.

Returns:

  • (String)


87
88
89
# File 'lib/rack-payment/payment.rb', line 87

def express_ok_path
  @express_ok_path
end

#gatewayActiveMerchant::Billing::Gateway

The actual instance of ActiveMerchant::Billing::Gateway object to use. Uses the #gateway_type and #gateway_options to instantiate a gateway.

Returns:

  • (ActiveMerchant::Billing::Gateway)


162
163
164
# File 'lib/rack-payment/payment.rb', line 162

def gateway
  @gateway
end

#gateway_optionsHash

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

Returns:

  • (Hash)


129
130
131
# File 'lib/rack-payment/payment.rb', line 129

def gateway_options
  @gateway_options
end

#gateway_typeString

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

Returns:

  • (String)


121
122
123
# File 'lib/rack-payment/payment.rb', line 121

def gateway_type
  @gateway_type
end

#loggerObject

:nodoc:



64
65
66
# File 'lib/rack-payment/payment.rb', line 64

def logger
  @logger
end

#on_successString?

When a payment is successful, we redirect to this path, if set. If this is ‘nil`, we display our own confirmation page.

Returns:

  • (String, nil)

    (nil)



73
74
75
# File 'lib/rack-payment/payment.rb', line 73

def on_success
  @on_success
end

#rack_session_variableString

The name of the Rack env variable used for the Rack::Session, eg. ‘rack.session` (the default for Rack::Session::Cookie)

Returns:

  • (String)


115
116
117
# File 'lib/rack-payment/payment.rb', line 115

def rack_session_variable
  @rack_session_variable
end

#session_variableString

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.

Returns:

  • (String)


110
111
112
# File 'lib/rack-payment/payment.rb', line 110

def session_variable
  @session_variable
end

#use_built_in_formObject

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.

Parameters:

  • The (Hash)

    Rack Request environment variables



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_typeString

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.

Returns:

  • (String)


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_fileHash

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.

Returns:

  • (Hash)


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 look_for_options_in_a_yml_file
  Rack::Payment.yml_file_names.each do |filename|
    if ::File.file?(filename)
      options = YAML.load_file(filename)

      # if the YAML loaded something and it's a Hash
      if options and options.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 options[ENV['RACK_ENV']].is_a?(Hash)
          options = options[ENV['RACK_ENV']]
        elsif ENV['RAILS_ENV'] and options[ENV['RAILS_ENV']].is_a?(Hash)
          options = options[ENV['RAILS_ENV']]
        end

        return options
      end
    end
  end

  return {}
end

#paymentRack::Payment::Helper

Returns a new Helper instance which can be used to fire off single payments (without needing to make web requests).



247
248
249
# File 'lib/rack-payment/payment.rb', line 247

def payment
  Rack::Payment::Helper.new(self)
end