Class: Spree::Gateway

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

Direct Known Subclasses

Bogus, CustomPaymentSourceMethod

Defined Under Namespace

Classes: Bogus, BogusSimple, CustomPaymentSourceMethod

Constant Summary collapse

FROM_DOLLAR_TO_CENT_RATE =
100.0

Constants included from DisplayOn

DisplayOn::DISPLAY

Instance Method Summary collapse

Methods inherited from PaymentMethod

#auto_capture?, #available_for_order?, #available_for_store?, #cancel, #default_name, find_with_destroyed, #payment_icon_name, #provider_class, providers, #public_preferences, #show_in_admin?, #source_required?, #store_credit?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

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



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

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



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

def exchange_multiplier
  FROM_DOLLAR_TO_CENT_RATE
end

#gateway_dashboard_payment_url(_payment) ⇒ Object

Override in the gateway to provide a payment url eg. for Stripe, this would be the payment intent url dashboard.stripe.com/payments/#Spree::Gateway.paymentpayment.transaction_id



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

def gateway_dashboard_payment_url(_payment)
  nil
end

#method_typeObject



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

def method_type
  'gateway'
end

#optionsObject



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

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

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


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

def payment_profiles_supported?
  false
end

#payment_source_classObject



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

def payment_source_class
  CreditCard
end

#providerObject



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

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



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/spree/gateway.rb', line 73

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

#sources_by_order(order) ⇒ Object



68
69
70
71
# File 'app/models/spree/gateway.rb', line 68

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

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  provider_class.supports?(source.brand)
end