Sha256: b9242f0add4a634a03b5ff96a6d5868ffa7902b06dada0dd9957f65c79192c57

Contents?: true

Size: 958 Bytes

Versions: 9

Compression:

Stored size: 958 Bytes

Contents

class RefundPayments
  attr_accessor :order

  include RefundMethods

  def initialize(order)
    @order = order
  end

  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

  private

  def sorted_eligible_refund_payments(payments)
    payments = payments.select { |p| eligible_refund_methods.include? p.payment_method.class }
    payments.sort_by { |p| eligible_refund_methods.index(p.payment_method.class) }
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
solidus-returnly-0.12.0 app/services/refund_payments.rb
solidus-returnly-0.11.0 app/services/refund_payments.rb
solidus-returnly-0.10.0 app/services/refund_payments.rb
solidus-returnly-0.9.0 app/services/refund_payments.rb
solidus-returnly-0.8.1 app/services/refund_payments.rb
solidus-returnly-0.8.0 app/services/refund_payments.rb
solidus-returnly-0.7.1 app/services/refund_payments.rb
solidus-returnly-0.7.0 app/services/refund_payments.rb
solidus-returnly-0.6.2 app/services/refund_payments.rb