Class: CryptocoinPayable::CoinPayment

Inherits:
ActiveRecord::Base show all
Defined in:
lib/cryptocoin_payable/coin_payment.rb

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

has_coin_payments

Instance Method Details

#adapterObject



103
104
105
# File 'lib/cryptocoin_payable/coin_payment.rb', line 103

def adapter
  @adapter ||= Adapters.for(coin_type)
end

#calculate_coin_amount_dueObject



82
83
84
# File 'lib/cryptocoin_payable/coin_payment.rb', line 82

def calculate_coin_amount_due
  adapter.convert_main_to_subunit(currency_amount_due / coin_conversion.to_f).ceil
end

#coin_amount_paidObject



60
61
62
# File 'lib/cryptocoin_payable/coin_payment.rb', line 60

def coin_amount_paid
  transactions.sum { |tx| adapter.convert_subunit_to_main(tx.estimated_value) }
end

#coin_amount_paid_subunitObject



64
65
66
# File 'lib/cryptocoin_payable/coin_payment.rb', line 64

def coin_amount_paid_subunit
  transactions.sum(&:estimated_value)
end

#coin_conversionObject



86
87
88
# File 'lib/cryptocoin_payable/coin_payment.rb', line 86

def coin_conversion
  @coin_conversion ||= CurrencyConversion.where(coin_type: coin_type).last.price
end

#currency_amount_dueObject



78
79
80
# File 'lib/cryptocoin_payable/coin_payment.rb', line 78

def currency_amount_due
  price - currency_amount_paid
end

#currency_amount_paidObject



69
70
71
72
73
74
75
76
# File 'lib/cryptocoin_payable/coin_payment.rb', line 69

def currency_amount_paid
  cents = transactions.inject(0) do |sum, tx|
    sum + (adapter.convert_subunit_to_main(tx.estimated_value) * tx.coin_conversion)
  end

  # Round to 0 decimal places so there aren't any partial cents.
  cents.round(0)
end

#transactions_confirmed?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/cryptocoin_payable/coin_payment.rb', line 97

def transactions_confirmed?
  transactions.all? do |t|
    t.confirmations >= CryptocoinPayable.configuration.send(coin_type).confirmations
  end
end

#update_coin_amount_due(rate: coin_conversion) ⇒ Object



90
91
92
93
94
95
# File 'lib/cryptocoin_payable/coin_payment.rb', line 90

def update_coin_amount_due(rate: coin_conversion)
  update!(
    coin_amount_due: calculate_coin_amount_due,
    coin_conversion: rate
  )
end