Skip to content

Commit

Permalink
Merge pull request #137 from xendit/feat/support-xp-v2-accounts
Browse files Browse the repository at this point in the history
Feat/support xp v2 accounts
  • Loading branch information
xen-HendryZheng authored Feb 15, 2022
2 parents 96920d7 + 7c3dca9 commit 41acabf
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 3 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ For PCI compliance to be maintained, tokenization of credit cards info should be
+ [Get Transaction](#get-transaction)
+ [List Transactions](#list-transactions)
* [XenPlatform Service](#xenplatform-service)
+ [Create sub-accounts](#create-sub-accounts)
+ [Create sub-account](#create-sub-account)
+ [Create sub-account using V2](#create-sub-account-using-v2)
+ [Get sub-account by ID](#get-sub-account-by-id)
+ [Update sub-account](#update-sub-account)
+ [Set Callback URL](#set-callback-url)
+ [Create transfers](#create-transfers)
+ [Create fee rules](#create-fee-rules)
Expand Down Expand Up @@ -1291,7 +1294,7 @@ p.createAccount({
Refer to [Xendit API Reference](https://developers.xendit.co/api-reference/#xenplatform) for more info about methods' parameters
#### Create sub-accounts
#### Create sub-account
```ts
p.createAccount(data: {
Expand All @@ -1303,6 +1306,38 @@ p.createAccount(data: {
})
```
#### Create sub-account using V2
```ts
p.createV2Account(data: {
email: string;
type: string;
publicProfile?: {
businessName: string;
};
})
```
#### Get sub-account by ID
```ts
p.getAccountByID(data: {
id: string;
})
```
#### Update sub-account
```ts
p.updateAccount(data: {
id: string;
email: string;
publicProfile?: {
businessName: string;
};
})
```
#### Set Callback URL
```ts
Expand Down
29 changes: 29 additions & 0 deletions examples/with_async/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,35 @@ const p = new Platform({});
// eslint-disable-next-line no-console
console.log('created fee rule detail:', feeRule);

const accountV2 = await p.createV2Account({
email: `example+${Date.now().toString()}@gmail.com`,
type: 'OWNED',
publicProfile: {
businessName: `example+${Date.now().toString()}`,
},
});

// eslint-disable-next-line no-console
console.log('created account details (using V2):', accountV2);

const getAccount = await p.getAccountByID({
id: accountV2.id,
});

// eslint-disable-next-line no-console
console.log('get account details: ', getAccount);

const updateAccount = await p.updateAccount({
id: accountV2.id,
email: `example_updated+${Date.now().toString()}@gmail.com`,
publicProfile: {
businessName: `example_updated+${Date.now().toString()}`,
},
});

// eslint-disable-next-line no-console
console.log('update account details: ', updateAccount);

process.exit(0);
} catch (e) {
console.error(e); // eslint-disable-line no-console
Expand Down
38 changes: 38 additions & 0 deletions examples/with_promises/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ p.createAccount({
console.log('created fee rule detail:', r);
return r;
})
.then(() =>
p.createV2Account({
email: `example+${Date.now().toString()}@gmail.com`,
type: 'OWNED',
publicProfile: {
businessName: `example+${Date.now().toString()}`,
},
}),
)
.then(r => {
// eslint-disable-next-line no-console
console.log('created account details (using V2):', r);
return r;
})
.then(r =>
p.getAccountByID({
id: r.id,
}),
)
.then(r => {
// eslint-disable-next-line no-console
console.log('get account details: ', r);
return r;
})
.then(r =>
p.updateAccount({
id: r.id,
email: `example_updated+${Date.now().toString()}@gmail.com`,
publicProfile: {
businessName: `example_updated+${Date.now().toString()}`,
},
}),
)
.then(r => {
// eslint-disable-next-line no-console
console.log('update account details: ', r);
return r;
})
.catch(e => {
console.error(e); // eslint-disable-line no-console
process.exit(1);
Expand Down
23 changes: 23 additions & 0 deletions integration_test/platform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ module.exports = function() {
],
}),
)
.then(() =>
p.createV2Account({
email: `example+${Date.now().toString()}@gmail.com`,
type: 'OWNED',
publicProfile: {
businessName: `example+${Date.now().toString()}`,
},
}),
)
.then(r =>
p.getAccountByID({
id: r.id,
}),
)
.then(r =>
p.updateAccount({
id: r.id,
email: `example_updated+${Date.now().toString()}@gmail.com`,
publicProfile: {
businessName: `example_updated+${Date.now().toString()}`,
},
}),
)
.then(() => {
// eslint-disable-next-line no-console
console.log('Platform integration test done...');
Expand Down
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.8",
"version": "1.19.9",
"description": "NodeJS client for Xendit API",
"main": "index.js",
"types": "index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions src/platform/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ export = class Platform {
businessName: string;
};
}): Promise<object>;
createV2Account(data: {
email: string;
type: string;
publicProfile?: {
businessName: string;
};
}): Promise<object>;
getAccountByID(data: { id: string }): Promise<object>;
updateAccount(data: {
id: string;
email: string;
publicProfile?: {
businessName: string;
};
}): Promise<object>;
setCallbackURL(data: {
type: string;
url: string;
Expand Down
67 changes: 67 additions & 0 deletions src/platform/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,73 @@ Platform.prototype.createAccount = function(data) {
});
};

Platform.prototype.createV2Account = function(data) {
return promWithJsErr((resolve, reject) => {
let validationFields = ['email', 'type'];
if (data.type === 'OWNED') {
validationFields.push('publicProfile');
}
Validate.rejectOnMissingFields(validationFields, data, reject);
const body = { email: data.email, type: data.type };
if (data.publicProfile) {
body.public_profile = {
business_name: data.publicProfile.businessName,
};
}
fetchWithHTTPErr(`${this.API_ENDPOINT}/v2/accounts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: Auth.basicAuthHeader(this.opts.secretKey),
},
body: JSON.stringify(body),
})
.then(resolve)
.catch(reject);
});
};

Platform.prototype.getAccountByID = function(data) {
return promWithJsErr((resolve, reject) => {
Validate.rejectOnMissingFields(['id'], data, reject);
fetchWithHTTPErr(`${this.API_ENDPOINT}/v2/accounts/${data.id}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: Auth.basicAuthHeader(this.opts.secretKey),
},
})
.then(resolve)
.catch(reject);
});
};

Platform.prototype.updateAccount = function(data) {
return promWithJsErr((resolve, reject) => {
let validationFields = ['email', 'id'];
if (data.type === 'OWNED') {
validationFields.push('publicProfile');
}
Validate.rejectOnMissingFields(validationFields, data, reject);
const body = { email: data.email };
if (data.publicProfile) {
body.public_profile = {
business_name: data.publicProfile.businessName,
};
}
fetchWithHTTPErr(`${this.API_ENDPOINT}/v2/accounts/${data.id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: Auth.basicAuthHeader(this.opts.secretKey),
},
body: JSON.stringify(body),
})
.then(resolve)
.catch(reject);
});
};

Platform.prototype.setCallbackURL = function(data) {
return promWithJsErr((resolve, reject) => {
Validate.rejectOnMissingFields(['type', 'url'], data, reject);
Expand Down
60 changes: 60 additions & 0 deletions test/platform/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const ACCOUNT_EMAIL = '[email protected]';
const ID = '61fb6d4d92b2ff75d2b45059';
const BUSINESS_NAME = 'angies pink panther';
const UPDATED_EMAIL = '[email protected]';
const UPDATED_BUSINESS_NAME = 'angies updated pink panther';
const TYPE = 'MANAGED';
const URL = 'https://www.xendit.co/callback_catcher';
const CALLBACK_TYPE = 'invoice';
Expand All @@ -15,6 +19,14 @@ const ROUTES = [
},
];

const PUBLIC_PROFILE = {
business_name: BUSINESS_NAME,
};

const UPDATED_PUBLIC_PROFILE = {
business_name: UPDATED_BUSINESS_NAME,
};

const VALID_CREATE_ACCOUNT_RESPONSE = {
created: '2019-01-01T08:51:44.484Z',
status: 'SUCCESSFUL',
Expand Down Expand Up @@ -51,9 +63,54 @@ const VALID_CREATE_FEE_RULE_RESPONSE = {
metadata: {},
};

const VALID_CREATE_V2_ACCOUNT_RESPONSE = {
id: ID,
created: '2022-02-01T07:00:00.000Z',
updated: '2022-02-01T07:00:00.000Z',
email: ACCOUNT_EMAIL,
type: TYPE,
public_profile: {
business_name: BUSINESS_NAME,
},
country: 'ID',
status: 'REGISTERED',
};

const VALID_GET_ACCOUNT_RESPONSE = {
id: ID,
created: '2022-02-01T07:00:00.000Z',
updated: '2022-02-01T07:00:00.000Z',
email: ACCOUNT_EMAIL,
type: TYPE,
public_profile: {
business_name: BUSINESS_NAME,
},
country: 'ID',
status: 'REGISTERED',
};

const VALID_UPDATE_ACCOUNT_RESPONSE = {
id: ID,
created: '2022-02-01T07:00:00.000Z',
updated: '2022-02-01T07:00:00.000Z',
email: UPDATED_EMAIL,
type: TYPE,
public_profile: {
business_name: UPDATED_BUSINESS_NAME,
},
country: 'ID',
status: 'REGISTERED',
};

module.exports = {
ID,
ACCOUNT_EMAIL,
TYPE,
UPDATED_EMAIL,
BUSINESS_NAME,
UPDATED_BUSINESS_NAME,
PUBLIC_PROFILE,
UPDATED_PUBLIC_PROFILE,
URL,
CALLBACK_TYPE,
REFERENCE,
Expand All @@ -66,4 +123,7 @@ module.exports = {
VALID_SET_CALLBACK_URL_RESPONSE,
VALID_CREATE_TRANSFER_RESPONSE,
VALID_CREATE_FEE_RULE_RESPONSE,
VALID_CREATE_V2_ACCOUNT_RESPONSE,
VALID_GET_ACCOUNT_RESPONSE,
VALID_UPDATE_ACCOUNT_RESPONSE,
};
Loading

0 comments on commit 41acabf

Please sign in to comment.