All Projects → Sailias → Bitcoin_payable

Sailias / Bitcoin_payable

Licence: mit
A rails bitcoin payment processing gem

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Bitcoin payable

Awesome Privacy On Blockchains
A curated list of privacy on blockchains resources
Stars: ✭ 86 (-22.52%)
Mutual labels:  blockchain, bitcoin
Coinpare
Compare cryptocurrency trading data across multiple exchanges and blockchains in the comfort of your terminal
Stars: ✭ 89 (-19.82%)
Mutual labels:  blockchain, bitcoin
Blockchaintechnology
Blockchain Frontier Technology Tracking
Stars: ✭ 88 (-20.72%)
Mutual labels:  blockchain, bitcoin
Copernicus
An alternative implementation of the Bitcoin Cash protocol, written in Golang
Stars: ✭ 83 (-25.23%)
Mutual labels:  blockchain, bitcoin
My First Bitcoin Miner
For the curious minds who want to understand how Bitcoin Blockchain works!
Stars: ✭ 110 (-0.9%)
Mutual labels:  blockchain, bitcoin
Blockchain Parser
The simpliest script for parsing Bitcoin blockchain. It made convertion of blk*****.dat files to the simple text.
Stars: ✭ 84 (-24.32%)
Mutual labels:  blockchain, bitcoin
Dotcoin
A simple and integrity blockchain implementation in Golang
Stars: ✭ 89 (-19.82%)
Mutual labels:  blockchain, bitcoin
Blockked
Blockked - blockchain.info companion (and educational) app for Android
Stars: ✭ 69 (-37.84%)
Mutual labels:  blockchain, bitcoin
Awesome Cryptocurrency Security
😎 Curated list about cryptocurrency security (reverse / exploit / fuzz..)
Stars: ✭ 102 (-8.11%)
Mutual labels:  blockchain, bitcoin
Masterblockchain
Stars: ✭ 100 (-9.91%)
Mutual labels:  blockchain, bitcoin
Whale
🐋 Show Ethereum and Bitcoin price in command line interface (CLI).
Stars: ✭ 81 (-27.03%)
Mutual labels:  blockchain, bitcoin
Diadata
DIAdata.org platform
Stars: ✭ 103 (-7.21%)
Mutual labels:  blockchain, bitcoin
Blockchain golang
Blockchain demo based on golang 基于golang编写的区块链公链demo
Stars: ✭ 80 (-27.93%)
Mutual labels:  blockchain, bitcoin
Elementsproject.org
Source code for the ElementsProject.org website
Stars: ✭ 84 (-24.32%)
Mutual labels:  blockchain, bitcoin
Lightning Rfc
Lightning Network Specifications
Stars: ✭ 1,224 (+1002.7%)
Mutual labels:  blockchain, bitcoin
Bitnfc
Bitcoin NFC Android Mobile Wallet - JS + Ionic + Cordova + Bitcore + Blockchain.info API + Cordova NFC plugin
Stars: ✭ 88 (-20.72%)
Mutual labels:  blockchain, bitcoin
Blocksci
A high-performance tool for blockchain science and exploration
Stars: ✭ 1,127 (+915.32%)
Mutual labels:  blockchain, bitcoin
Fast Dat Parser
Superfast blockchain parser for stats
Stars: ✭ 68 (-38.74%)
Mutual labels:  blockchain, bitcoin
Bitcoin in a nutshell
Книга о том, как действительно работает Bitcoin
Stars: ✭ 98 (-11.71%)
Mutual labels:  blockchain, bitcoin
Solana
Web-Scale Blockchain for fast, secure, scalable, decentralized apps and marketplaces.
Stars: ✭ 1,383 (+1145.95%)
Mutual labels:  blockchain, bitcoin

BitcoinPayable

A rails gem that enables any model to have bitcoin payments. The polymorphic table bitcoin_payments creates payments with unique addresses based on a BIP32 deterministic seed using https://github.com/wink/money-tree and uses the (https://helloblock.io OR https://blockchain.info/) API to check for payments.

Payments have 5 states: pending, partial_payment, paid_in_full, confirmed, comped

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

Rails 5.1

Master now supports multiple Rails versions.

No longer maintained Support for Rails 5.1

Other crypto currencies

Cryptocoin Payable

Installation

Add this line to your application's Gemfile: (I might be too lazy to update RubyGems all the time)

gem 'bitcoin_payable', git: 'https://github.com/Sailias/bitcoin_payable', branch: 'master'

And then execute:

$ bundle

$ rails g bitcoin_payable:install --skip

$ bundle exec rake db:migrate

Or install it yourself as:

$ gem install bitcoin_payable

Usage

Configuration

config/initializers/bitcoin_payable.rb

BitcoinPayable.config do |config|

  config.currency = :usd    # Default currency

  config.node_path = "m/0/"
  config.master_public_key = "your xpub master public key here"

  config.testnet = true
  config.adapter = 'blocktrail' # Use blocktrail, blockchain_info or blockcypher

  # Confirmations (defaults to 6)
  config.confirmations = 6

  # The rate for Bitcoin you'll be using to calculate prices
  # Optional setting. Default to :daily_average
  # :last               The last market's price
  # :high               Today's highest price
  # :low                Today's lowest price
  # :daily_average      The daily average price
  # :weekly_average     The weekly average price
  # :monthly_average    The monthly average price
  config.rate_calculation = :daily_average

  # Webhooks
  # Only available for blocktrail adapter
  config.allowwebhooks = true
  config.webhook_domain = "subdomain.domain.com:port"
end
  • In order to use the bitcoin network and issue real addresses, BitcoinPayable.config.testnet must be set to false *

    BitcoinPayable.config.testnet = false

Blocktrail Adapter

If you use config.adapter = 'blocktrail' * The only one supporting webhooks* you'll need to set the following environment variables:

# Basic authentification for your webhooks
ENV['BITCOIN_PAYABLE_WEBHOOK_USER']= "username"
ENV['BITCOIN_PAYABLE_WEBHOOK_PASS']= "password"

# API keys provided by Blocktrail.com
ENV['BLOCKTRAIL_API_KEY']= "key"
ENV['BLOCKTRAIL_API_SECRET']= "secret"

You can obtain your API keys at https://www.blocktrail.com/dev/login

Node Path

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

Master Public Key

A BIP32 MPK in "Extended Key" format.

Public net starts with: xpub Testnet starts with: tpub

Adding it to your model

class Product < ActiveRecord::Base
  has_bitcoin_payments
end

Creating a payment from your application

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

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

BitcoinPayable also supports local currency conversions and BTC exchange rates.

The process_prices rake task connects to api.bitcoinaverage.com to get the 24 hour weighted average of BTC for your specified currency. 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 bitcoin_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 bitcoin_payable:process_payments

Do no run the payment processor if you have webhooks set up as new transactions will be processed automatically by the webhook.

Notify your application when a payment is made

Use the bitcoin_payment_paid method

def Product < ActiveRecord::Base
  has_bitcoin_payments

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

  def bitcoin_payment_paid
    self.ship!
  end
end

Use the bitcoin_payment_status_changed method

def bitcoin_payment_status_changed(from_state, to_state)
  if to_state == "confirmed"
    self.ship!
  elsif to_state == "paid_in_full"
    self.notify_payment_received(self)
  end
end

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.

@bitcoin_payment.comp

View all the transactions in the payment

bitcoin_payment = @product.bitcoin_payments.first
bitcoin_payment.transactions.each do |transaction|
  puts transaction.block_hash
  puts transaction.block_time

  puts transaction.transaction_hash

  puts transaction.estimated_value
  puts transaction.estimated_time

  puts transaction.btc_conversion
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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].