Git Product home page Git Product logo

cryptocoin_payable's Introduction

Gem Version Build Status

Cryptocoin Payable

Forked from Bitcoin Payable

A rails gem that enables any model to have cryptocurrency payments. The polymorphic table coin_payments creates payments with unique addresses based on a BIP32 deterministic seed using https://github.com/GemHQ/money-tree and uses external APIs to check for payments:

Supported coins are:

  • Bitcoin
  • Bitcoin Cash
  • Ethereum

Payments have the following states:

  • pending
  • partial_payment
  • paid_in_full
  • comped (useful for refunding payments)
  • confirmed (enters state after n blockchain confirmations, see confirmations config option)
  • expired (useful for auto-expiring incomplete payments, see expire_payments_after config option)

No private keys needed, No bitcoind blockchain indexing on new servers, just address and payments.

Donations appreciated

  • 14xXZ6SFjwYZHATiywBE2durFknLePYqHS (Maros Hluska)

Installation

Add this line to your application's Gemfile:

gem 'cryptocoin_payable'

And then execute:

bundle
rails g cryptocoin_payable:install
bundle exec rake db:migrate
populate cryptocoin_payable.rb (see below)
bundle exec rake cryptocoin_payable:process_prices (see below)

Uninstall

rails d cryptocoin_payable:install

Run Tests

cucumber features
rspec
rubocop

Usage

Configuration

config/initializers/cryptocoin_payable.rb

CryptocoinPayable.configure do |config|
  # config.currency = :usd
  # config.testnet = true

  config.request_delay = 0.5
  config.expire_payments_after = 15.minutes

  config.configure_btc do |btc_config|
    # btc_config.confirmations = 3
    # btc_config.node_path = ''

    btc_config.master_public_key = 'tpub...'
  end

  config.configure_bch do |bch_config|
    # bch_config.confirmations = 3
    # btc_config.node_path = ''

    bch_config.master_public_key = 'tpub...'
  end

  config.configure_eth do |eth_config|
    # eth_config.confirmations = 12
    # eth_config.node_path = ''

    eth_config.master_public_key = 'tpub...'
  end
end

In order to use the bitcoin network and issue real addresses, CryptocoinPayable.config.testnet must be set to false:

CryptocoinPayable.config.testnet = false

Consider adding a request delay (in seconds) to prevent API rate limit errors:

CryptocoinPayable.config.request_delay = 0.5

Node Path

The derivation path for the node that will be creating your addresses.

Master Public Key

A BIP32 MPK in "Extended Key" format used when configuring bitcoin payments (see btc_config.master_public_key above).

Public net starts with: xpub Testnet starts with: tpub

Adding it to your model

class Product < ActiveRecord::Base
  has_coin_payments
end

Creating a payment from your application

def create_payment(amount_in_cents)
  self.coin_payments.create!(reason: 'sale', price: amount_in_cents, coin_type: :btc)
end

Update payments with the current price of BTC based on your currency

CryptocoinPayable also supports local currency conversions and BTC exchange rates.

The process_prices rake task connects to api.coinbase.com to get the latest BTC price for your specified currency. As a fallback, it will connect to api.gemini.com to get the latest price. It then updates all payments that havent received an update in the last 30 minutes with the new value owing in BTC. This honors the price of a payment for 30 minutes at a time.

rake cryptocoin_payable:process_prices

Processing payments

All payments are calculated against the dollar amount of the payment. So a bitcoin_payment for $49.99 will have it's value calculated in BTC. It will stay at that price for 30 minutes. When a payment is made, a transaction is created that stores the BTC in satoshis paid and the exchange rate is was paid at. This is very valuable for accounting later. (capital gains of all payments received)

If a partial payment is made, the BTC value is recalculated for the remaining dollar amount with the latest exchange rate. This means that if someone pays 0.01 for a 0.5 payment, that 0.01 is converted into dollars at the time of processing and the remaining amount is calculated in dollars and the remaining amount in BTC is issued. (If BTC bombs, that value could be greater than 0.5 now)

This prevents people from gaming the payments by paying very little BTC in hopes the price will rise. Payments are not recalculated based on the current value of BTC, but in dollars.

To run the payment processor:

rake cryptocoin_payable:process_payments

Notify your application when a payment is made

Use the coin_payment_paid and coin_payment_confirmed methods

def Product < ActiveRecord::Base
  has_coin_payments

  def create_payment(amount_in_cents)
    self.coin_payments.create!(reason: 'sale', price: amount_in_cents, type: :btc)
  end

  # Runs when the payment is first detected on the network.
  def coin_payment_paid(payment)
    self.notify!
  end

  # Runs when enough confirmations have occurred.
  def coin_payment_confirmed(payment)
    self.ship!
  end
end

Delete old CurrencyConversion data

Every time the payment processor is run, several rows are inserted into the database to record the value of the coin at a given instance in time. Over time, your application will accumulate historical currency conversion data and you may want to clear it out:

rake cryptocoin_payable:delete_currency_conversions

By default, it will delete any data older than 1 month. You can configure this using an env variable:

DELETE_BEFORE=2017-12-15 rake cryptocoin_payable:delete_currency_conversions

Comp a payment

This will bypass the payment, set the state to comped and call back to your app that the payment has been processed.

@coin_payment.comp

Expire a payment

@coin_payment.expire

Payments will auto-expire if you set the expire_payments_after option. The exact timing is not precise because payment expiry is evaluated whenever payment_processor runs.

View all the transactions in the payment

coin_payment = @product.coin_payments.first
coin_payment.transactions.find_each do |transaction|
  puts transaction.attributes
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

  • andersonlewin
  • krtschmr
  • mhluska

cryptocoin_payable's People

Contributors

andersonlewin avatar jmarbach avatar sailias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cryptocoin_payable's Issues

Trying Bitcoin Cash

i try to create pay on BCH.... and i get this:

ArgumentError ('bch' is not a valid coin_type):

bip32 for ethereum

Hi,
I can't understand how to generate master_public_key for ethereum. Anyone one has a little piece of code that could help me ?

thanks

Remittance

@Sailias my app has a need to pay out users periodically (as well as collect payments on their behalf which this gem already covers). Would it make sense to add this feature to cryptocoin_payable? I'm envisioning this as just an extra method to each coin adapter that will interact with the correct API (e.g. Blockcypher for btc). And then wrapped in a Rake task.

NameError: uninitialized constant Net::HTTP

hi
thanks for this gem.

I have an issue when I run

`rake cryptocoin_payable:process_prices

i get

rake aborted!
NameError: uninitialized constant Net::HTTP
gems/cryptocoin_payable-1.4.5/lib/cryptocoin_payable/adapters/base.rb:92:in `get_request'

ruby 2.5.3
rails 5.2.2

also I don't understand how to generate the Master Public Key. Maybe you have a bit more documentation ?

thanks

Generated address didin't receive payment

Thanks for the gem!
I added the xpub public key and transfered to the coinpayment address that was created, i can see the transaction has been confirmed but it hasn't appear on my wallet. Maybe i'm missing something?? this was on the bitcoin mainnet.

cryptocoin_payable.rb

CryptocoinPayable.configure do |config|
    config.currency = :usd
    config.testnet = false
  
    config.request_delay = 0.5
    config.expire_payments_after = 15.minutes
  
    config.configure_btc do |btc_config|
      # btc_config.confirmations = 3
      # btc_config.node_path = ''
  
      btc_config.master_public_key = 'xpub_address'
    end
  
    config.configure_bch do |bch_config|
      # bch_config.confirmations = 3
      # btc_config.node_path = ''
  
      bch_config.master_public_key = 'tpub...'
    end
  
    config.configure_eth do |eth_config|
      # eth_config.confirmations = 12
      # eth_config.node_path = ''
  
      eth_config.master_public_key = 'tpub...'
    end
  end

cryptocoin_payable:process_payments with BCH

i try the command:

rake cryptocoin_payable:process_payments

and return >

PaymentProcessor: Unknown error encountered, skipping transaction
CryptocoinPayable::NetworkNotSupported

i see the code on > lib/cryptocoin_payable/adapters/bitcoin_cash.rb

at line:

url = "https://#{prefix}blockexplorer.com/api/txs/?address=#{legacy_address(address)}"

the prefix bchtest. not work anymore

How to add DOGE support

I'm considering adding support for DOGE in this library. Would someone be able to give me some pointers?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.