Class: CryptocoinPayable::CoinPayment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CryptocoinPayable::CoinPayment
- Defined in:
- lib/cryptocoin_payable/coin_payment.rb
Instance Method Summary collapse
- #adapter ⇒ Object
- #calculate_coin_amount_due ⇒ Object
- #coin_amount_paid ⇒ Object
- #coin_amount_paid_subunit ⇒ Object
- #coin_conversion ⇒ Object
- #currency_amount_due ⇒ Object
- #currency_amount_paid ⇒ Object
- #transactions_confirmed? ⇒ Boolean
- #update_coin_amount_due(rate: coin_conversion) ⇒ Object
Methods inherited from ActiveRecord::Base
Instance Method Details
#adapter ⇒ Object
104 105 106 |
# File 'lib/cryptocoin_payable/coin_payment.rb', line 104 def adapter @adapter ||= Adapters.for(coin_type) end |
#calculate_coin_amount_due ⇒ Object
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_paid ⇒ Object
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_subunit ⇒ Object
65 66 67 |
# File 'lib/cryptocoin_payable/coin_payment.rb', line 65 def coin_amount_paid_subunit transactions.sum(&:estimated_value) end |
#coin_conversion ⇒ Object
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_due ⇒ Object
79 80 81 |
# File 'lib/cryptocoin_payable/coin_payment.rb', line 79 def currency_amount_due price - currency_amount_paid end |
#currency_amount_paid ⇒ Object
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
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 |