Accept bitcoins using python

By : Javed Khan

In this blog post we'll talk about bitcoins and look at an overview of how to accept them from your python app.

Bitcoin:

Bitcoin is crypto-currency whose protocol allows fast, anonymous and secure digital transactions.

To know more about bitcoin and to install it, go to the "Developer Guide" guide.

Getting started:

To get started, install bitcoin (assuming Ubuntu):

$ sudo apt-get install bitcoind

Now edit ~/.bitcoin/bitcoin.conf and add:

testnet=1

This will allow us to run bitcoin in test mode.

Now start the bitcoind daemon using:

$ bitcoind -daemon

To check that it's working, do:

$ bitcoind getinfo

If you see a JSON output, you should be good to go.

Using JSON RPC:

Bitcoin includes a JSON RPC server as a part of the daemon.

Using a client library like "bitcoin-python", we can invoke bitcoin calls from python like so:

>>> import bitcoinrpc
>>> bitcoin = bitcoinrpc.connect_to_local()
>>> bitcoin.getinfo()

Payment Flow:

In a standard payment flow, the steps involved would be:

  • User checks out a shopping cart. Transaction starts.
  • Generate a new bitcoin addres and ask the user to pay.
  • Verify that payment is made, wait for confirmations if required.
  • Generate invoice. Transaction ends.

Let's see how to do each of this step using the python client:

Generate a new bitcoin address:

>>> add = bitcoin.getnewaddress()

Verify payment is made:

>>> bitcoin.getreceivedbyaddress(add)

Get transcation info:

>>> bitcoin.listtransactions(address=add)

Note: By default the RPC client waits for 1 confirmation, you can increase this using the minconf argument to most API calls.

You can use a testnet faucet for testing:

http://tpfaucet.appspot.com/

http://testnet.mojocoin.com/

When you're ready, you can switch the testnet mode in your bitcoinf.conf.

Security:

You need to take precautions to make sure that your bitcoin wallet is secured to prevent wallet theft, this is out of the scope of this tutorial, so please refer to:

https://en.bitcoin.it/wiki/Securing_your_wallet

References:

https://en.bitcoin.it/wiki/Bitcoin-python

https://en.bitcoin.it/wiki/Merchant_Howto


Related Posts


Can we help you build amazing apps? Contact us today.

Topics : python bitcoin

Comments

Anon 16th July, 2013

Thanks! Keep posting about virtual currency development please. It's still early days and we need a lot of documentation and tutorials for the developer community :)

commmenttor
praveen kumar reddy 7th Aug., 2013

hi madam,

THis is praveen, very nice information, i am also right now completed my python training, any openings call me.i am expert in django and python.7795044070

commmenttor
bitcoin store

Nice that is very interesting I will be sure to take that into contemplation next time. Where can I find more about this?

commmenttor
© Agiliq, 2009-2012