Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Added support for Bitpay test environment #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
})
Expand All @@ -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/
15 changes: 8 additions & 7 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -25,6 +25,6 @@
},
"devDependencies": {
"mocha": "1.16.x",
"assert": "1.1.x"
"assert": "1.1.x"
}
}
49 changes: 38 additions & 11 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();
}
});

Expand Down