Class: CryptocoinPayable::PaymentProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptocoin_payable/commands/payment_processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.performObject



3
4
5
# File 'lib/cryptocoin_payable/commands/payment_processor.rb', line 3

def self.perform
  new.perform
end

.update_transactions_for(payment) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cryptocoin_payable/commands/payment_processor.rb', line 7

def self.update_transactions_for(payment)
  transactions = Adapters.for(payment.coin_type).fetch_transactions(payment.address)

  transactions.each do |tx|
    tx.symbolize_keys!

    transaction = payment.transactions.find_by_transaction_hash(tx[:txHash])
    if transaction
      transaction.update(confirmations: tx[:confirmations])
    else
      payment.transactions.create_from_tx_data!(tx, payment.coin_conversion)
      payment.update(
        coin_amount_due: payment.calculate_coin_amount_due,
        coin_conversion: CurrencyConversion.where(coin_type: payment.coin_type).last.price
      )
    end
  end
end

Instance Method Details

#performObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cryptocoin_payable/commands/payment_processor.rb', line 26

def perform
  CoinPayment.unconfirmed.find_each do |payment|
    # Check for completed payment first, in case it's 0 and we don't need to
    # make an API call.
    update_payment_state(payment)

    next if payment.confirmed?

    begin
      self.class.update_transactions_for(payment)
    rescue StandardError => error
      STDERR.puts 'PaymentProcessor: Unknown error encountered, skipping transaction'
      STDERR.puts error
      next
    end

    # Check for payments after the response comes back.
    update_payment_state(payment)

    # If the payment has not moved out of the pending state after loading
    # new transactions, we expire it.
    update_payment_expired_state(payment) if payment.pending?
  end
end