Skip to content

Commit

Permalink
Merge pull request #8 from noradomi/feature/add-tokenization-api
Browse files Browse the repository at this point in the history
Feature : add tokenization api
  • Loading branch information
qtdan authored May 9, 2023
2 parents 961a218 + c42d246 commit b32296a
Show file tree
Hide file tree
Showing 73 changed files with 4,167 additions and 480 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/src/zalopay/typings/**/*.ts
/examples
45 changes: 45 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path');

module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
env: {
es6: true,
node: true
},
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
modules: true
},
project: path.resolve(__dirname, "./tsconfig.json"),
tsconfigRootDir: __dirname
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
rules: {
quotes: ["error", "double"],
semi: ["error", "always"],
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true,
"types": {
"{}": false
}
}
]
},
overrides: [
{
files: ["*.ts"],
rules: {
"no-dupe-class-members": "off"
}
}
]
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

.idea
# dependencies
/node_modules
package-lock.json

26 changes: 26 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Contribution guidelines

### Add new API
To adding a new api, for example TokenizationAPI
- Update Open API specification in `specs` folder to latest.
- Generate request, response models by run the script in `scripts/generate.sh`
- Add new service and related resources in `services` folder.
- Update `services/index.ts` to expose new service API to client.
- Add unit tests for new service in `__tests__` folder and verify by run `npm run test`.
- Check coding style by `npm run lint` and fix issues if having by `npm run lint:fix`.

### How to contribute step-by-step
1. Fork the [zalopay-oss/zalopay-nodejs](https://github.com/zalopay-oss/zalopay-nodejs) repository.
2. Create a new branch from main in your fork. This makes it easier for you to keep track of your changes.
3. Make the desired changes to the code.
- If you are adding new functionality or fixing a bug, we recommend you add unit tests that cover it.
4. Push the changes to your fork.
5. Create a pull request to the [zalopay-oss/zalopay-nodejs](https://github.com/zalopay-oss/zalopay-nodejs) repository.
6. In your pull request, please describe in detail:
- What problem you’re solving
- Your approach to fixing the problem
- Any tests you wrote
7. Check Allow edits from maintainers.
8. Create the pull request.
9. Ensure that all checks have passed.

135 changes: 107 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ZaloPay Node.js SDK

The Zalopay Node SDK provides convenient access to the ZaloPay API from applications written in server-side JavaScript or TypeScript.
The ZaloPay Node SDK provides convenient access to the ZaloPay API from applications written in server-side JavaScript
or TypeScript.

## Installation

Expand All @@ -10,41 +11,119 @@ Run:
npm i @zalopay-oss/zalopay-nodejs
```

## Quick Start
## Usage

1. Create a client with some initial configuration
```javascript
// Step 1: Import the parts of the module you want to use
const {
ZaloPayClient,
TokenizationAPI,
AgreementBindRequest,
AgreementBindResponse
} = require("@zalopay-oss/zalopay-nodejs");

```ts
import { ZaloPayClient } from "@zalopay-oss/zalopay-nodejs";
// Step 2: Initialize the ZaloPay client object with your merchant information
const client = new ZaloPayClient({
appId: "your_app_id",
key1: "your_key_1",
key2: "your_key_2",
callbackUrl: "your_default_callback_url",
env: "sandbox",
});

// Step 3: Initialize the API object, eg: Tokenization API
const tokenizationAPI: TokenizationAPI = new TokenizationAPI(client);

// Step 4: Create the request object
const request: AgreementBindRequest = {
app_id: 0,
app_trans_id: "",
binding_type: BindingTypeEnum.Wallet,
callback_url: "",
identifier: "",
mac: "",
max_amount: 0,
redirect_deep_link: "",
redirect_url: "",
req_date: 0,
binding_data: ""
};

// Step 5: Make the request
tokenizationAPI.bind(request)
.then(bindResponse => console.log(bindResponse))
.catch(error => console.log(error));
```

### Step 1: Import the parts of the module you want to use

Use the Node.js `require` function to load the `ZaloPayClient` and API objects. For the name of the API objects, see [API Explorer](https://beta-docs.zalopay.vn/docs/specs/swagger-zalopay-openapi).

For example, to use the [Tokenization API](https://beta-docs.zalopay.vn/docs/specs/tokenization):

``` javascript
const { ZaloPayClient, TokenizationAPI } = require("@zalopay-oss/zalopay-nodejs");
```

### Step 2: Initialize the client object

Initialize the client object, passing the following:
- `appId`, `key1`, `key2`: The merchant keys you [registered from the Merchant Portal](https://mc.zalopay.vn/mso-v3/register)
- `callbackUrl`: The url that you want ZaloPay return after processing some APIs.
- `env`: For the test environment, use **sanbox**. For the live environment, use **production**.

For example:

``` javascript
const client = new ZaloPayClient({
appId: "your_app_id",
key1: "your_key_1",
key2: "your_key_2",
callbackUrl: "your_default_callback_url",
env: "sandbox",
appId: "your_app_id",
key1: "your_key_1",
key2: "your_key_2",
callbackUrl: "your_default_callback_url",
env: "sandbox",
});
```

2. Create a simple order through the client and handle the result

```ts
const order: CreateOrderRequest = {
appTransId: "your_app_trans_id",
appUser: "user_id",
item: JSON.stringify(items),
embedData: JSON.stringify(embed_data),
amount: 1000,
description: "description",
bankCode: "zalopayapp",
type: 'order',
### Step 3: Initialize the API object

Initialize the API object you want to use, passing the `client` object from the previous step.

For example, to use the [Tokenization API](https://beta-docs.zalopay.vn/docs/specs/tokenization):

``` javascript
const tokenizationAPI: TokenizationAPI = new TokenizationAPI(client);
```

### Step 4: Create the request object

Create the request object. For example, for a request to the `/v2/agreement/bind` endpoint:

``` javascript
const request: AgreementBindRequest = {
app_id: 0,
app_trans_id: "",
binding_type: BindingTypeEnum.Wallet,
callback_url: "",
identifier: "",
mac: "",
max_amount: 0,
redirect_deep_link: "",
redirect_url: "",
req_date: 0,
binding_data: ""
};
```

### Step 5: Make the request

client.orderProvider.create(order)
.then(result => {
console.log(result)
...
})
.catch(err => console.log(err));
Use the API object's method to make the request. For example, to make a request to the `/v2/agreement/bind` endpoint using the `TokenizationAPI` object:

``` javascript
tokenizationAPI.bind(request)
.then(bindResponse => console.log(bindResponse))
.catch(error => console.log(error));
```

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md)
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: [
"**/src/zalopay/__tests__/**/*.test.ts"
],
};
7 changes: 7 additions & 0 deletions openapitools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "6.5.0"
}
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
],
"scripts": {
"test": "jest",
"build": "tsc"
"build": "tsc",
"lint": "eslint 'src/zalopay/**/*.ts'",
"lint:fix": "eslint --fix 'src/**/*.ts'"
},
"keywords": [],
"author": "",
Expand All @@ -22,7 +24,11 @@
"@types/jest": "^29.5.0",
"@types/node": "^18.15.5",
"@types/qs": "^6.9.7",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^8.39.0",
"jest": "^29.5.0",
"nock": "^13.3.1",
"ts-jest": "^29.1.0",
"typescript": "^5.0.2"
},
Expand Down
16 changes: 16 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

# Generate models from specification
# Make sure Open API specification in specs folder is latest
openapi-generator-cli generate \
-i specs/* \
-g typescript-node \
-t templates/typescript \
-o build \
--global-property models,supportingFiles \
--additional-properties=serviceName=Tokenization \
--additional-properties=modelPropertyNaming=original \
--additional-properties=helperFunctions=./templates/helpers/camelCase.ts

rm src/zalopay/models/*
cp build/model/* src/zalopay/models
rm -r build
Loading

0 comments on commit b32296a

Please sign in to comment.