6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'app/services/spree/store_credits/apply.rb', line 6
def call(order:, amount: nil)
@order = order
return failed unless @order
remaining_total = [amount ? [amount, @order.outstanding_balance].min : @order.outstanding_balance, 0].max
return failure(nil, I18n.t('spree.store_credit_using_gift_card_error')) if @order.gift_card.present?
return failure(nil, I18n.t('spree.error_user_does_not_have_any_store_credits')) unless spendable_store_credits.exists?
ApplicationRecord.transaction do
existing = @order.payments.store_credits.where(status: :checkout)
if existing.any?
update_existing_payments(existing, remaining_total)
else
apply_store_credits(remaining_total)
end
end
if @order.reload.payments.store_credits.valid.any?
@order.update_hooks.each { |hook| @order.send(hook) } if @order.respond_to?(:update_hooks)
success(@order)
else
failure(@order, I18n.t('spree.store_credit_not_applied'))
end
end
|