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) ⇒ RefundPayments

Returns a new instance of RefundPayments.



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

def initialize(order)
  @order = order
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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/refund_payments.rb', line 10

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 << {
      code:                    payment.source_type.constantize.model_name.human,
      amount:                  allowed_amount.to_f,
      original_transaction_id: ''
    }
  end
end