Skip to content

Commit

Permalink
integration test and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmasatrya committed Nov 8, 2022
2 parents 42340c3 + 2ceccc8 commit 7dddf38
Show file tree
Hide file tree
Showing 17 changed files with 611 additions and 10 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ For PCI compliance to be maintained, tokenization of credit cards info should be
- [Set Callback URL](#set-callback-url)
- [Create transfers](#create-transfers)
- [Create fee rules](#create-fee-rules)
- [Refund Services](#refund-services)
- [Create refund](#create-refund-1)
- [List refunds](#list-refunds)
- [Get refund details by ID](#get-refund-details-by-id)
- [Contributing](#contributing)

<!-- tocstop -->
Expand Down Expand Up @@ -1599,6 +1603,68 @@ p.createFeeRule(data: {
})
```
### Refund Services
Instanitiate Refund service using constructor that has been injected with Xendit keys
```js
const { Refund } = x;
const r = new Refund();
```
Example: Create a refund
```js
r.createRefund({
invoice_id: 'your-invoice-id',
reason: 'FRAUDULENT',
amount: 1000,
}).then(({ id }) => {
console.log(`refund created with ID: ${id}`);
});
```
Refer to [Xendit API Reference](https://developers.xendit.co/api-reference/#refunds) for more info about methods' parameters
#### Create refund
```ts
r.createRefund(data: {
payment_request_id?: string;
reference_id?: string;
invoice_id?: string;
currency?: string;
amount?: number;
reason: RefundReasons;
metadata?: object;
idempotencty_key?: string;
for_user_id?: string;
})
```
#### List refunds
```ts
r.listRefunds(data: {
payment_request_id?: string;
invoice_id?: string;
payment_method_type?: string;
channel_code?: string;
limit?: number;
after_id?: string;
before_id?: string;
for_user_id?: string;
})
```
#### Get refund details by ID
```ts
r.getRefundById(data: {
id: string;
})
```
## Contributing
Running test suite
Expand Down
47 changes: 47 additions & 0 deletions examples/with_async/payment_method_v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const x = require('../xendit');

const { PaymentMethodV2 } = x;
const pm = new PaymentMethodV2({});

(async function() {
try {
let createdPaymentMethod = await pm.createPaymentMethodV2({
type: 'DIRECT_DEBIT',
reusability: 'ONE_TIME_USE',
customer_id: '16f72571-9b3a-43dc-b241-5b71f470202f',
country: 'ID',
direct_debit: {
channel_code: 'BRI',
channel_properties: {
mobile_number: '+6281299640904',
card_last_four: '8888',
card_expiry: '10/29',
email: '[email protected]',
},
},
});
console.log('created payment method', createdPaymentMethod); // eslint-disable-line no-console

const paymentMethodDetailsById = await pm.getPaymentMethodByIdV2({
id: createdPaymentMethod.id,
});
// eslint-disable-next-line no-console
console.log('retrieved payment method', paymentMethodDetailsById);

const listOfPaymentMethod = await pm.listPaymentMethodV2({});
// eslint-disable-next-line no-console
console.log('retrieved payment method list', listOfPaymentMethod);

const authorizedPaymentMethod = await pm.authorizePaymentMethodV2({
id: createdPaymentMethod.id,
auth_code: '333000',
});
// eslint-disable-next-line no-console
console.log('authorized payment method', authorizedPaymentMethod);

process.exit(0);
} catch (e) {
console.error(e); // eslint-disable-line no-console
process.exit(1);
}
})();
28 changes: 28 additions & 0 deletions examples/with_async/refund.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const x = require('../xendit');

const { Refund } = x;
const r = new Refund({});

(async function() {
try {
let refund = await r.createRefund({
invoice_id: '63676ed0eb10cf38ce0550b7',
reason: 'OTHERS',
amount: 1,
});
console.log('created refund', refund); // eslint-disable-line no-console

const refundDetails = await r.getRefundById({ id: refund.id });
// eslint-disable-next-line no-console
console.log('retrieved refund', refundDetails);

const refundList = await r.listRefunds({});
// eslint-disable-next-line no-console
console.log('list of refunds', refundList);

process.exit(0);
} catch (e) {
console.error(e); // eslint-disable-line no-console
process.exit(1);
}
})();
37 changes: 37 additions & 0 deletions examples/with_promises/payment_methods_v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const x = require('../xendit');

const PaymentMethodV2 = x.PaymentMethodV2;
const pm = new PaymentMethodV2();

pm.createPaymentMethodV2({
type: 'DIRECT_DEBIT',
reusability: 'ONE_TIME_USE',
customer_id: '16f72571-9b3a-43dc-b241-5b71f470202f',
country: 'ID',
direct_debit: {
channel_code: 'BRI',
channel_properties: {
mobile_number: '+6281299640904',
card_last_four: '8888',
card_expiry: '10/29',
email: '[email protected]',
},
},
})
.then(id => {
pm.authorizePaymentMethodV2({
id,
auth_code: '333000',
});
})
.then(id => {
pm.getPaymentMethodByIdV2({ id });
})
.then(() => {
pm.listPaymentMethodV2({});
})
.catch(e => {
throw new Error(
`payment method integration tests failed with error: ${e.message}`,
);
});
34 changes: 34 additions & 0 deletions examples/with_promises/refund.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const x = require('../xendit');

const Refund = x.Refund;
const ref = new Refund();

ref
.createRefund({
invoice_id: '63676ed0eb10cf38ce0550b7',
reason: 'OTHERS',
amount: 1,
})
.then(r => {
// eslint-disable-next-line no-console
console.log('refund created:', r);
return r;
})
.then(({ id }) => ref.getRefundById({ id }))
.then(r => {
// eslint-disable-next-line no-console
console.log('refund details:', r);
return r;
})
.then(() => {
return ref.listRefunds({});
})
.then(r => {
// eslint-disable-next-line no-console
console.log(':', r);
return r;
})
.catch(e => {
console.error(e); // eslint-disable-line no-console
process.exit(1);
});
10 changes: 5 additions & 5 deletions integration_test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ Promise.all([
// require('./ewallet.test')(),
require('./qr_code.test')(),
require('./platform.test')(),
require('./regional_retail_outlet.test'),
require('./customer.test')(),
require('./direct_debit.test')(),
require('./report.test')(),
require('./transaction.test')(),
// require('./payment_method_v2.test')
require('./payment_method_v2.test'),
// require('./refund.test')() //test disabled until refunds endpoint is fixed
])
.then(() => {
Promise.all([require('./regional_retail_outlet.test')()]).then(() =>
// eslint-disable-next-line no-console
console.log('Successful Integration Test!'),
);
// eslint-disable-next-line no-console
console.log('Successful Integration Test!');
})
.catch(e => {
console.error(e); // eslint-disable-line no-console
Expand Down
44 changes: 44 additions & 0 deletions integration_test/payment_method_v2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const x = require('./xendit.test');

const { PaymentMethodV2 } = x;
const pm = new PaymentMethodV2({});

module.exports = function() {
return pm
.createPaymentMethodV2({
type: 'DIRECT_DEBIT',
reusability: 'ONE_TIME_USE',
customer_id: '16f72571-9b3a-43dc-b241-5b71f470202f',
country: 'ID',
direct_debit: {
channel_code: 'BRI',
channel_properties: {
mobile_number: '+6281299640904',
card_last_four: '8888',
card_expiry: '10/29',
email: '[email protected]',
},
},
})
.then(id => {
pm.authorizePaymentMethodV2({
id,
auth_code: '333000',
});
})
.then(id => {
pm.getPaymentMethodByIdV2({ id });
})
.then(() => {
pm.listPaymentMethodV2({});
})
.then(() => {
// eslint-disable-next-line no-console
console.log('payment method integration test done...');
})
.catch(e => {
throw new Error(
`payment method integration tests failed with error: ${e.message}`,
);
});
};
28 changes: 28 additions & 0 deletions integration_test/refund.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const x = require('./xendit.test');

const { Refund } = x;
const r = new Refund({});

module.exports = function() {
return r
.createRefund({
invoice_id: '63676ed0eb10cf38ce0550b7',
reason: 'FRAUDULENT',
amount: 1,
})
.then(id => {
r.getRefundById({ id });
})
.then(() => {
r.listRefunds({});
})
.then(() => {
// eslint-disable-next-line no-console
console.log('QR Code integration test done...');
})
.catch(e => {
throw new Error(
`Recurring integration tests failed with error: ${e.message}`,
);
});
};
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.21.7",
"version": "1.21.9",
"description": "NodeJS client for Xendit API",
"main": "index.js",
"types": "index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/refund/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import RefundService from './refund';

export { RefundService };
3 changes: 3 additions & 0 deletions src/refund/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const RefundService = require('./refund');

module.exports = { RefundService };
39 changes: 39 additions & 0 deletions src/refund/refund.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
enum RefundReasons {
Fraudulent = 'FRAUDULENT',
Duplicate = 'DUPLICATE',
RequestedByCustomer = 'REQUESTED_BY_CUSTOMER',
Cancellation = 'CANCELLATION',
Others = 'OTHERS',
}

export = class Refund {
constructor({});
static _constructorWithInjectedXenditOpts: (
opts: XenditOptions,
) => typeof Refund;

createRefund(data: {
payment_request_id?: string;
reference_id?: string;
invoice_id?: string;
currency?: string;
amount?: number;
reason: RefundReasons;
metadata?: object;
idempotencty_key?: string;
for_user_id?: string;
}): Promise<object>;

listRefunds(data: {
payment_request_id?: string;
invoice_id?: string;
payment_method_type?: string;
channel_code?: string;
limit?: number;
after_id?: string;
before_id?: string;
for_user_id?: string;
}): Promise<object>;

getRefundById(data: { id: string }): Promise<object>;
};
Loading

0 comments on commit 7dddf38

Please sign in to comment.