Class: RefundPayments

Inherits:
Object
  • Object
show all
Includes:
RefundMethods
Defined in:
app/services/refund_payments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, gift_card) ⇒ RefundPayments

Returns a new instance of RefundPayments.



6
7
8
9
# File 'app/services/refund_payments.rb', line 6

def initialize(order, gift_card)
  @order = order
  @gift_card = gift_card
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



2
3
4
# File 'app/services/refund_payments.rb', line 2

def order
  @order
end

Instance Method Details

#perform!(payment_amount) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/refund_payments.rb', line 11

def perform!(payment_amount)
  sorted_payments = sorted_eligible_refund_payments(order.payments.completed)
  sorted_payments.each_with_object([]) do |payment, payments|
    break payments if payment_amount <= 0
    next payments unless payment.can_credit?

    allowed_amount = [payment_amount, payment.credit_allowed].min
    payment_amount -= allowed_amount

    payments << {
      id: payment.id.to_s,
      amount: allowed_amount.to_s,
      status: 'PENDING',
      type: 'REFUND',
      gateway: payment.payment_method.type.demodulize,
      is_online: true,
      is_test: !Rails.env.match?(/production/),
      payment_details: transaction_payment_details(payment),
      created_at: Time.zone.now.iso8601(3)
    }
  end
end