From f43fcdf549dda5ecce892092b9305e024306ccae Mon Sep 17 00:00:00 2001 From: Born Ready Date: Thu, 23 Oct 2014 23:35:07 +0100 Subject: [PATCH 1/6] [FEATURE] Added support for Bitpay test environment (helpful when developing locally or testing) --- README.md | 34 ++++++++++++++++++++-------------- lib/client.js | 11 ++++++++--- test/client.js | 48 +++++++++++++++++++++++++++++++++++++----------- 3 files changed, 65 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 70cd0ae..9155108 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 your NODE_ENV, like so: + + var client = new Bitpay.Client({ apiKey: process.env.BITPAY_API_KEY, environment: process.env.NODE_ENV }); + +If your environment is ***dev***, ***development*** or ***test***, 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..19a5132 100644 --- a/lib/client.js +++ b/lib/client.js @@ -8,13 +8,18 @@ function Client(options) { pass: '', sendImmediately: true } - } + } + this.baseUrl = "https://bitpay.com"; + var envs = ["dev", "development", "test"]; + if(envs.indexOf(options.environment) > -1) { + 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.url = this.baseUrl+"/api/invoice"; options.form = { price: opts.price, currency: opts.currency @@ -31,7 +36,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/test/client.js b/test/client.js index 3ea0737..7cd3427 100644 --- a/test/client.js +++ b/test/client.js @@ -12,13 +12,13 @@ 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, @@ -41,26 +41,52 @@ describe('Bitpay.Client', function() { 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; + var environment = process.env.NODE_ENV; + if (apiKey && environment) { + client = new Bitpay.Client({ apiKey: apiKey, environment: environment }); + 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(); } }); From 697c908f95d12ba38505ebf842018bd18159c8b6 Mon Sep 17 00:00:00 2001 From: Born Ready Date: Fri, 24 Oct 2014 20:53:44 +0100 Subject: [PATCH 2/6] Updated bitpay-node client js --- lib/client.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/client.js b/lib/client.js index 19a5132..2ad198e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -20,6 +20,7 @@ Client.prototype.createInvoice = function(opts, fn) { var options = new Object(this.httpOptions) options.method = "POST" options.url = this.baseUrl+"/api/invoice"; + console.log(options.url); options.form = { price: opts.price, currency: opts.currency From 8534756524e49e4adb4256738d9922b17df653fc Mon Sep 17 00:00:00 2001 From: Born Ready Date: Fri, 24 Oct 2014 21:10:28 +0100 Subject: [PATCH 3/6] [CHORE] Removing console log and bumping version --- lib/client.js | 1 - package.json | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 2ad198e..19a5132 100644 --- a/lib/client.js +++ b/lib/client.js @@ -20,7 +20,6 @@ Client.prototype.createInvoice = function(opts, fn) { var options = new Object(this.httpOptions) options.method = "POST" options.url = this.baseUrl+"/api/invoice"; - console.log(options.url); options.form = { price: opts.price, currency: opts.currency diff --git a/package.json b/package.json index 4ac505c..b7d3c00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitpay-node", - "version": "0.0.4", + "version": "0.0.5", "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" } } From 2c3b0e40922cbc6c76e9f069ca7c884893fb9cd4 Mon Sep 17 00:00:00 2001 From: Born Ready Date: Sun, 2 Nov 2014 22:10:08 +0000 Subject: [PATCH 4/6] [FEATURE] Passing through any invoice options when calling createInvoice rather than being limited to just price and currency --- lib/client.js | 5 +---- test/client.js | 2 ++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index 19a5132..26775f1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -20,10 +20,7 @@ Client.prototype.createInvoice = function(opts, fn) { var options = new Object(this.httpOptions) options.method = "POST" options.url = this.baseUrl+"/api/invoice"; - options.form = { - price: opts.price, - currency: opts.currency - }; + options.form = opts; request(options, function(err, resp, body) { result = JSON.parse(body); if (result.error) { diff --git a/test/client.js b/test/client.js index 7cd3427..b1614dc 100644 --- a/test/client.js +++ b/test/client.js @@ -23,6 +23,7 @@ describe('Bitpay.Client', function() { 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,6 +36,7 @@ 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); From a9cb3b0ffe731ea7ce5d42cdaab503be355e3198 Mon Sep 17 00:00:00 2001 From: Born Ready Date: Sun, 2 Nov 2014 22:15:18 +0000 Subject: [PATCH 5/6] 0.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b7d3c00..d44efce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitpay-node", - "version": "0.0.5", + "version": "0.0.6", "description": "A Node.js library for the Bitpay Bitcoin API", "main": "bitpay.js", "scripts": { From 07224223a58a4e5d1ac573f561defb14fe462d56 Mon Sep 17 00:00:00 2001 From: Lawrence Dabir-Alai Date: Tue, 20 Jan 2015 23:33:26 +0000 Subject: [PATCH 6/6] [ENHANCEMENT] If you pass in testEnv we'll use the bitpay test environment, otherwise we'll default to production --- README.md | 6 +++--- lib/client.js | 3 +-- test/client.js | 5 ++--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9155108..d1d25e4 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,11 @@ made using the Client object initialized with the Bitpay account credentials. var client = new Bitpay.Client({ apiKey: process.env.BITPAY_API_KEY }); -You can specify which environment you are using by passing in your NODE_ENV, like so: +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, environment: process.env.NODE_ENV }); + var client = new Bitpay.Client({ apiKey: process.env.BITPAY_API_KEY, testEnv: true }); -If your environment is ***dev***, ***development*** or ***test***, then we will use the [Bitpay test environment](https://test.bitpay.com) otherwise we default to production. +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 diff --git a/lib/client.js b/lib/client.js index 26775f1..b0c7bdc 100644 --- a/lib/client.js +++ b/lib/client.js @@ -10,8 +10,7 @@ function Client(options) { } } this.baseUrl = "https://bitpay.com"; - var envs = ["dev", "development", "test"]; - if(envs.indexOf(options.environment) > -1) { + if(options.testEnv) { this.baseUrl = "https://test.bitpay.com"; } } diff --git a/test/client.js b/test/client.js index b1614dc..36143bd 100644 --- a/test/client.js +++ b/test/client.js @@ -68,9 +68,8 @@ describe('Bitpay.Client', function() { it('should be able to use the bitpay test environment', function(done) { var apiKey = process.env.BITPAY_API_KEY; - var environment = process.env.NODE_ENV; - if (apiKey && environment) { - client = new Bitpay.Client({ apiKey: apiKey, environment: environment }); + if (apiKey) { + client = new Bitpay.Client({ apiKey: apiKey, testEnv: true }); assert.equal(client.baseUrl, "https://test.bitpay.com"); done(); } else {