Skip to content

Commit df00836

Browse files
committed
First version; only supports the customer API
0 parents  commit df00836

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2011 Ask Bjørn Hansen
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# node-stripe
2+
3+
Access to the [Stripe](https://stripe.com/) [API](http://stripe.com/api).
4+
5+
6+
## Installation
7+
8+
`npm install node-strip`
9+
10+
## Usage overview
11+
12+
13+
var api_key = 'abc'; // secret stripe API key
14+
var stripe = require('stripe')(api_key);
15+
16+
var customer = stripe.customers.create({ email: '[email protected]' });
17+
console.log("customer id", customer.id);
18+
19+
20+
## API
21+
22+
All methods takes a callback as their last parameter. The callback is called with an error code (if any) and then
23+
the response.
24+
25+
* `stripe.customers` - create, retrieve, update and delete customers
26+
* `.create(customer)` - create a customer, takes the data as an object
27+
* `.retrieve(customer_id)` - retrieve a customer by customer id
28+
* `.update(customer_id, updates)` - update a customer; `updates` is an object with new parameters
29+
* `.del(customer_id)` - mark the customer deleted
30+
31+
32+
## TODO
33+
34+
See the [issue tracker](http://github.com/abh/node-stripe).
35+
36+
## Author
37+
38+
Ask Bjørn Hansen ([email protected]). Development was sponsored by [YellowBot](http://www.yellowbot.com/).
39+
40+
41+
## License
42+
43+
Copyright (C) 2011 Ask Bjørn Hansen
44+
45+
Permission is hereby granted, free of charge, to any person obtaining a copy
46+
of this software and associated documentation files (the "Software"), to deal
47+
in the Software without restriction, including without limitation the rights
48+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49+
copies of the Software, and to permit persons to whom the Software is
50+
furnished to do so, subject to the following conditions:
51+
52+
The above copyright notice and this permission notice shall be included in
53+
all copies or substantial portions of the Software.
54+
55+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
61+
THE SOFTWARE.

lib/main.js

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright 2011 Ask Bjørn Hansen, see LICENSE */
2+
"use strict";
3+
4+
var https = require('https');
5+
var querystring = require('querystring');
6+
7+
function setup_response_handler(req, callback) {
8+
if (typeof callback !== "function") {
9+
//console.log("missing callback");
10+
return;
11+
}
12+
req.on('response',
13+
function(res) {
14+
var response = '';
15+
res.setEncoding('utf8');
16+
res.on('data',
17+
function(chunk) {
18+
response += chunk;
19+
});
20+
res.on('end',
21+
function() {
22+
var err = 200 == res.statusCode ? 0 : res.statusCode;
23+
try {
24+
response = JSON.parse(response);
25+
callback(null, response);
26+
}
27+
catch(e) {
28+
console.log(response);
29+
callback(err, {});
30+
}
31+
});
32+
});
33+
34+
}
35+
36+
37+
module.exports = function (api_key, options) {
38+
var defaults = options || {};
39+
40+
var auth = 'Basic ' + new Buffer(api_key + ":").toString('base64');
41+
42+
function _request(method, path, data, callback) {
43+
44+
var request_data = querystring.stringify(data);
45+
46+
//console.log("http request", request_data);
47+
48+
var request_options = {
49+
host: 'api.stripe.com',
50+
port: '443',
51+
path: path,
52+
method: method,
53+
headers: {
54+
'Authorization': auth,
55+
'Accept': 'application/json',
56+
'Content-Type': 'application/x-www-form-urlencoded',
57+
'Content-Length': request_data.length
58+
}
59+
};
60+
61+
var req = https.request(request_options);
62+
setup_response_handler(req, callback);
63+
req.write(request_data);
64+
req.end();
65+
}
66+
67+
function post(path, data, callback) {
68+
_request('POST', path, data, callback);
69+
}
70+
71+
function get(path, data, callback) {
72+
_request('GET', path, data, callback);
73+
}
74+
75+
function del(path, data, callback) {
76+
_request('DELETE', path, data, callback);
77+
}
78+
79+
return {
80+
customers: {
81+
create: function (data, cb) {
82+
post("/v1/customers", data, cb);
83+
},
84+
retrieve: function(customer_id, cb) {
85+
get("/v1/customers/" + customer_id, {}, cb);
86+
},
87+
update: function(customer_id, data, cb) {
88+
post("/v1/customers/" + customer_id, data, cb);
89+
},
90+
del: function(customer_id, cb) {
91+
del("/v1/customers/" + customer_id, {}, cb);
92+
}
93+
}
94+
95+
96+
};
97+
}

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"author": "Ask Bjørn Hansen <[email protected]> (http://www.askask.com/)",
3+
"name": "stripe",
4+
"description": "Stripe API wrapper",
5+
"version": "0.0.1",
6+
"homepage": "https://github.com/abh/node-stripe",
7+
"repository": {
8+
"type": "git",
9+
"url": "git://github.com/abh/node-stripe.git"
10+
},
11+
"engines": {
12+
"node": ">= v0.4.10"
13+
},
14+
"main": "lib/main.js",
15+
"dependencies": {},
16+
"devDependencies": {}
17+
}

test/customers.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var vows = require('vows'),
2+
assert = require('assert'),
3+
sys = require('sys');
4+
5+
var api_key = process.env.STRIPE_API;
6+
7+
if (!api_key) {
8+
sys.puts('To run vows, you must have a STRIPE_API environment variable with a test api key');
9+
process.exit(2)
10+
}
11+
12+
var stripe = require('./../lib/main.js')(api_key);
13+
14+
vows.describe("Customer API").addBatch({
15+
'Create customer' : {
16+
topic: function() {
17+
stripe.customers.create({email: "[email protected]"}, this.callback);
18+
},
19+
'returns a customer': function(err, response) {
20+
assert.isNull(err);
21+
console.log("response", response);
22+
assert.isDefined(response);
23+
assert.equal(response.object, 'customer');
24+
assert.equal(response.email, "[email protected]");
25+
},
26+
'Retrieve customer' : {
27+
topic: function(create_err, customer) {
28+
stripe.customers.retrieve(customer.id, this.callback);
29+
},
30+
'Got customer' : function(err, response) {
31+
assert.equal( response.email, '[email protected]' );
32+
},
33+
},
34+
'Update customer' : {
35+
topic: function(create_err, customer) {
36+
stripe.customers.update(customer.id, { description: "test"}, this.callback);
37+
},
38+
'Customer updated' : function (err, response) {
39+
assert.equal(response.description, 'test');
40+
assert.equal(response.email, '[email protected]');
41+
},
42+
'Delete customer' : {
43+
topic: function(create_err, customer) {
44+
console.log("customer for deletion", customer);
45+
stripe.customers.del(customer.id, this.callback);
46+
},
47+
'Customer was deleted': function(err,response) {
48+
console.log("response", response);
49+
assert.isNull(err);
50+
assert.isTrue(response.deleted);
51+
}
52+
},
53+
},
54+
},
55+
}).export(module, {error: false});

0 commit comments

Comments
 (0)