Class: Spree::Gateway

Inherits:
PaymentMethod show all
Defined in:
app/models/spree/gateway.rb

Direct Known Subclasses

Bogus

Defined Under Namespace

Classes: Bogus, BogusSimple

Constant Summary

Constants inherited from PaymentMethod

PaymentMethod::DISPLAY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PaymentMethod

active?, #auto_capture?, available, find_with_destroyed, #provider_class, providers, #source_required?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



32
33
34
35
36
37
38
# File 'app/models/spree/gateway.rb', line 32

def method_missing(method, *args)
  if @provider.nil? || !@provider.respond_to?(method)
    super
  else
    provider.send(method, *args)
  end
end

Class Method Details

.currentObject

instantiates the selected gateway and configures with the options stored in the database



15
16
17
# File 'app/models/spree/gateway.rb', line 15

def self.current
  super
end

Instance Method Details

#disable_customer_profile(source) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/spree/gateway.rb', line 54

def disable_customer_profile(source)
  if source.is_a? CreditCard
    source.update_column :gateway_customer_profile_id, nil
  else
    raise 'You must implement disable_customer_profile method for this gateway.'
  end
end

#method_typeObject



44
45
46
# File 'app/models/spree/gateway.rb', line 44

def method_type
  'gateway'
end

#optionsObject



28
29
30
# File 'app/models/spree/gateway.rb', line 28

def options
  self.preferences.inject({}){ |memo, (key, value)| memo[key.to_sym] = value; memo }
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/spree/gateway.rb', line 40

def payment_profiles_supported?
  false
end

#payment_source_classObject



10
11
12
# File 'app/models/spree/gateway.rb', line 10

def payment_source_class
  CreditCard
end

#providerObject



19
20
21
22
23
24
25
26
# File 'app/models/spree/gateway.rb', line 19

def provider
  gateway_options = options
  gateway_options.delete :login if gateway_options.has_key?(:login) and gateway_options[:login].nil?
  if gateway_options[:server]
    ActiveMerchant::Billing::Base.gateway_mode = gateway_options[:server].to_sym
  end
  @provider ||= provider_class.new(gateway_options)
end

#reusable_sources(order) ⇒ Object Also known as: source_with_profiles



67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/spree/gateway.rb', line 67

def reusable_sources(order)
  if order.completed?
    sources_by_order order
  else
    if order.user_id
      self.credit_cards.where(user_id: order.user_id).with_payment_profile
    else
      []
    end
  end
end

#sources_by_order(order) ⇒ Object



62
63
64
65
# File 'app/models/spree/gateway.rb', line 62

def sources_by_order(order)
  source_ids = order.payments.where(source_type: payment_source_class.to_s, payment_method_id: self.id).pluck(:source_id).uniq
  payment_source_class.where(id: source_ids).with_payment_profile
end

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'app/models/spree/gateway.rb', line 48

def supports?(source)
  return true unless provider_class.respond_to? :supports?
  return false unless source.brand
  provider_class.supports?(source.brand)
end