Skip to content

Commit

Permalink
Add support for Customer Balance Transaction resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed May 4, 2019
1 parent 3f7b135 commit 3f09b5f
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/resources/CustomerBalanceTransactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var StripeResource = require('../StripeResource');

/**
* CustomerBalanceTransactions is a unique resource in that, upon instantiation,
* requires a customerId, and therefore each of its methods only
* require the transactionId argument.
*
* This streamlines the API specifically for the case of accessing a customer balance tranaction
* on a returned customer object.
*
* E.g. customerObject.transactions.retrieve(transactionId)
* (As opposed to the also-supported stripe.customers.retrieveTaxId(customerId, transactionId))
*/
module.exports = StripeResource.extend({
path: 'customers/{customerId}/customer_balance_transactions',
includeBasic: ['create', 'list', 'retrieve'],
});
29 changes: 29 additions & 0 deletions lib/resources/Customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,33 @@ module.exports = StripeResource.extend({
path: '/{customerId}/tax_ids/{taxIdId}',
urlParams: ['customerId', 'taxIdId'],
}),

/**
* Customer: CustomerBalanceTransactions methods
*/

createTransaction: stripeMethod({
method: 'POST',
path: '/{customerId}/customer_balance_transactions',
urlParams: ['customerId'],
}),

deleteTransaction: stripeMethod({
method: 'DELETE',
path: '/{customerId}/customer_balance_transactions/{transactionId}',
urlParams: ['customerId', 'transactionId'],
}),

listTransactions: stripeMethod({
method: 'GET',
path: '/{customerId}/customer_balance_transactions',
urlParams: ['customerId'],
methodType: 'list',
}),

retrieveTransaction: stripeMethod({
method: 'GET',
path: '/{customerId}/customer_balance_transactions/{transactionId}',
urlParams: ['customerId', 'transactionId'],
}),
});
1 change: 1 addition & 0 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var resources = {
// The following rely on pre-filled IDs:
ApplicationFeeRefunds: require('./resources/ApplicationFeeRefunds'),
ChargeRefunds: require('./resources/ChargeRefunds'),
CustomerBalanceTransactions: require('./resources/CustomerBalanceTransactions'),
CustomerCards: require('./resources/CustomerCards'),
CustomerSubscriptions: require('./resources/CustomerSubscriptions'),
Persons: require('./resources/Persons'),
Expand Down
58 changes: 58 additions & 0 deletions test/resources/CustomerBalanceTransactions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var resources = require('../../lib/stripe').resources;
var stripe = require('../../testUtils').getSpyableStripe();
var expect = require('chai').expect;

var CUSTOMER_TEST_ID = 'cus_123';
var TRANSACTION_TEST_ID = 'cbtxn_123';

var taxId = new resources.CustomerBalanceTransactions(
stripe,
{customerId: CUSTOMER_TEST_ID}
);

// Use spy from existing resource:
taxId._request = stripe.customers._request;

describe('CustomerBalanceTransaction Resource', function() {
describe('create', function() {
it('Sends the correct request', function() {
var data = {
amount: 1234,
currency: 'usd',
};
taxId.create(data);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'POST',
url: '/v1/customers/' + CUSTOMER_TEST_ID + '/customer_balance_transactions',
data: data,
headers: {},
});
});
});

describe('list', function() {
it('Sends the correct request', function() {
taxId.list();
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/customers/' + CUSTOMER_TEST_ID + '/customer_balance_transactions',
data: {},
headers: {},
});
});
});

describe('retrieve', function() {
it('Sends the correct request', function() {
taxId.retrieve(TRANSACTION_TEST_ID);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/customers/' + CUSTOMER_TEST_ID + '/customer_balance_transactions/' + TRANSACTION_TEST_ID,
data: {},
headers: {},
});
});
});
});
43 changes: 43 additions & 0 deletions test/resources/Customers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,4 +793,47 @@ describe('Customers Resource', function() {
});
});
});

describe('CustomerBalanceTransaction methods', function() {
describe('retrieveTransaction', function() {
it('Sends the correct request', function() {
stripe.customers.retrieveTransaction('cus_123', 'cbtxn_123');
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/customers/cus_123/customer_balance_transactions/cbtxn_123',
headers: {},
data: {},
});
});
});

describe('createTransaction', function() {
it('Sends the correct request', function() {
var data = {
amount: 1234,
currency: 'usd',
};
stripe.customers.createTransaction('cus_123', data);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'POST',
url: '/v1/customers/cus_123/customer_balance_transactions',
headers: {},
data: data,
});
});
});

describe('listTransactions', function() {
it('Sends the correct request', function() {
stripe.customers.listTransactions('cus_123');
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/customers/cus_123/customer_balance_transactions',
headers: {},
data: {},
});
});
});
});
});

0 comments on commit 3f09b5f

Please sign in to comment.