Class: Killbill::Litle::LitleTransaction

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/litle/models/litle_transaction.rb

Class Method Summary collapse

Class Method Details

.find_candidate_transaction_for_refund(kb_payment_id, amount_in_cents) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/litle/models/litle_transaction.rb', line 21

def self.find_candidate_transaction_for_refund(kb_payment_id, amount_in_cents)
  # Find one successful charge which amount is at least the amount we are trying to refund
  litle_transactions = LitleTransaction.where("litle_transactions.amount_in_cents >= ?", amount_in_cents)
                                       .find_all_by_api_call_and_kb_payment_id(:charge, kb_payment_id)
  raise "Unable to find Litle transaction id for payment #{kb_payment_id}" if litle_transactions.size == 0

  # We have candidates, but we now need to make sure we didn't refund more than for the specified amount
  amount_refunded_in_cents = Killbill::Litle::LitleTransaction.where("api_call = ? and kb_payment_id = ?", :refund, kb_payment_id)
                                                              .sum("amount_in_cents")

  amount_left_to_refund_in_cents = -amount_refunded_in_cents
  litle_transactions.map { |transaction| amount_left_to_refund_in_cents += transaction.amount_in_cents }
  raise "Amount #{amount_in_cents} too large to refund for payment #{kb_payment_id}" if amount_left_to_refund_in_cents < amount_in_cents

  litle_transactions.first
end

.from_kb_payment_id(kb_payment_id) ⇒ Object



6
7
8
# File 'lib/litle/models/litle_transaction.rb', line 6

def self.from_kb_payment_id(kb_payment_id)
  single_transaction_from_kb_payment_id :charge, kb_payment_id
end

.refund_from_kb_payment_id(kb_payment_id) ⇒ Object



10
11
12
# File 'lib/litle/models/litle_transaction.rb', line 10

def self.refund_from_kb_payment_id(kb_payment_id)
  single_transaction_from_kb_payment_id :refund, kb_payment_id
end

.single_transaction_from_kb_payment_id(api_call, kb_payment_id) ⇒ Object



14
15
16
17
18
19
# File 'lib/litle/models/litle_transaction.rb', line 14

def self.single_transaction_from_kb_payment_id(api_call, kb_payment_id)
  litle_transactions = find_all_by_api_call_and_kb_payment_id(api_call, kb_payment_id)
  raise "Unable to find Litle transaction id for payment #{kb_payment_id}" if litle_transactions.empty?
  raise "Killbill payment mapping to multiple Litle transactions for payment #{kb_payment_id}" if litle_transactions.size > 1
  litle_transactions[0]
end