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 collapse

FROM_DOLLAR_TO_CENT_RATE =
100.0

Constants inherited from PaymentMethod

PaymentMethod::DISPLAY

Instance Method Summary collapse

Methods inherited from PaymentMethod

#auto_capture?, #available_for_order?, #cancel, find_with_destroyed, #provider_class, providers, #source_required?, #store_credit?

Methods inherited from Base

belongs_to_required_by_default, page, spree_base_scopes

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object


29
30
31
32
33
34
35
# File 'app/models/spree/gateway.rb', line 29

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

Instance Method Details

#disable_customer_profile(source) ⇒ Object


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

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

#exchange_multiplierObject


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

def exchange_multiplier
  FROM_DOLLAR_TO_CENT_RATE
end

#method_typeObject


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

def method_type
  'gateway'
end

#optionsObject


25
26
27
# File 'app/models/spree/gateway.rb', line 25

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

#payment_profiles_supported?Boolean


37
38
39
# File 'app/models/spree/gateway.rb', line 37

def payment_profiles_supported?
  false
end

#payment_source_classObject


12
13
14
# File 'app/models/spree/gateway.rb', line 12

def payment_source_class
  CreditCard
end

#providerObject


16
17
18
19
20
21
22
23
# File 'app/models/spree/gateway.rb', line 16

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

#reusable_sources(order) ⇒ Object


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

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

#sources_by_order(order) ⇒ Object


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

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

#supports?(source) ⇒ Boolean


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

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