Class: BitcoinPayable::PaymentProcessor

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePaymentProcessor

Returns a new instance of PaymentProcessor.



7
8
# File 'lib/bitcoin_payable/commands/payment_processor.rb', line 7

def initialize
end

Class Method Details

.performObject



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

def self.perform
  new.perform
end

Instance Method Details

#performObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bitcoin_payable/commands/payment_processor.rb', line 10

def perform
  BitcoinPayable::BitcoinPayment.where(state: [:pending, :partial_payment]).each do |payment|
    transactions = HelloBlock::Transaction.where(addresses: [payment.address]).to_hash
    if transactions["data"]
      transactions["data"]["transactions"].each do |tx|
        unless payment.transactions.find_by_transaction_hash(tx["txHash"])
          payment.transactions.create!(
            estimated_value: tx["estimatedTxValue"],
            transaction_hash: tx["txHash"],
            block_hash: tx["blockHash"],
            block_time: (Time.at(tx["blockTime"]) if tx["blockTime"]),
            estimated_time: (Time.at(tx["estimatedTxTime"]) if tx["estimatedTxTime"]),
            btc_conversion: BitcoinPayable::CurrencyConversion.last.btc
          )

          payment.update(btc_amount_due: payment.calculate_btc_amount_due)
        end
      end
    end

    if payment.currency_amount_paid >= payment.price
      payment.paid
    elsif payment.currency_amount_paid > 0
       payment.partially_paid
    end
  end
end