Sha256: e0d9f30731bc34de6eefcf68a8a56604bb973e0796a5406892dfd79db50b333a

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Dune::Balanced
  class Refund
    FIXED_OPERATIONAL_FEE = 0.3

    attr_reader :paid_resource

    def initialize(paid_resource)
      @paid_resource = paid_resource
    end

    def complete!(reason, amount = paid_resource.value)
      unless amount.zero?
        refund_amount = ((amount + refundable_fees(amount)) * 100).round
        debit.refund(
          amount:      refund_amount,
          description: I18n.t('dune.balanced.refund.description',
            resource_id:   paid_resource.id,
            resource_name: paid_resource.class.model_name.human
          ),
          meta: {
            'reason' => I18n.t("dune.balanced.refund_reasons.#{reason}")
          }
        )
      end
      paid_resource.refund!
    end

    def debit
      @debit ||= ::Balanced::Debit.find("/debits/#{paid_resource.payment_id}")
    end

    def refundable_fees(refund_amount)
      percentual_fee = if paid_resource.payment_service_fee_paid_by_user
        refund_amount / paid_resource.value * paid_resource.payment_service_fee
      else
        0
      end

      (percentual_fee - FIXED_OPERATIONAL_FEE).round(2)
    end

    private

    def resource_amount
      to_be_refunded = if paid_resource.payment_service_fee_paid_by_user
        paid_resource.value + paid_resource.payment_service_fee
      else
        paid_resource.value
      end
      (to_be_refunded * 100 - FIXED_OPERATIONAL_FEE).round
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dune-balanced-1.0.0 app/models/dune/balanced/refund.rb