Class: Spree::StoreCredit
Constant Summary
collapse
- VOID_ACTION =
'void'.freeze
- CANCEL_ACTION =
'cancel'.freeze
- CREDIT_ACTION =
'credit'.freeze
- CAPTURE_ACTION =
'capture'.freeze
- ELIGIBLE_ACTION =
'eligible'.freeze
- AUTHORIZE_ACTION =
'authorize'.freeze
- ALLOCATION_ACTION =
'allocation'.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#actions ⇒ Object
-
#amount_remaining ⇒ Object
-
#authorize(amount, order_currency, options = {}) ⇒ Object
-
#can_be_deleted? ⇒ Boolean
-
#can_capture?(payment) ⇒ Boolean
-
#can_credit?(payment) ⇒ Boolean
-
#can_void?(payment) ⇒ Boolean
-
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
-
#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object
-
#editable? ⇒ Boolean
-
#generate_authorization_code ⇒ Object
-
#outstanding? ⇒ Boolean
Whether the store still owes anything on this credit.
-
#validate_authorization(amount, order_currency) ⇒ Object
-
#void(authorization_code, options = {}) ⇒ Object
money_methods
#user, #user=
Methods included from Metadata
#private_metadata, #private_metadata=
Methods included from ActedBy
#acted_by_prefixed_id, #acted_by_type, assume_admin_actor_type, columns_for, models, registered_model_names
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
105
106
107
|
# File 'app/models/spree/store_credit.rb', line 105
def action
@action
end
|
#action_amount ⇒ Object
Returns the value of attribute action_amount.
105
106
107
|
# File 'app/models/spree/store_credit.rb', line 105
def action_amount
@action_amount
end
|
#action_authorization_code ⇒ Object
Returns the value of attribute action_authorization_code.
105
106
107
|
# File 'app/models/spree/store_credit.rb', line 105
def action_authorization_code
@action_authorization_code
end
|
#action_originator ⇒ Object
Returns the value of attribute action_originator.
105
106
107
|
# File 'app/models/spree/store_credit.rb', line 105
def action_originator
@action_originator
end
|
Class Method Details
.ransackable_scopes_skip_sanitize_args ⇒ Object
Two-state scopes: see Spree::Base.ransack_flag? for why the cast is
opted out of here and done inside each scope instead.
120
121
122
|
# File 'app/models/spree/store_credit.rb', line 120
def self.ransackable_scopes_skip_sanitize_args
super | %w[outstanding from_gift_card]
end
|
Instance Method Details
#amount_remaining ⇒ Object
124
125
126
|
# File 'app/models/spree/store_credit.rb', line 124
def amount_remaining
amount - amount_used - amount_authorized
end
|
#authorize(amount, order_currency, options = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'app/models/spree/store_credit.rb', line 128
def authorize(amount, order_currency, options = {})
authorization_code = options[:action_authorization_code]
if authorization_code
if store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code)
return true
end
else
authorization_code = generate_authorization_code
end
if validate_authorization(amount, order_currency)
update!(
action: AUTHORIZE_ACTION,
action_amount: amount,
action_originator: options[:action_originator],
action_authorization_code: authorization_code,
amount_authorized: amount_authorized + amount
)
authorization_code
else
errors.add(:base, :insufficient_authorized_amount, message: I18n.t('spree.store_credit_payment_method.insufficient_authorized_amount'))
false
end
end
|
#can_be_deleted? ⇒ Boolean
256
257
258
|
# File 'app/models/spree/store_credit.rb', line 256
def can_be_deleted?
amount_used.zero? && amount_authorized.zero?
end
|
#can_capture?(payment) ⇒ Boolean
230
231
232
|
# File 'app/models/spree/store_credit.rb', line 230
def can_capture?(payment)
payment.pending? || payment.checkout?
end
|
#can_credit?(payment) ⇒ Boolean
238
239
240
|
# File 'app/models/spree/store_credit.rb', line 238
def can_credit?(payment)
payment.completed? && payment.credit_allowed > 0
end
|
#can_void?(payment) ⇒ Boolean
234
235
236
|
# File 'app/models/spree/store_credit.rb', line 234
def can_void?(payment)
payment.pending? || (payment.checkout? && !payment.order.completed?)
end
|
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'app/models/spree/store_credit.rb', line 162
def capture(amount, authorization_code, order_currency, options = {})
return false unless authorize(amount, order_currency, action_authorization_code: authorization_code)
if amount <= amount_authorized
if currency != order_currency
errors.add(:base, :currency_mismatch, message: I18n.t('spree.store_credit_payment_method.currency_mismatch'))
false
else
update!(
action: CAPTURE_ACTION,
action_amount: amount,
action_originator: options[:action_originator],
action_authorization_code: authorization_code,
amount_used: amount_used + amount,
amount_authorized: amount_authorized - amount
)
authorization_code
end
else
errors.add(:base, :insufficient_authorized_amount, message: I18n.t('spree.store_credit_payment_method.insufficient_authorized_amount'))
false
end
end
|
#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'app/models/spree/store_credit.rb', line 203
def credit(amount, authorization_code, order_currency, options = {})
capture_event = store_credit_events.find_by(action: CAPTURE_ACTION, authorization_code: authorization_code)
if currency != order_currency errors.add(:base, :currency_mismatch, message: I18n.t('spree.store_credit_payment_method.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, :unable_to_credit, auth_code: authorization_code,
message: I18n.t('spree.store_credit_payment_method.unable_to_credit', auth_code: authorization_code))
false
end
end
|
#editable? ⇒ Boolean
252
253
254
|
# File 'app/models/spree/store_credit.rb', line 252
def editable?
amount_used.zero? && amount_authorized.zero?
end
|
#generate_authorization_code ⇒ Object
260
261
262
|
# File 'app/models/spree/store_credit.rb', line 260
def generate_authorization_code
"#{id}-SC-#{Time.now.utc.strftime('%Y%m%d%H%M%S%6N')}"
end
|
#outstanding? ⇒ Boolean
Whether the store still owes anything on this credit. The record-level
twin of the unspent scope, and the two must agree: the list filters on
the scope and the row renders this, so a client deriving one from the
money columns itself would drift from the filter that hides it.
248
249
250
|
# File 'app/models/spree/store_credit.rb', line 248
def outstanding?
amount_used + amount_authorized < amount
end
|
#validate_authorization(amount, order_currency) ⇒ Object
153
154
155
156
157
158
159
160
|
# File 'app/models/spree/store_credit.rb', line 153
def validate_authorization(amount, order_currency)
if BigDecimal(amount_remaining, 3) < BigDecimal(amount, 3)
errors.add(:base, :insufficient_funds, message: I18n.t('spree.store_credit_payment_method.insufficient_funds'))
elsif currency != order_currency
errors.add(:base, :currency_mismatch, message: I18n.t('spree.store_credit_payment_method.currency_mismatch'))
end
errors.blank?
end
|
#void(authorization_code, options = {}) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'app/models/spree/store_credit.rb', line 186
def void(authorization_code, options = {})
if auth_event = store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code)
update!(
action: VOID_ACTION,
action_amount: auth_event.amount,
action_authorization_code: authorization_code,
action_originator: options[:action_originator],
amount_authorized: amount_authorized - auth_event.amount
)
true
else
errors.add(:base, :unable_to_void, auth_code: authorization_code,
message: I18n.t('spree.store_credit_payment_method.unable_to_void', auth_code: authorization_code))
false
end
end
|