diff --git a/README.md b/README.md index 70cd0ae..d1d25e4 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ ## Bitpay - Bitcoin Payment Library for Node.js -I want a simple way to use the Bitpay Bitcoin API in node.js, +I want a simple way to use the Bitpay Bitcoin API in node.js, and npm -- Node Package Manager -- is the standard way to distribute javascript libraries for Node.js. Like other HTTP client libraries a `Client` object will manage authentication -and connection to Bitpay's servers. All requests to the API will ultimately be +and connection to Bitpay's servers. All requests to the API will ultimately be made using the Client object initialized with the Bitpay account credentials. ## Installation @@ -17,16 +17,22 @@ made using the Client object initialized with the Bitpay account credentials. #### Initialzing a Client object with Bitpay Api Key var Bitpay = require('bitpay-node'); - + var client = new Bitpay.Client({ apiKey: process.env.BITPAY_API_KEY }); - -#### Creating an Invoice - - var invoiceOptions = { - price: 0.001, - currency: 'BTC' - }; - + +You can specify which environment you are using by passing in testEnv, and setting it to true, like so: + + var client = new Bitpay.Client({ apiKey: process.env.BITPAY_API_KEY, testEnv: true }); + +If you send is ***true***, then we will use the [Bitpay test environment](https://test.bitpay.com) otherwise we default to production. + +#### Creating an Invoice + + var invoiceOptions = { + price: 0.001, + currency: 'BTC' + }; + client.createInvoice(invoiceOptions, function(err, invoice) { console.log(invoice); }) @@ -51,14 +57,14 @@ Once an invoice has been created a call can be made to get its info and status. client.getInvoice('2Rpei3aKcJZUDWDSJ92oSq', function(err, invoice) { console.log(invoice); }); - + Which will return the same structure as the call to createInvoice, except now the status may have transitioned to either `paid`, `confirmed`, `complete`, `expired` or `invalid`. - + ## Tests Run the tests wiith Mocha, and make sure to specify your Bitpay API Key in environment. On the Bitpay account [API keys page](https://bitpay.com/api-keys) your can generate multiple API keys for your various applications. Enable API key access and generate a key to use in the tests: - BITPAY_API_KEY=46beb6dc657d4ceff4219a8e691b5015 mocha test/ + BITPAY_API_KEY=46beb6dc657d4ceff4219a8e691b5015 NODE_ENV=test mocha test/ diff --git a/lib/client.js b/lib/client.js index 1101a89..b0c7bdc 100644 --- a/lib/client.js +++ b/lib/client.js @@ -8,17 +8,18 @@ function Client(options) { pass: '', sendImmediately: true } - } + } + this.baseUrl = "https://bitpay.com"; + if(options.testEnv) { + this.baseUrl = "https://test.bitpay.com"; + } } Client.prototype.createInvoice = function(opts, fn) { var options = new Object(this.httpOptions) options.method = "POST" - options.url = "https://bitpay.com/api/invoice"; - options.form = { - price: opts.price, - currency: opts.currency - }; + options.url = this.baseUrl+"/api/invoice"; + options.form = opts; request(options, function(err, resp, body) { result = JSON.parse(body); if (result.error) { @@ -31,7 +32,7 @@ Client.prototype.createInvoice = function(opts, fn) { Client.prototype.getInvoice = function(invoiceId, fn) { var options = new Object(this.httpOptions); - options.url = 'https://bitpay.com/api/invoice/'+invoiceId; + options.url = this.baseUrl+'/api/invoice/'+invoiceId; console.log(options.url); options.methd = 'GET'; request(options, function(err, resp, body) { diff --git a/package.json b/package.json index 4ac505c..d44efce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitpay-node", - "version": "0.0.4", + "version": "0.0.6", "description": "A Node.js library for the Bitpay Bitcoin API", "main": "bitpay.js", "scripts": { @@ -25,6 +25,6 @@ }, "devDependencies": { "mocha": "1.16.x", - "assert": "1.1.x" + "assert": "1.1.x" } } diff --git a/test/client.js b/test/client.js index 3ea0737..36143bd 100644 --- a/test/client.js +++ b/test/client.js @@ -12,17 +12,18 @@ describe('Bitpay.Client', function() { assert.equal(err.type, 'unauthorized'); assert.equal(err.message, 'invalid api key'); done(); - }); + }); }); it('it should create a bitpay invoice', function(done) { var apiKey = process.env.BITPAY_API_KEY; if (apiKey) { - client = new Bitpay.Client({ apiKey: apiKey }); + client = new Bitpay.Client({ apiKey: apiKey }); var invoiceOptions = { price: 0.001, currency: 'BTC', + notificationURL: 'http://localhost', itemDesc: 'A Test Item to Purchase with Bitcoins!', buyerName: 'Steven Zeiler', posData: JSON.stringify({ @@ -35,32 +36,58 @@ describe('Bitpay.Client', function() { assert(!!!err); assert.equal(invoice.price, 0.001); assert.equal(invoice.currency, 'BTC'); + assert.equal(invoice.notificationURL, 'http://localhost'); assert.equal(invoice.btcPrice, '0.0010'); assert.equal(invoice.status, 'new'); console.log(invoice); done(); }); - } else { + } else { console.log('CONFIG: set ENV variable BITPAY_API_KEY and re-run test'); - assert(false); - done(); + assert(false); + done(); } }); it('it should get a bitpay invoice that already exists', function(done) { var apiKey = process.env.BITPAY_API_KEY; if (apiKey) { - client = new Bitpay.Client({ apiKey: apiKey }); - + client = new Bitpay.Client({ apiKey: apiKey }); + client.getInvoice('VZ7oTPS4Kngph99kKPDm35', function(err, inv) { - console.log(inv); + console.log(inv); done(); }); - } else { - assert(false); - done(); + } else { + assert(false); + done(); + } + }); + + it('should be able to use the bitpay test environment', function(done) { + var apiKey = process.env.BITPAY_API_KEY; + if (apiKey) { + client = new Bitpay.Client({ apiKey: apiKey, testEnv: true }); + assert.equal(client.baseUrl, "https://test.bitpay.com"); + done(); + } else { + console.log('CONFIG: set ENV variable NODE_ENV and re-run test'); + assert(false); + done(); + } + }); + + it('should be able to use the bitpay production environment', function(done) { + var apiKey = process.env.BITPAY_API_KEY; + if (apiKey) { + client = new Bitpay.Client({ apiKey: apiKey }); + assert.equal(client.baseUrl, "https://bitpay.com"); + done(); + } else { + assert(false); + done(); } });