Method: Spree::StoreCredit#credit

Defined in:
app/models/spree/store_credit.rb

#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/spree/store_credit.rb', line 125

def credit(amount, authorization_code, order_currency, options = {})
  # Find the amount related to this authorization_code in order to add the store credit back
  capture_event = store_credit_events.find_by(action: CAPTURE_ACTION, authorization_code: authorization_code)

  if currency != order_currency # sanity check to make sure the order currency hasn't changed since the auth
    errors.add(:base, Spree.t('store_credit.currency_mismatch'))
    false
  elsif capture_event && amount <= capture_event.amount
    action_attributes = {
      action: CREDIT_ACTION,
      action_amount: amount,
      action_originator: options[:action_originator],
      action_authorization_code: authorization_code
    }
    create_credit_record(amount, action_attributes)
    true
  else
    errors.add(:base, Spree.t('store_credit.unable_to_credit', auth_code: authorization_code))
    false
  end
end