Sha256: da15213140e018c997b978784fb1e3f390f3dc29660f16c194f2dbf8579dec20

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

class Kaui::InvoicePayment < KillBillClient::Model::InvoicePayment

  include Kaui::PaymentState

  SAMPLE_REASON_CODES = ['600 - Alt payment method',
                         '699 - OTHER']

  class << self

    def build_from_raw_payment(raw_payment)

      return nil if raw_payment.nil?

      result = Kaui::InvoicePayment.new
      KillBillClient::Model::InvoicePaymentAttributes.instance_variable_get('@json_attributes').each do |attr|
        result.send("#{attr}=", raw_payment.send(attr))
      end
      # Use  Kaui::Transaction to benefit from additional fields (e.g next_retry_date)
      original_transactions = (result.transactions || [])
      result.transactions = []
      original_transactions.each do |transaction|
        new_transaction = Kaui::Transaction.new
        KillBillClient::Model::PaymentTransactionAttributes.instance_variable_get('@json_attributes').each do |attr|
          new_transaction.send("#{attr}=", transaction.send(attr))
        end
        result.transactions << new_transaction
      end
      result.build_transactions_next_retry_date!
      result
    end

  end

  [:auth, :captured, :purchased, :refunded, :credited].each do |type|
    define_method "#{type}_amount_to_money" do
      Kaui::Base.to_money(send("#{type}_amount"), currency)
    end


    # For each payment transaction, compute next_retry date by joining with payment attempts
    def build_transactions_next_retry_date!
      (transactions || []).each do |transaction|
        # Filter attempts matching that transaction and SCHEDULED for retry
        transaction.next_retry_date = (payment_attempts || []).select do |attempt|
          ((attempt.transaction_id && attempt.transaction_id == transaction.transaction_id) ||
              (attempt.transaction_external_key && attempt.transaction_external_key == transaction.transaction_external_key)) &&
              attempt.state_name == 'SCHEDULED'
        end.map do |attempt|
          attempt.effective_date
        end.first
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kaui-0.15.0 app/models/kaui/invoice_payment.rb