When I started to try this out with a friend I was surprised how few resources there are. Especially in the CakePHP sector. Bitcoins are not very common yet – although quite promising: anonymous, not refundable payments without any transaction fees. A good info website is weusecoins.com, by the way.
You can run your own bitcoin daemon and use RPC to communicate with it. Setting up the bitcoind damon on a lenny was quite a mess. But with a more modern Debian squeeze it should be not that difficult. Using binaries will not work in all cases. So it might be necessary to compile it. Anyway – for a debian sqeeze (6) for example it is as easy as described here!
If it is set up and running you will have to create a user “bitcoind” which then has a wallet in his home directory.
Test it with “su bitcoind” to switch to this user and type
You should now see the current bitcoin settings as well as your total amount and other infos.
It is supposed to work with windows systems, as well. I didn’t try that, though.
The following article describes how to work with a shop system and bitcoins as payment. It requires a running bitcoin daemon.
I use github.com/mikegogulski/bitcoin-php as a vendor file as it provides basic access to the daemon.
Download and follow the instructions: github.com/dereuromark/CakePHP-Payment-Plugin
# in your configs.php $config['Bitcoin'] = array( 'account' => 'myaccountname', ); # in your private config (which should not be part of the svn/git commit) Configure::write('Bitcoin.username','myusername'); Configure::write('Bitcoin.password','mydaemonpassword');
The account name must match the account set as default in your daemon. Username and password are set in the /home/bitcoind/.bitcoind/bitcoin.conf:
... # You must set rpcuser and rpcpassword to secure the JSON-RPC api rpcuser=... rpcpassword=...
You can import the lib at any point and query it:
App::import('Lib', 'Payment.BitcoinLib'); $this->Bitcoin = new BitcoinLib(); $balance = $this->Bitcoin->getBalance(); # should be a numeric value if everything works
Check the test case for other methods and how to use them.
Checking received money
Probably the most important part:
$amount = $this->Bitcoin->getReceivedByAddress($address, 3);
It will tell you how much money you received. Since you always use a different address for a specific transaction you will know exactly who paid it and what for. The second param is the amount of confirmations. Set it to 3-6 just to be sure.
List all your transactions
$transactions = $this->Bitcoin->listTransactions();
You can then iterate over them and process the transactions manually.
Using the non-daemon (webservice) version
Might not be as safe and reliable – but it works. If you don’t set username or password it will automatically use the webservice if possible.
$amount = $this->Bitcoin->getReceivedByAddress('1PJ3Jy1T36BzxuikZDXY5YV7YjTmfcvQNc');
Note: Currently it always uses the webservice here as the daemon seems to return 0 in all cases.
The callback part of the model is not ready yet. Working on it. The basic idea: Running a cronjob every 5-10 minutes checking the pending transactions. It will then trigger the callback if new payments arrive. In the callback you can then decide what to do after the payment is received (and has the correct amount).