@bekhruzmm/merchant-api
v1.1.2
Published
The base module for merchants
Maintainers
Readme
MerchantApi
Description
MerchantAPI module is merchant side Nodejs application which handles incoming requests sent by Paycom gateway. Read docs to know how MerchantAPI internally works
Structure
MerchantAPI is based on two main payment type strategy
- Cumulative Payment
- OneTime Payment
as example, we have provided four Mongodb schemes (Models)
Merchant is in form of json as
merchant.jsonwhich holds array of merchants; Driver object.| name | type | description | | :--- | :---: | :---- | | endpoint | String | merchant server endpoint|
Account object.
| name | type | description | | :--- | :---: | :---- | | name | String | name of the field which is identifier of a payment. f.e
user_id,order_id... | | title | Object | title of field to display{ru:"", uz:"",en:""}format |Merchant object.
| name | type | description| |:--------- | :--------:| :----- | | name | String | Merchant name | | organization | String | Organization title | | driver | Driver | | | account | Account[] | | | address | String | Address of organization| | logo | String | Logo of Merchant| | test_key | String | To enter SandBox|
[
{
"name": "Merchant #1",
"organization": "my organization",
"driver": {
"endpoint": "http://localhost:3001"
},
"account": [
{
"name": "user_id",
"title": {
"ru": "ID пользователя"
}
}
],
"address": "Toshkent shahar, Mirzo Ulug'bek",
"logo": "Logo",
"test_key": 1
}
]- Account model in the database
{
"name": "Someone",
"balance": 5000000.0,
"status": 1,
"user_id": "1"
}- Transaction (it will be created based on account options)
{
"create_time": "2021-04-13T10:16:21.000+0000",
"receivers": [],
"account": {
"user_id": "1"
},
"amount": 1000,
"id": "60758e63c3d03f74574bf82d",
"time": 1618316899120.0,
"state": 1,
"cancel_time": "2021-04-13T12:29:24.850+0000",
"perform_time": "2021-04-13T12:29:18.520+0000",
"reason": 5,
"transaction": "60758e63c3d03f74574bf82d"
}Pre-requisites
- Node.js environment
- Mongodb
Example using Express (Node.js framework)
npm init -y
npm install @bekhruzmm/merchant-apiinside .env file in the root project directory, will have following environment variables and initialize Account
models as given examples above.
APP_PORT=3001
APP_HOST=localhost
APP_NAME=MerchantAPI
APP_MODE=developmentthen in index.js before project starts, MerchantApi must be imported and initialized.
import {MerchantAPI, errorHandler} from "@bekhruzmm/merchant-api";
import {json} from "body-parser";
app.use(json());
// app.use(cors()); if you have cors errors;
MerchantApi class has two main function to handle authentication and incoming request from Paycom gateway
new MerchantAPI(new NonrelationalStrategy("mongodb://localhost:27017/merchant_test"),
new OneTimeStrategy(new OnetimeError()),
path.join(__dirname,"merchant.json"))
app.post("/", (req: Request, res: Response, next: NextFunction) => {
const auth: string = req.headers?.authorization;
MerchantAPI.getInstance().authHandler(auth);
next();
},
async (req: Request, res: Response, next: NextFunction) => {
try {
const result = await MerchantAPI.getInstance().requestHandler(req.body);
res.status(200).send({result});
} catch (e) {
next(e);
}
});
app.use(errorHandler);
app.listen(process.env.APP_PORT, () => {
console.log(`${process.env.APP_NAME} is running on port:${process.env.APP_PORT} with mode:${process.env.APP_MODE}`);
});
Next
- install sandbox frontend and backend;
