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



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

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

#calculate_coin_amount_dueObject



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

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

#coin_amount_paidObject



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

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

#coin_amount_paid_subunitObject



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

def coin_amount_paid_subunit
  transactions.sum(&:estimated_value)
end

#coin_conversionObject



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

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

#currency_amount_dueObject



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

def currency_amount_due
  price - currency_amount_paid
end

#currency_amount_paidObject



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

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)


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

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



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

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