简体   繁体   中英

How do I use Blockchain.info's Receive Payments API with node JS / Express?

They have great documentation for PHP, but nothing really for node!

Does anyone here have any projects,where they used the blockchain.info Receive Payments API, in their node apps, to receive money / check for confirmations?

I learn a lot better by viewing / recreating example code so any you have would help me out tons!

-Thanks for your time

To create the address you can use something like this:

app.js:

api = require('blockchain')
app.post('/api/blockchain/createAddress', api.blockchainCreateAddress);

blockchain.js:

exports.blockchainCreateAddress = function(req, res) {

    var btc_address = '<you-destination-btc-address>';
    var api_url = 'https://blockchain.info/api/receive';
    var callback_url = '<your-callback-url>';

    var url = api_url + '?method=create&address=' + btc_address + '&callback=' + encodeURIComponent(callback_url);

    if (btc_address)
    {
        https.get(url, function(resp) {

            console.log("Calling Blockchain API at " + url)

            var body = '';

            resp.on('data', function(chunk) {
                body += chunk;
            });

            resp.on('end', function() {
                try
                {
                    console.log('Blockchain returns: ' + body);

                    res.json(JSON.parse(body));
                }
                catch(e)
                {
                    msg.error = e;
                }           

            });
        }).on('error', function(e) {
            msg.error = e;
        });
    }
};

This is the first part, blockchain will respond you with some data, you should store the input_address returned and send payment to that (expose it to the public).

After you send a payment to input_address you should create a new module to handle the Blockchain callback.

Create something like this and go on following the doc at https://blockchain.info/api/api_receive (Implementing The Callback)

app.js

[...]
app.get('<your-callback-path>/:value/:input_address/:confirmations/:transaction_hash/:input_transaction_hash/:destination_address', api.blockchainCallback);
[...]

blockchain.js:

exports.blockchainCallback = function(req, res) {
   // Go on and save/store the payment
   // remember to send *ok* result string when you are done
   res.send("*ok*");
});

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM