Skip to content

Commit

Permalink
Add Charges API and tests
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
Chris authored and abh committed Oct 5, 2011
1 parent aa8d3f8 commit ab8f27f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Access to the [Stripe](https://stripe.com/) [API](http:https://stripe.com/api).
All methods takes a callback as their last parameter. The callback is
called with an error code (if any) and then the response.

* `stripe.charges` - create, retrieve, refund and list charges
* `.create(charge) - create a charge
* `.retrieve(charge_id) - retrieve a charge by charge id
* `.refund(charge_id, amount) - refund a given charge, amount in cents
* `.list(data) - [List charges](https://stripe.com/api/docs#list_charges)
* `stripe.customers` - create, retrieve, update and delete customers
* `.create(customer)` - create a customer, takes the data as an object
* `.retrieve(customer_id)` - retrieve a customer by customer id.
Expand All @@ -54,7 +59,6 @@ To run the tests, install vows with `npm install vows` and then run

Ask Bjørn Hansen ([email protected]). Development was sponsored by [YellowBot](http:https://www.yellowbot.com/).


## License

Copyright (C) 2011 Ask Bjørn Hansen
Expand Down
20 changes: 20 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ module.exports = function (api_key, options) {
}

return {
charges: {
create: function (data, cb) {
post("/v1/charges", data, cb);
},
retrieve: function(charge_id, cb) {
if(!(charge_id && typeof charge_id === 'string')) {
cb("charge_id required");
}
get("/v1/charges/" + charge_id, {}, cb);
},
refund: function(charge_id, amount, cb) {
if(!(charge_id && typeof charge_id === 'string')) {
cb("charge_id required");
}
post("/v1/charges/" + charge_id + "/refund", { amount: amount }, cb);
},
list: function(data, cb) {
get("/v1/charges", data, cb);
},
},
customers: {
create: function (data, cb) {
post("/v1/customers", data, cb);
Expand Down
64 changes: 64 additions & 0 deletions test/charges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var vows = require('vows'),
assert = require('assert'),
sys = require('sys');

var api_key = process.env.STRIPE_API;

if (!api_key) {
sys.puts('To run vows, you must have a STRIPE_API environment variable with a test api key');
process.exit(2)
}

var stripe = require('./../lib/main.js')(api_key);

vows.describe("Charges API").addBatch({
'Create charge' : {
topic: function() {
stripe.charges.create({
amount: "50",
currency: "usd",
card: { number: "4242424242424242",
exp_month: 12,
exp_year: 2020,
name: "T. Ester",
}
}, this.callback);
},
'returns a charge' : function(err, response) {
assert.isNull(err);
console.log("response", response);
assert.isDefined(response);
assert.equal(response.object, 'charge');
assert.isDefined(response.id);
},
'Retrieve a charge' : {
topic: function(create_err, charge) {
stripe.charges.retrieve(charge.id, this.callback);
},
'Got a charge' : function(err, response) {
assert.isNull(err);
assert.isDefined(response);
assert.equal(response.object, 'charge');
assert.isDefined(response.id);
},
},
'Refund a charge' : {
topic: function(create_err, charge) {
stripe.charges.refund(charge.id, 50, this.callback);
},
'Got a refund' : function(err, response) {
assert.isNull(err);
assert.isDefined(response);
assert.isTrue(response.refunded);
},
},
},
'Charge list' : {
topic: function() {
stripe.charges.list({}, this.callback);
},
'Got count': function(err, response) {
assert.isNumber(response.count);
},
}
}).export(module, {error: false});

0 comments on commit ab8f27f

Please sign in to comment.