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,
  '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

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)


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, 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?
  raise ArgumentError, 'You must pass a valid Rack application' unless rack_app.nil? or rack_app.respond_to?(:call)

  @app             = rack_app
  @gateway_options = options
  @gateway_type    = options['gateway'] || options[: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

.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:



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

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)


85
86
87
# File 'lib/rack-payment/payment.rb', line 85

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)


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

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)


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

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)


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

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)


147
148
149
# File 'lib/rack-payment/payment.rb', line 147

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)


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

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)


168
169
170
# File 'lib/rack-payment/payment.rb', line 168

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)


135
136
137
# File 'lib/rack-payment/payment.rb', line 135

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)


127
128
129
# File 'lib/rack-payment/payment.rb', line 127

def gateway_type
  @gateway_type
end

#loggerObject

:nodoc:



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

def logger
  @logger
end

#on_errorString?

When a payment is unsuccessful, we render this path, if set. If this is ‘nil`, we render the URL that the POST came from.

Returns:

  • (String, nil)

    (nil)



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

def on_error
  @on_error
end

#on_successString?

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

Returns:

  • (String, nil)

    (nil)



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

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)


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

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)


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

def session_variable
  @session_variable
end

#use_built_in_formObject

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.

Parameters:

  • The (Hash)

    Rack Request environment variables



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_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)


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_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)


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 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).



261
262
263
# File 'lib/rack-payment/payment.rb', line 261

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