From 5ff5e99fd55128d9a49a7ebc03c5e9f414491baf Mon Sep 17 00:00:00 2001 From: Dharnit <85389770+dharmasatrya@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:23:19 +0700 Subject: [PATCH 1/8] RegionalRetailOutlet --- examples/with_async/regional_retail_outlet.js | 36 +++++++ .../with_promises/regional_retail_outlet.js | 39 ++++++++ package-lock.json | 2 +- src/regional_retail_outlet/index.d.ts | 3 + src/regional_retail_outlet/index.js | 3 + .../regional_retail_outlet.d.ts | 28 ++++++ .../regional_retail_outlet.js | 95 +++++++++++++++++++ src/xendit.js | 4 + 8 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 examples/with_async/regional_retail_outlet.js create mode 100644 examples/with_promises/regional_retail_outlet.js create mode 100644 src/regional_retail_outlet/index.d.ts create mode 100644 src/regional_retail_outlet/index.js create mode 100644 src/regional_retail_outlet/regional_retail_outlet.d.ts create mode 100644 src/regional_retail_outlet/regional_retail_outlet.js diff --git a/examples/with_async/regional_retail_outlet.js b/examples/with_async/regional_retail_outlet.js new file mode 100644 index 0000000..97e9de8 --- /dev/null +++ b/examples/with_async/regional_retail_outlet.js @@ -0,0 +1,36 @@ +const x = require('../xendit'); + +const RegionalRetailOutlet = x.RegionalRetailOutlet; +const ro = new RegionalRetailOutlet({}); + +(async function() { + try { + const pmCode = await ro.createPaymentCode({ + referenceId: 'test_dharma_61', + channelCode: '7ELEVEN', + customerName: 'Dharma', + amount: 50, + currency: 'PHP', + market: 'PH' + }); + // eslint-disable-next-line no-console + console.log('fixed payment code created:', pmCode); + + const { id } = pmCode; + const retrievedPmCode = await ro.getPaymentCode({ id }); + // eslint-disable-next-line no-console + console.log('fixed payment code details:', retrievedPmCode); + + const updatedPmCode = await ro.updatePaymentCode({ + id, + expectedAmt: 12000, + }); + // eslint-disable-next-line no-console + console.log('updated payment code details:', updatedPmCode); + + process.exit(0); + } catch (e) { + console.error(e); // eslint-disable-line no-console + process.exit(1); + } +})(); diff --git a/examples/with_promises/regional_retail_outlet.js b/examples/with_promises/regional_retail_outlet.js new file mode 100644 index 0000000..fdd4867 --- /dev/null +++ b/examples/with_promises/regional_retail_outlet.js @@ -0,0 +1,39 @@ +const x = require('../xendit'); + +const RegionalRetailOutlet = x.RegionalRetailOutlet; +const ro = new RegionalRetailOutlet(); + +ro.createPaymentCode({ + referenceId: 'test_dharma_3', + channelCode: '7ELEVEN', + customerName: 'Dharma', + amount: 50, + currency: 'PHP', + market: 'PH' +}) + .then(r => { + // eslint-disable-next-line no-console + console.log('fixed payment code created:', r); + return r; + }) + .then(({ id }) => ro.getPaymentCode({ id })) + .then(r => { + // eslint-disable-next-line no-console + console.log('fixed payment code details:', r); + return r; + }) + .then(({ id }) => { + return ro.updatePaymentCode({ + id, + amount: 30, + }); + }) + .then(r => { + // eslint-disable-next-line no-console + console.log('updated payment code details:', r); + return r; + }) + .catch(e => { + console.error(e); // eslint-disable-line no-console + process.exit(1); + }); diff --git a/package-lock.json b/package-lock.json index c6ba547..670de1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "xendit-node", - "version": "1.17.0", + "version": "1.17.1", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/src/regional_retail_outlet/index.d.ts b/src/regional_retail_outlet/index.d.ts new file mode 100644 index 0000000..3c47edf --- /dev/null +++ b/src/regional_retail_outlet/index.d.ts @@ -0,0 +1,3 @@ +import RegionalRetailOutletService from './regional_retail_outlet'; + +export { RegionalRetailOutletService }; diff --git a/src/regional_retail_outlet/index.js b/src/regional_retail_outlet/index.js new file mode 100644 index 0000000..c9ac636 --- /dev/null +++ b/src/regional_retail_outlet/index.js @@ -0,0 +1,3 @@ +const RegionalRetailOutletService = require('./regional_retail_outlet'); + +module.exports = { RegionalRetailOutletService }; diff --git a/src/regional_retail_outlet/regional_retail_outlet.d.ts b/src/regional_retail_outlet/regional_retail_outlet.d.ts new file mode 100644 index 0000000..4b42dbf --- /dev/null +++ b/src/regional_retail_outlet/regional_retail_outlet.d.ts @@ -0,0 +1,28 @@ +import { XenditOptions } from '../xendit_opts'; + +export = class RegionalRetailOutlet { + constructor({}); + static _constructorWithInjectedXenditOpts: ( + opts: XenditOptions, + ) => typeof RetailOutlet; + createPaymentCode(data: { + reference_id: string; + channel_code: string; + amount: number; + currency: number; + customer_name: string; + market: string; + payment_code?: string; + expires_at?: Date; + is_single_use?: boolean; + desciption?: string; + metadata?: object[]; + }): Promise; + updateFixedPaymentCode(data: { + id: string; + name?: string; + expectedAmt?: number; + expirationDate?: Date; + }): Promise; + getFixedPaymentCode(data: { id: string }): Promise; +}; diff --git a/src/regional_retail_outlet/regional_retail_outlet.js b/src/regional_retail_outlet/regional_retail_outlet.js new file mode 100644 index 0000000..d7d73c9 --- /dev/null +++ b/src/regional_retail_outlet/regional_retail_outlet.js @@ -0,0 +1,95 @@ +const { promWithJsErr, Validate, fetchWithHTTPErr, Auth } = require('../utils'); + +const REGIONAL_RETAIL_OUTLET_PATH = '/payment_codes'; + +function RegionalRetailOutlet(options) { + let aggOpts = options; + if ( + RegionalRetailOutlet._injectedOpts && + Object.keys(RegionalRetailOutlet._injectedOpts).length > 0 + ) { + aggOpts = Object.assign({}, options, RegionalRetailOutlet._injectedOpts); + } + + this.opts = aggOpts; + this.API_ENDPOINT = this.opts.xenditURL + REGIONAL_RETAIL_OUTLET_PATH; +} + +RegionalRetailOutlet._injectedOpts = {}; +RegionalRetailOutlet._constructorWithInjectedXenditOpts = function(options) { + RegionalRetailOutlet._injectedOpts = options; + return RegionalRetailOutlet; +}; + +RegionalRetailOutlet.prototype.createPaymentCode = function(data) { + return promWithJsErr((resolve, reject) => { + Validate.rejectOnMissingFields( + ['referenceId', 'channelCode', 'amount', 'currency', 'customerName', 'market'], + data, + reject, + ); + + fetchWithHTTPErr(`${this.API_ENDPOINT}`, { + method: 'POST', + headers: { + Authorization: Auth.basicAuthHeader(this.opts.secretKey), + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + reference_id: data.referenceId, + channel_code: data.channelCode, + amount: data.amount, + currency: data.currency, + customer_name: data.customerName, + market: data.market, + payment_code: data.paymentCode, + description: data.description, + metadata: data.metadata, + expires_at: data.expiresAt + ? data.expiresAt.toISOString() + : undefined, + is_single_use: data.isSingleUse, + }), + }) + .then(resolve) + .catch(reject); + }); +}; + +RegionalRetailOutlet.prototype.updatePaymentCode = function(data) { + return promWithJsErr((resolve, reject) => { + Validate.rejectOnMissingFields(['id'], data, reject); + + fetchWithHTTPErr(`${this.API_ENDPOINT}/${data.id}`, { + method: 'PATCH', + headers: { + Authorization: Auth.basicAuthHeader(this.opts.secretKey), + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + amount: data.amount, + currency: data.amount, + customer_name: data.customerName + }), + }) + .then(resolve) + .catch(reject); + }); +}; + +RegionalRetailOutlet.prototype.getPaymentCode = 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); + }); +}; + +module.exports = RegionalRetailOutlet; diff --git a/src/xendit.js b/src/xendit.js index a1000dd..f980b84 100644 --- a/src/xendit.js +++ b/src/xendit.js @@ -11,6 +11,7 @@ const { QrCode } = require('./qr_code'); const { PlatformService } = require('./platform'); const { CustomerService } = require('./customer'); const { DirectDebitService } = require('./direct_debit'); +const { RegionalRetailOutletService } = require('./regional_retail_outlet'); const Errors = require('./errors'); function Xendit(options) { @@ -38,6 +39,9 @@ function Xendit(options) { this.RetailOutlet = RetailOutletService._constructorWithInjectedXenditOpts( this.opts, ); + this.RegionalRetailOutlet = RegionalRetailOutletService._constructorWithInjectedXenditOpts( + this.opts, + ); this.QrCode = QrCode._constructorWithInjectedXenditOpts(this.opts); this.Platform = PlatformService._constructorWithInjectedXenditOpts(this.opts); this.Customer = CustomerService._constructorWithInjectedXenditOpts(this.opts); From 5b4d22e3654c0ae0a2ddffe91fd6e640c0f38f8e Mon Sep 17 00:00:00 2001 From: Dharnit <85389770+dharmasatrya@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:25:31 +0700 Subject: [PATCH 2/8] update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da976cc..0fdc1f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xendit-node", - "version": "1.19.1", + "version": "1.19.3", "description": "NodeJS client for Xendit API", "main": "index.js", "types": "index.d.ts", From 93a11ced40f92957182c68933e56078ee92fe45f Mon Sep 17 00:00:00 2001 From: Dharnit <85389770+dharmasatrya@users.noreply.github.com> Date: Tue, 4 Jan 2022 21:39:01 +0700 Subject: [PATCH 3/8] added unit test for Regional RO --- examples/with_async/regional_retail_outlet.js | 4 +-- .../with_promises/regional_retail_outlet.js | 6 ++--- integration_test/index.js | 1 + .../regional_retail_outlet.test.js | 27 +++++++++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 integration_test/regional_retail_outlet.test.js diff --git a/examples/with_async/regional_retail_outlet.js b/examples/with_async/regional_retail_outlet.js index 97e9de8..79011c3 100644 --- a/examples/with_async/regional_retail_outlet.js +++ b/examples/with_async/regional_retail_outlet.js @@ -7,7 +7,7 @@ const ro = new RegionalRetailOutlet({}); try { const pmCode = await ro.createPaymentCode({ referenceId: 'test_dharma_61', - channelCode: '7ELEVEN', + channelCode: 'CEBUANA', customerName: 'Dharma', amount: 50, currency: 'PHP', @@ -23,7 +23,7 @@ const ro = new RegionalRetailOutlet({}); const updatedPmCode = await ro.updatePaymentCode({ id, - expectedAmt: 12000, + customerName: "DharmaLain", }); // eslint-disable-next-line no-console console.log('updated payment code details:', updatedPmCode); diff --git a/examples/with_promises/regional_retail_outlet.js b/examples/with_promises/regional_retail_outlet.js index fdd4867..befcf14 100644 --- a/examples/with_promises/regional_retail_outlet.js +++ b/examples/with_promises/regional_retail_outlet.js @@ -4,8 +4,8 @@ const RegionalRetailOutlet = x.RegionalRetailOutlet; const ro = new RegionalRetailOutlet(); ro.createPaymentCode({ - referenceId: 'test_dharma_3', - channelCode: '7ELEVEN', + referenceId: 'test_dharma_4', + channelCode: 'CEBUANA', customerName: 'Dharma', amount: 50, currency: 'PHP', @@ -25,7 +25,7 @@ ro.createPaymentCode({ .then(({ id }) => { return ro.updatePaymentCode({ id, - amount: 30, + customerName: "DharmaLain", }); }) .then(r => { diff --git a/integration_test/index.js b/integration_test/index.js index 1126486..cc499a8 100644 --- a/integration_test/index.js +++ b/integration_test/index.js @@ -13,6 +13,7 @@ Promise.all([ require('./platform.test')(), require('./customer.test')(), require('./direct_debit.test')(), + require('./regional_retail_outlet.test')(), ]) .then(() => { // eslint-disable-next-line no-console diff --git a/integration_test/regional_retail_outlet.test.js b/integration_test/regional_retail_outlet.test.js new file mode 100644 index 0000000..7237371 --- /dev/null +++ b/integration_test/regional_retail_outlet.test.js @@ -0,0 +1,27 @@ +const x = require('./xendit.test'); + +const RegionalRetailOutlet = x.RegionalRetailOutlet; +const ro = new RegionalRetailOutlet({}); + +const dynamicReferenceId = Math.floor((Math.random() * 9999) + 1) + +module.exports = function() { + return ro + .createPaymentCode({ + referenceId: `test_dharma_${dynamicReferenceId}`, + channelCode: 'CEBUANA', + customerName: 'Dharma', + amount: 50, + currency: 'PHP', + market: 'PH' + }) + .then(({ id }) => ro.getPaymentCode({ id })) + .then(({ id }) => ro.updatePaymentCode({ id: id, customerName: "DharmaLain" })) + .then(() => { + // eslint-disable-next-line no-console + console.log('Regional Retail outlet integration test done...'); + }) + .catch(e => { + throw new Error(`Regional RO integration tests failed with error: ${e.message}`); + }); +}; From d06024990e8415972fb580032eeb022e975496af Mon Sep 17 00:00:00 2001 From: xen-HendryZheng <90175540+xen-HendryZheng@users.noreply.github.com> Date: Wed, 5 Jan 2022 11:22:18 +0700 Subject: [PATCH 4/8] Update test.yml --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9daeeb2..52e1140 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,4 +25,4 @@ jobs: - run: npm test - run: npm run test:integration env: - SECRET_KEY: xnd_development_chBDpzjQZEI8nncBLrWtwau5r7rgiuunCJ4JCqsd68rXemDd74BnRpdO0bIZAMK + SECRET_KEY: xnd_development_Va7pPIfYxIo7letKqhZCkwniwggDGyjB0sAzmOUe1mMkED4k0nLh1TvtVi0raNne From 718656965049b6e01d8fcec14c799bbfb8c69d4c Mon Sep 17 00:00:00 2001 From: Dharnit <85389770+dharmasatrya@users.noreply.github.com> Date: Wed, 5 Jan 2022 13:18:05 +0700 Subject: [PATCH 5/8] added testfile for xendit ph --- integration_test/regional_retail_outlet.test.js | 2 +- integration_test/xendit_ph.test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 integration_test/xendit_ph.test.js diff --git a/integration_test/regional_retail_outlet.test.js b/integration_test/regional_retail_outlet.test.js index 7237371..33388a7 100644 --- a/integration_test/regional_retail_outlet.test.js +++ b/integration_test/regional_retail_outlet.test.js @@ -1,4 +1,4 @@ -const x = require('./xendit.test'); +const x = require('./xendit_ph.test'); const RegionalRetailOutlet = x.RegionalRetailOutlet; const ro = new RegionalRetailOutlet({}); diff --git a/integration_test/xendit_ph.test.js b/integration_test/xendit_ph.test.js new file mode 100644 index 0000000..fe2d6cd --- /dev/null +++ b/integration_test/xendit_ph.test.js @@ -0,0 +1,12 @@ +// For actual usage, this should be require('xendit-node') +const Xendit = require('../src/xendit'); +const dotenv = require('dotenv'); + +dotenv.config(); + +const xph = new Xendit({ + secretKey: process.env.SECRET_KEY_PH, + xenditURL: process.env.XENDIT_URL, +}); + +module.exports = xph; From 250ad43b810e0b549713a8cef7cfc81d47c4ddc8 Mon Sep 17 00:00:00 2001 From: xen-HendryZheng <90175540+xen-HendryZheng@users.noreply.github.com> Date: Wed, 5 Jan 2022 13:36:24 +0700 Subject: [PATCH 6/8] Update test.yml --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 52e1140..9daeeb2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,4 +25,4 @@ jobs: - run: npm test - run: npm run test:integration env: - SECRET_KEY: xnd_development_Va7pPIfYxIo7letKqhZCkwniwggDGyjB0sAzmOUe1mMkED4k0nLh1TvtVi0raNne + SECRET_KEY: xnd_development_chBDpzjQZEI8nncBLrWtwau5r7rgiuunCJ4JCqsd68rXemDd74BnRpdO0bIZAMK From 176304783c45d6fe1513ba6d171d2de6e033ae97 Mon Sep 17 00:00:00 2001 From: xen-HendryZheng <90175540+xen-HendryZheng@users.noreply.github.com> Date: Wed, 5 Jan 2022 13:38:01 +0700 Subject: [PATCH 7/8] Update test.yml --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9daeeb2..593892c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,3 +26,4 @@ jobs: - run: npm run test:integration env: SECRET_KEY: xnd_development_chBDpzjQZEI8nncBLrWtwau5r7rgiuunCJ4JCqsd68rXemDd74BnRpdO0bIZAMK + SECRET_KEY_PH: xnd_development_Va7pPIfYxIo7letKqhZCkwniwggDGyjB0sAzmOUe1mMkED4k0nLh1TvtVi0raNne From 79256c14d180bca67c40285cacb62f70c6fbe3c5 Mon Sep 17 00:00:00 2001 From: Hendry Zheng Date: Wed, 5 Jan 2022 13:56:33 +0700 Subject: [PATCH 8/8] add more delay on va --- integration_test/va.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/integration_test/va.test.js b/integration_test/va.test.js index c62cc13..cdee1ad 100644 --- a/integration_test/va.test.js +++ b/integration_test/va.test.js @@ -11,7 +11,7 @@ module.exports = function () { .getVABanks() .then(banks => { return va.createFixedVA({ - externalID: 'VA-' + new Date().toLocaleString(), + externalID: 'VA-xendit-node-js998877', bankCode: banks[0].code, name: 'Stanley Nguyen', isClosed: true, @@ -23,10 +23,10 @@ module.exports = function () { return va.getFixedVA({ id }) }) .then(({ id }) => { + sleepFor(5000); return va.updateFixedVA({ id, - suggestedAmt: 10000, - expectedAmt: 10000, + expectedAmt: 12000, }); }) .then(() => {