6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'app/services/spree/checkout/add_store_credit.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
return failure(nil, Spree.t(:error_user_does_not_have_any_store_credits)) unless @order.user&.store_credits&.any?
ApplicationRecord.transaction do
@order.payments.store_credits.where(state: :checkout).map(&:invalidate!)
apply_store_credits(remaining_total)
end
if @order.reload.payments.store_credits.valid.any?
@order.updater.run_hooks
success(@order)
else
failure(@order)
end
end
|