-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from xendit/feat/payout
Payouts endpoints
- Loading branch information
Showing
11 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const x = require('./xendit'); | ||
|
||
const { Payout } = x; | ||
const p = new Payout({}); | ||
|
||
p.createPayout({ | ||
externalID: Date.now().toString(), | ||
amount: 10000, | ||
}) | ||
.then(r => { | ||
console.log('created payout:', r); // eslint-disable-line no-console | ||
return r; | ||
}) | ||
.then(({ id }) => p.getPayout({ id })) | ||
.then(r => { | ||
console.log('retrieved payout:', r); // eslint-disable-line no-console | ||
return r; | ||
}) | ||
.then(({ id }) => p.voidPayout({ id })) | ||
.then(r => { | ||
console.log('payout voided:', r.id); // eslint-disable-line no-console | ||
return r; | ||
}) | ||
.catch(e => { | ||
console.error(e); // eslint-disable-line no-console | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const x = require('./xendit.test'); | ||
|
||
const { Payout } = x; | ||
const p = new Payout({}); | ||
|
||
module.exports = function() { | ||
return p | ||
.createPayout({ | ||
externalID: Date.now().toString(), | ||
amount: 10000, | ||
}) | ||
.then(({ id }) => p.getPayout({ id })) | ||
.then(({ id }) => p.voidPayout({ id })) | ||
.then(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('Payout integration test done...'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const PayoutService = require('./payout'); | ||
|
||
module.exports = { PayoutService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { XenditOptions } from '../xendit_opts'; | ||
|
||
export = class Payout { | ||
constructor({}); | ||
static _constructorWithInjectedXenditOpts: ( | ||
opts: XenditOptions, | ||
) => typeof Payout; | ||
createPayout(data: { externalID: string; amount: string }): Promise<object>; | ||
getPayout(data: { id: string }): Promise<object>; | ||
voidPayout(data: { id: string }): Promise<object>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { promWithJsErr, Validate, fetchWithHTTPErr, Auth } = require('../utils'); | ||
|
||
const PAYOUT_PATH = '/payouts'; | ||
|
||
function Payout(options) { | ||
let aggOpts = options; | ||
if (Payout._injectedOpts && Object.keys(Payout._injectedOpts).length > 0) { | ||
aggOpts = Object.assign({}, options, Payout._injectedOpts); | ||
} | ||
|
||
this.opts = aggOpts; | ||
this.API_ENDPOINT = this.opts.xenditURL + PAYOUT_PATH; | ||
} | ||
|
||
Payout._injectedOpts = {}; | ||
Payout._constructorWithInjectedXenditOpts = function(options) { | ||
Payout._injectedOpts = options; | ||
return Payout; | ||
}; | ||
|
||
Payout.prototype.createPayout = function(data) { | ||
return promWithJsErr((resolve, reject) => { | ||
Validate.rejectOnMissingFields(['externalID', 'amount'], data, reject); | ||
|
||
fetchWithHTTPErr(`${this.API_ENDPOINT}`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: Auth.basicAuthHeader(this.opts.secretKey), | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
external_id: data.externalID, | ||
amount: data.amount, | ||
}), | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
Payout.prototype.getPayout = function(data) { | ||
return promWithJsErr((resolve, reject) => { | ||
Validate.rejectOnMissingFields(['id'], data, reject); | ||
|
||
fetchWithHTTPErr(`${this.API_ENDPOINT}/${data.id}`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: Auth.basicAuthHeader(this.opts.secretKey), | ||
}, | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
Payout.prototype.voidPayout = function(data) { | ||
return promWithJsErr((resolve, reject) => { | ||
Validate.rejectOnMissingFields(['id'], data, reject); | ||
|
||
fetchWithHTTPErr(`${this.API_ENDPOINT}/${data.id}/void`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: Auth.basicAuthHeader(this.opts.secretKey), | ||
}, | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
module.exports = Payout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const EXT_ID = '123'; | ||
const PAYOUT_ID = '34a2fcb6-37d8-4c11-b2dc-c46615662f23'; | ||
const AMOUNT = 10000; | ||
const VALID_PAYOUT = { | ||
id: PAYOUT_ID, | ||
external_id: EXT_ID, | ||
amount: AMOUNT, | ||
}; | ||
|
||
module.exports = { | ||
EXT_ID, | ||
PAYOUT_ID, | ||
AMOUNT, | ||
VALID_PAYOUT, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const chai = require('chai'); | ||
const chaiAsProm = require('chai-as-promised'); | ||
const TestConstants = require('./constants'); | ||
const { expect } = chai; | ||
const nock = require('nock'); | ||
const { Errors } = require('../../src/xendit'); | ||
const Xendit = require('../../src/xendit'); | ||
|
||
const x = new Xendit({ | ||
publicKey: 'fake_public_key', | ||
secretKey: 'fake_secret_key', | ||
}); | ||
|
||
chai.use(chaiAsProm); | ||
|
||
const { Payout } = x; | ||
let p = new Payout({}); | ||
beforeEach(function() { | ||
p = new Payout({}); | ||
}); | ||
before(function() { | ||
nock(p.API_ENDPOINT) | ||
.post('', { | ||
external_id: TestConstants.EXT_ID, | ||
amount: TestConstants.AMOUNT, | ||
}) | ||
.reply(201, TestConstants.VALID_PAYOUT) | ||
.get(`/${TestConstants.PAYOUT_ID}`) | ||
.reply(200, TestConstants.VALID_PAYOUT) | ||
.post(`/${TestConstants.PAYOUT_ID}/void`) | ||
.reply(200, TestConstants.VALID_PAYOUT); | ||
}); | ||
|
||
describe('Payout Service', () => { | ||
describe('createPayout', () => { | ||
it('should create a payout', done => { | ||
expect( | ||
p.createPayout({ | ||
externalID: TestConstants.EXT_ID, | ||
amount: TestConstants.AMOUNT, | ||
}), | ||
) | ||
.to.eventually.deep.equal(TestConstants.VALID_PAYOUT) | ||
.and.notify(done); | ||
}); | ||
|
||
it('should report missing required fields', done => { | ||
expect(p.createPayout({})) | ||
.to.eventually.be.rejected.then(e => | ||
Promise.all([ | ||
expect(e).to.have.property('status', 400), | ||
expect(e).to.have.property('code', Errors.API_VALIDATION_ERROR), | ||
]), | ||
) | ||
.then(() => done()) | ||
.catch(e => done(e)); | ||
}); | ||
}); | ||
|
||
describe('getPayout', () => { | ||
it('should retrieve payout details', done => { | ||
expect(p.getPayout({ id: TestConstants.PAYOUT_ID })) | ||
.to.eventually.deep.equal(TestConstants.VALID_PAYOUT) | ||
.and.notify(done); | ||
}); | ||
|
||
it('should report missing required fields', done => { | ||
expect(p.getPayout({})) | ||
.to.eventually.be.rejected.then(e => | ||
Promise.all([ | ||
expect(e).to.have.property('status', 400), | ||
expect(e).to.have.property('code', Errors.API_VALIDATION_ERROR), | ||
]), | ||
) | ||
.then(() => done()) | ||
.catch(e => done(e)); | ||
}); | ||
}); | ||
|
||
describe('voidPayout', () => { | ||
it('should void a payout', done => { | ||
expect(p.voidPayout({ id: TestConstants.PAYOUT_ID })) | ||
.to.eventually.deep.equal(TestConstants.VALID_PAYOUT) | ||
.and.notify(done); | ||
}); | ||
|
||
it('should report missing required fields', done => { | ||
expect(p.voidPayout({})) | ||
.to.eventually.be.rejected.then(e => | ||
Promise.all([ | ||
expect(e).to.have.property('status', 400), | ||
expect(e).to.have.property('code', Errors.API_VALIDATION_ERROR), | ||
]), | ||
) | ||
.then(() => done()) | ||
.catch(e => done(e)); | ||
}); | ||
}); | ||
}); |