Class: Spree::PaymentMethod::StoreCredit

Inherits:
Spree::PaymentMethod show all
Defined in:
app/models/spree/payment_method/store_credit.rb

Constant Summary

Constants included from DisplayOn

DisplayOn::DISPLAY

Instance Method Summary collapse

Methods inherited from Spree::PaymentMethod

#auto_capture?, #available_for_store?, #confirmation_required?, #default_name, find_with_destroyed, #payment_icon_name, #payment_profiles_supported?, #provider_class, providers, #public_preferences, #reusable_sources, #show_in_admin?, #store_credit?, #supports?

Instance Method Details

#authorize(amount_in_cents, store_credit, gateway_options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/spree/payment_method/store_credit.rb', line 23

def authorize(amount_in_cents, store_credit, gateway_options = {})
  if store_credit.nil?
    ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})
  else
    action = lambda do |store_credit|
      store_credit.authorize(
        amount_in_cents / 100.0.to_d,
        gateway_options[:currency],
        action_originator: gateway_options[:originator]
      )
    end
    handle_action_call(store_credit, action, :authorize)
  end
end

#available_for_order?(order) ⇒ Boolean

Returns:



104
105
106
# File 'app/models/spree/payment_method/store_credit.rb', line 104

def available_for_order?(order)
  order.gift_card.present? || order.could_use_store_credit?
end

#can_capture?(payment) ⇒ Boolean

Returns:



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

def can_capture?(payment)
  ['checkout', 'pending'].include?(payment.state)
end

#can_void?(payment) ⇒ Boolean

Returns:



19
20
21
# File 'app/models/spree/payment_method/store_credit.rb', line 19

def can_void?(payment)
  payment.pending?
end

#cancel(auth_code, _payment = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/spree/payment_method/store_credit.rb', line 85

def cancel(auth_code, _payment = nil)
  store_credit_event = StoreCreditEvent.find_by(authorization_code: auth_code,
                                                action: Spree::StoreCredit::CAPTURE_ACTION)
  store_credit = store_credit_event.try(:store_credit)

  if !store_credit_event || !store_credit
    handle_action(nil, :cancel, false)
  else
    action = lambda do |store_credit|
      store_credit.credit(store_credit_event.amount, auth_code, store_credit.currency)
    end
    handle_action(action, :cancel, auth_code)
  end
end

#capture(amount_in_cents, auth_code, gateway_options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/payment_method/store_credit.rb', line 38

def capture(amount_in_cents, auth_code, gateway_options = {})
  action = lambda do |store_credit|
    store_credit.capture(
      amount_in_cents / 100.0.to_d,
      auth_code,
      gateway_options[:currency],
      action_originator: gateway_options[:originator]
    )
  end
  handle_action(action, :capture, auth_code)
end

#credit(amount_in_cents, auth_code, gateway_options) ⇒ Object



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

def credit(amount_in_cents, auth_code, gateway_options)
  action = lambda do |store_credit|
    currency = gateway_options[:currency] || store_credit.currency
    originator = gateway_options[:originator]

    store_credit.credit(amount_in_cents / 100.0.to_d, auth_code, currency, action_originator: originator)
  end

  handle_action(action, :credit, auth_code)
end

#description_partial_nameObject



11
12
13
# File 'app/models/spree/payment_method/store_credit.rb', line 11

def description_partial_name
  'store_credit'
end

#method_typeObject



7
8
9
# File 'app/models/spree/payment_method/store_credit.rb', line 7

def method_type
  'store_credit'
end

#payment_source_classObject



3
4
5
# File 'app/models/spree/payment_method/store_credit.rb', line 3

def payment_source_class
  ::Spree::StoreCredit
end

#purchase(amount_in_cents, store_credit, gateway_options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/spree/payment_method/store_credit.rb', line 50

def purchase(amount_in_cents, store_credit, gateway_options = {})
  eligible_events = store_credit.store_credit_events.where(
    amount: amount_in_cents / 100.0.to_d,
    action: Spree::StoreCredit::ELIGIBLE_ACTION
  )
  event = eligible_events.detect do |eligible_event|
    store_credit.store_credit_events.where(authorization_code: eligible_event.authorization_code).
      where.not(action: Spree::StoreCredit::ELIGIBLE_ACTION).empty?
  end

  if event.blank?
    ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})
  else
    capture(amount_in_cents, event.authorization_code, gateway_options)
  end
end

#source_required?Boolean

Returns:



100
101
102
# File 'app/models/spree/payment_method/store_credit.rb', line 100

def source_required?
  true
end

#void(auth_code, gateway_options = {}) ⇒ Object



67
68
69
70
71
72
# File 'app/models/spree/payment_method/store_credit.rb', line 67

def void(auth_code, gateway_options = {})
  action = lambda do |store_credit|
    store_credit.void(auth_code, action_originator: gateway_options[:originator])
  end
  handle_action(action, :void, auth_code)
end