Skip to content

Commit

Permalink
Merge pull request #130 from xendit/feature/add-get-payments-by-fixed…
Browse files Browse the repository at this point in the history
…-payments-code-support

Feature/add get payments by fixed payments code support
  • Loading branch information
xen-HendryZheng authored Jan 24, 2022
2 parents 33a7bc6 + ab15a94 commit 24473c1
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ For PCI compliance to be maintained, tokenization of credit cards info should be
* [Retail Outlet Services](#retail-outlet-services)
+ [Create fixed payment code](#create-fixed-payment-code)
+ [Get fixed payment code](#get-fixed-payment-code)
+ [Get payments by fixed payment code ID](#get-payments-by-fixed-payment-code-id)
+ [Update fixed payment code](#update-fixed-payment-code)
+ [Simulate payment (only in dev mode)](#simulate-payment)
* [QR Code Services](#qr-code-services)
+ [Create code](#create-code)
+ [Get code](#get-code)
Expand Down Expand Up @@ -844,6 +846,12 @@ ro.createFixedPaymentCode(data: {
ro.getFixedPaymentCode(data: { id: string })
```

#### Get payments by fixed payment code ID

```ts
ro.getPaymentsByFixedPaymentCodeId(data: { id: string })
```

#### Update fixed payment code

```ts
Expand All @@ -855,6 +863,16 @@ ro.updateFixedPaymentCode(data: {
})
```

#### Simulate payment

```ts
ro.simulatePayment(data: {
retailOutletName: string;
paymentCode: string;
transferAmount: number;
})
```

### QR Code Services

Instanitiate QR Code service using constructor that has been injected with Xendit keys
Expand Down
12 changes: 12 additions & 0 deletions examples/with_async/retail_outlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ const ro = new RetailOutlet({});
// eslint-disable-next-line no-console
console.log('updated payment code details:', updatedPmCode);

await ro.simulatePayment({
retailOutletName: 'ALFAMART',
paymentCode: updatedPmCode.payment_code,
transferAmount: 12000,
});
// eslint-disable-next-line no-console
console.log('simulated payment:', updatedPmCode);

const paymentsByCodeId = await ro.getPaymentsByFixedPaymentCodeId({ id });
// eslint-disable-next-line no-console
console.log('payments by fixed payment code ID:', paymentsByCodeId);

process.exit(0);
} catch (e) {
console.error(e); // eslint-disable-line no-console
Expand Down
21 changes: 21 additions & 0 deletions examples/with_promises/retail_outlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ ro.createFixedPaymentCode({
console.log('updated payment code details:', r);
return r;
})
.then(({ id, payment_code }) =>
Promise.all([
id,
ro.simulatePayment({
retailOutletName: 'ALFAMART',
paymentCode: payment_code,
transferAmount: 12000,
}),
]),
)
.then(r => {
// eslint-disable-next-line no-console
console.log('simulated payment:', r);
return r;
})
.then(([id]) => ro.getPaymentsByFixedPaymentCodeId({ id }))
.then(r => {
// eslint-disable-next-line no-console
console.log('payments by fixed payment code ID:', r);
return r;
})
.catch(e => {
console.error(e); // eslint-disable-line no-console
process.exit(1);
Expand Down
11 changes: 11 additions & 0 deletions integration_test/retail_outlet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ module.exports = function() {
})
.then(({ id }) => ro.getFixedPaymentCode({ id }))
.then(({ id }) => ro.updateFixedPaymentCode({ id: id, expectedAmt: 12000 }))
.then(({ id, payment_code }) =>
Promise.all([
id,
ro.simulatePayment({
retailOutletName: 'ALFAMART',
paymentCode: payment_code,
transferAmount: 12000,
}),
]),
)
.then(([id]) => ro.getPaymentsByFixedPaymentCodeId({ id }))
.then(() => {
// eslint-disable-next-line no-console
console.log('Retail outlet integration test done...');
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xendit-node",
"version": "1.19.5",
"version": "1.19.6",
"description": "NodeJS client for Xendit API",
"main": "index.js",
"types": "index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/retail_outlet/retail_outlet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ export = class RetailOutlet {
expirationDate?: Date;
}): Promise<object>;
getFixedPaymentCode(data: { id: string }): Promise<object>;
getPaymentsByFixedPaymentCodeId(data: { id: string }): Promise<object>;
simulatePayment(data: {
retailOutletName: string;
paymentCode: string;
transferAmount: number;
}): Promise<object>;
};
40 changes: 40 additions & 0 deletions src/retail_outlet/retail_outlet.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,44 @@ RetailOutlet.prototype.getFixedPaymentCode = function(data) {
});
};

RetailOutlet.prototype.getPaymentsByFixedPaymentCodeId = function(data) {
return promWithJsErr((resolve, reject) => {
Validate.rejectOnMissingFields(['id'], data, reject);

fetchWithHTTPErr(`${this.API_ENDPOINT}/${data.id}/payments`, {
method: 'GET',
headers: {
Authorization: Auth.basicAuthHeader(this.opts.secretKey),
},
})
.then(resolve)
.catch(reject);
});
};

RetailOutlet.prototype.simulatePayment = function(data) {
return promWithJsErr((resolve, reject) => {
Validate.rejectOnMissingFields(
['retailOutletName', 'paymentCode', 'transferAmount'],
data,
reject,
);

fetchWithHTTPErr(`${this.API_ENDPOINT}/simulate_payment`, {
method: 'POST',
headers: {
Authorization: Auth.basicAuthHeader(this.opts.secretKey),
'Content-Type': 'application/json',
},
body: JSON.stringify({
retail_outlet_name: data.retailOutletName,
payment_code: data.paymentCode,
transfer_amount: data.transferAmount,
}),
})
.then(resolve)
.catch(reject);
});
};

module.exports = RetailOutlet;
15 changes: 15 additions & 0 deletions test/retail_outlet/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const NAME = 'Uvuvwevwevwe Osas';
const ALFMART_RETAIL_OUTLET_NAME = 'ALFAMART';
const FIXED_PAYMENT_CODE_ID = '5e0ed8797bc384e60435ec62';
const UPDATED_AMOUNT = 12000;
const API_ENDPOINT = 'https://api.xendit.co';
const AFTER_ID = '61c53c4f6';

const FIXED_PAYMENT_CODE_DETAILS = {
owner_id: '12121212',
Expand All @@ -20,6 +22,18 @@ const FIXED_PAYMENT_CODE_DETAILS = {
id: FIXED_PAYMENT_CODE_ID,
};

const PAYMENTS_BY_FIXED_PAYMENT_CODE_ID = {
data: [FIXED_PAYMENT_CODE_DETAILS],
has_more: true,
links: {
href:
`${API_ENDPOINT}/fixed_payment_code/` +
`${FIXED_PAYMENT_CODE_ID}/payments?after_id=${AFTER_ID}`,
rel: 'next',
method: 'GET',
},
};

const UPDATED_FIXED_PAYMENT_CODE_DETAILS = Object.assign(
{},
FIXED_PAYMENT_CODE_DETAILS,
Expand All @@ -36,5 +50,6 @@ module.exports = {
FIXED_PAYMENT_CODE_ID,
UPDATED_AMOUNT,
FIXED_PAYMENT_CODE_DETAILS,
PAYMENTS_BY_FIXED_PAYMENT_CODE_ID,
UPDATED_FIXED_PAYMENT_CODE_DETAILS,
};
28 changes: 28 additions & 0 deletions test/retail_outlet/retail_outlet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ before(function() {
.reply(200, TestConstants.FIXED_PAYMENT_CODE_DETAILS)
.get(`/${TestConstants.FIXED_PAYMENT_CODE_ID}`)
.reply(200, TestConstants.FIXED_PAYMENT_CODE_DETAILS)
.get(`/${TestConstants.FIXED_PAYMENT_CODE_ID}/payments`)
.reply(200, TestConstants.PAYMENTS_BY_FIXED_PAYMENT_CODE_ID)
.patch(`/${TestConstants.FIXED_PAYMENT_CODE_ID}`, {
expected_amount: TestConstants.UPDATED_AMOUNT,
})
Expand Down Expand Up @@ -84,6 +86,32 @@ describe('Retaiil Outlet Service', () => {
});
});

describe('getPaymentsByFixedPaymentCodeId', () => {
it('should retrieve payments by fixed payment code id', done => {
expect(
ro.getPaymentsByFixedPaymentCodeId({
id: TestConstants.FIXED_PAYMENT_CODE_ID,
}),
)
.to.eventually.deep.equal(
TestConstants.PAYMENTS_BY_FIXED_PAYMENT_CODE_ID,
)
.and.notify(done);
});

it('should report missing required fields', done => {
expect(ro.getPaymentsByFixedPaymentCodeId({}))
.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('updateFixedPaymentCode', () => {
it('should update a fixed payment code', done => {
expect(
Expand Down

0 comments on commit 24473c1

Please sign in to comment.