Class: Kaui::InvoicePayment

Inherits:
KillBillClient::Model::InvoicePayment
  • Object
show all
Includes:
PaymentState
Defined in:
app/models/kaui/invoice_payment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PaymentState

#amount_capturable, #amount_refundable, #capturable?, #chargebackable?, #is_fully_refunded?, #paid_amount_to_money, #refundable?, #returned_amount_to_money, #total_authed_amount_to_money, #voidable?

Class Method Details

.build_from_raw_payment(raw_payment) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/kaui/invoice_payment.rb', line 15

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

.find_safely_by_id(id, options = {}) ⇒ Object


7
8
9
10
11
12
13
# File 'app/models/kaui/invoice_payment.rb', line 7

def find_safely_by_id(id, options = {})
  Kaui::InvoicePayment.find_by_id(id, true, true, options)
rescue => e
  # Maybe the plugin is not registered or the plugin threw an exception
  Rails.logger.warn(e)
  Kaui::InvoicePayment.find_by_id(id, false, true, options)
end

Instance Method Details

#build_transactions_next_retry_date!Object

For each payment transaction, compute next_retry date by joining with payment attempts


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/kaui/invoice_payment.rb', line 46

def build_transactions_next_retry_date!

  # Filter scheduled attempts: We could have several in parallel when multiple independent transactions occur at the same time
  # (They would have different transaction_external_key)
  scheduled_attempts = (payment_attempts || []).select do |a|
    a.state_name == 'SCHEDULED'
  end

  # Look for latest transaction associated with each such scheduled attempt and set retry date accordingly
  scheduled_attempts.each do |a|

    last_transaction_for_attempt = (transactions || []).select do |t|
      t.transaction_external_key == a.transaction_external_key
    end.sort_by do |t|
      t.effective_date
    end.last

    last_transaction_for_attempt.next_retry_date = a.effective_date if last_transaction_for_attempt
  end

end