class CryptocoinPayable::CoinPayment

Public Instance Methods

adapter() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 104
def adapter
  @adapter ||= Adapters.for(coin_type)
end
calculate_coin_amount_due() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 65
def coin_amount_paid_subunit
  transactions.sum(&:estimated_value)
end
coin_conversion() click to toggle source
# 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() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 79
def currency_amount_due
  price - currency_amount_paid
end
currency_amount_paid() click to toggle source

@returns cents in fiat currency.

# 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?() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

notify_payable_confirmed() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 131
def notify_payable_confirmed
  notify_payable_event(:confirmed)
end
notify_payable_event(event_name) click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 120
def notify_payable_event(event_name)
  method_name = :"coin_payment_#{event_name}"
  payable.send(method_name, self) if payable.respond_to?(method_name)

  payable.coin_payment_event(self, event_name) if payable.respond_to?(:coin_payment_event)
end
notify_payable_expired() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 135
def notify_payable_expired
  notify_payable_event(:expired)
end
notify_payable_paid() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 127
def notify_payable_paid
  notify_payable_event(:paid)
end
populate_address() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 116
def populate_address
  update(address: adapter.create_address(id))
end
populate_currency_and_amount_due() click to toggle source
# File lib/cryptocoin_payable/coin_payment.rb, line 110
def populate_currency_and_amount_due
  self.currency ||= CryptocoinPayable.configuration.currency
  self.coin_amount_due = calculate_coin_amount_due
  self.coin_conversion = coin_conversion
end