Class: Spree::PaymentMethod::StoreCredit

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

Constant Summary

Constants included from CaptureMethod

CaptureMethod::CAPTURE_METHODS, CaptureMethod::DEFAULT_CAPTURE_METHOD

Instance Method Summary collapse

Methods inherited from Spree::PaymentMethod

#auto_capture?, #available_for_store?, #available_on_front_end?, #cancel_payment_session, #complete_payment_session, #complete_payment_setup_session, #confirmation_required?, #create_payment_session, #create_payment_setup_session, #default_name, #display_on, #display_on=, find_with_destroyed, #gateway_amount, inherited, #modification_record, #parse_webhook_event, #payment_icon_name, #payment_profiles_supported?, #payment_session_class, #payment_setup_session_class, #provider_class, provider_listing, providers, #public_preferences, #resolved_capture_method, #reusable_sources, #risk_codes_for, #session_required?, #setup_session_supported?, #show_in_admin?, #store_credit?, #supports?, third_party?, #update_payment_session, #webhook_url

Methods included from CaptureMethod

#capture_at_checkout?, #capture_manually?, #capture_on_dispatch?

Methods included from SecretPreferences

#separated_secret_preferences

Methods included from Metadata

#private_metadata, #private_metadata=

Methods included from StorePreferences

current, #preference_store, read, #store_preference

Instance Method Details

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree/payment_method/store_credit.rb', line 27

def authorize(money, store_credit, gateway_options = {})
  if store_credit.nil?
    Spree::PaymentResponse.new(false, I18n.t('spree.store_credit_payment_method.unable_to_find'), {}, {})
  else
    action = lambda do |store_credit|
      store_credit.authorize(
        money.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:

  • (Boolean)


116
117
118
# File 'app/models/spree/payment_method/store_credit.rb', line 116

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

#can_capture?(payment) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/models/spree/payment_method/store_credit.rb', line 17

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

#can_void?(payment) ⇒ Boolean

Mirrors Spree::StoreCredit#can_void?: a checkout payment can still be voided while its order is incomplete.

Returns:

  • (Boolean)


23
24
25
# File 'app/models/spree/payment_method/store_credit.rb', line 23

def can_void?(payment)
  payment.pending? || (payment.checkout? && !payment.order&.completed?)
end

#cancel(auth_code, _payment = nil, refund: true) ⇒ Object

Credit goes back to the customer's balance rather than a card, so this is a release rather than a refund and refund: does not apply.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/spree/payment_method/store_credit.rb', line 97

def cancel(auth_code, _payment = nil, refund: true)
  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

#cancel_voids?(_payment) ⇒ Boolean

Cancelling gives the credit back, so the payment is void even when it was captured.

Returns:

  • (Boolean)


91
92
93
# File 'app/models/spree/payment_method/store_credit.rb', line 91

def cancel_voids?(_payment)
  true
end

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



42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/spree/payment_method/store_credit.rb', line 42

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

#credit(money, auth_code, gateway_options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'app/models/spree/payment_method/store_credit.rb', line 78

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

    store_credit.credit(money.to_d, auth_code, currency, action_originator: originator)
  end

  handle_action(action, :credit, auth_code)
end

#description_partial_name ⇒ Object



13
14
15
# File 'app/models/spree/payment_method/store_credit.rb', line 13

def description_partial_name
  'store_credit'
end

#method_type ⇒ Object



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

def method_type
  'store_credit'
end

#payment_source_class ⇒ Object



5
6
7
# File 'app/models/spree/payment_method/store_credit.rb', line 5

def payment_source_class
  ::Spree::StoreCredit
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/spree/payment_method/store_credit.rb', line 54

def purchase(money, store_credit, gateway_options = {})
  eligible_events = store_credit.store_credit_events.where(
    amount: money.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?
    Spree::PaymentResponse.new(false, I18n.t('spree.store_credit_payment_method.unable_to_find'), {}, {})
  else
    capture(money, event.authorization_code, gateway_options)
  end
end

#source_required? ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/spree/payment_method/store_credit.rb', line 112

def source_required?
  true
end

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



71
72
73
74
75
76
# File 'app/models/spree/payment_method/store_credit.rb', line 71

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