Module: Workarea::Payment::Processing

Extended by:
ActiveSupport::Concern
Includes:
ApplicationDocument
Included in:
Capture, Refund
Defined in:
app/models/workarea/payment/processing.rb

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Instance Method Details

#complete!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/workarea/payment/processing.rb', line 50

def complete!
  return false unless valid?

  run_callbacks :complete do
    operation = Operation.new(transactions)
    operation.complete!
    operation.errors.each do |message|
      errors.add(:base, message)
    end

    self.result_transaction_ids = transactions.map(&:id)
    operation.success? && save(validate: false)
  end
end

#totalObject



65
66
67
# File 'app/models/workarea/payment/processing.rb', line 65

def total
  amounts.values.sum
end

#transactionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/workarea/payment/processing.rb', line 23

def transactions
  @transactions ||= amounts_with_tenders.reduce([]) do |memo, tuple|
    tender, amount = *tuple
    amount_left = amount

    find_reference_transactions_for(tender).each do |reference|
      next if amount_left.zero?

      amount_for_this_txn = if reference.amount < amount_left
                              reference.amount
                            else
                              amount_left
                            end

      amount_left -= amount_for_this_txn

      memo << tender.build_transaction(
        action: transaction_type,
        amount: amount_for_this_txn,
        reference: reference
      )
    end

    memo
  end
end