-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (184 loc) · 6.06 KB
/
index.js
File metadata and controls
198 lines (184 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const axios = require("axios");
const moment = require("moment");
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, label, printf } = format;
const myFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
//Tracking error in log file
const logger = createLogger({
format: combine(label({ label: "shurjopay" }), timestamp(), myFormat),
transports: [
new transports.Console(),
new transports.File({ filename: "shurjopay-plugin.log" }),
],
});
function randomString(length) {
return Math.round(
Math.pow(36, length + 1) - Math.random() * Math.pow(36, length)
)
.toString(36)
.slice(1);
}
function Shurjopay() {
const _this = this;
this.data = {};
this.randomString = randomString;
this.log = (message, level) => {
logger.log({
level: level,
message: message,
});
};
//Getting credentials from merchant as parameter by calling config
this.config = function (
root_url,
merchant_username,
merchant_password,
merchant_key_prefix,
return_url
) {
this.credentials = {
root_url: root_url,
merchant_username: merchant_username,
merchant_password: merchant_password,
merchant_key_prefix: merchant_key_prefix,
return_url: return_url,
get token_url() {
return this.root_url + "/api/get_token";
},
get verification_url() {
return this.root_url + "/api/verification";
},
get payment_status_url() {
return this.root_url + "/api/payment-status";
},
};
};
/**
* Return authentication token for shurjoPay payment gateway system.
* Setup shurjopay.properties file.
*
* @return authentication details with valid token
* @throws ShurjopayException while merchant username and password is invalid.
*/
this.authentication = function (callback) {
axios
.post(this.credentials.token_url, {
username: this.credentials.merchant_username,
password: this.credentials.merchant_password,
})
.then(function (response) {
_this.data.sp_token = {
token: response.data.token,
token_type: response.data.token_type,
token_create_time: response.data.TokenCreateTime, //eg. 2022-01-13 10:55:12am
token_valid_duration: response.data.expires_in, //eg. 3600
};
callback(response.data);
})
.catch(function (error) {
_this.log(
"Did not receive auth token from shurjopay. Check your credentials.",
"error"
);
});
};
/**
* This method is used for making payment.
*
* @param Payment request object. See the shurjoPay version-2 integration documentation(beta).docx for details.
* @return Payment response object contains redirect URL to reach payment page,token_details,order_id, form_data to verify order in shurjoPay.
* @throws ShurjopayException while merchant username and password is invalid.
* @throws ShurjopayPaymentException while {#link PaymentReq} is not prepared properly or {#link HttpClient} exception
*/
this.makePayment = function (
checkout_params,
checkout_callback,
error_handler
) {
this.authentication((data) => {
axios
.post(data.execute_url, {
...checkout_params,
prefix: _this.credentials.merchant_key_prefix,
store_id: data.store_id,
token: data.token,
return_url: _this.credentials.return_url,
cancel_url: _this.credentials.return_url,
})
.then(function (response) {
checkout_callback(response.data);
})
.catch(function (checkout_error) {
error_handler(checkout_error);
});
});
};
/**
* This method is used for verifying order by order id which could be get by payment response object
*
* @param orderId
* @return order object if order verified successfully
* @throws ShurjopayException while merchant user name and password is invalid.
* @throws ShurjopayVerificationException while token_type, token, order id is invalid or payment is not initiated properly or {#link HttpClient} exception
*/
this.verifyPayment = function (order_id, callback, error_handler) {
this.authentication((data) => {
axios({
method: "post",
url: this.credentials.verification_url,
headers: {
"content-type": "application/json",
Authorization: data.token_type + " " + data.token,
},
data: { order_id: order_id },
})
.then(function (response) {
callback(response.data);
})
.catch(function (verify_error) {
error_handler(verify_error);
});
});
};
/**
* This method is used for verifying order by order id which could be get by payment response object
*
* @param orderId
* @return order object if order verified successfully
* @throws ShurjopayException while merchant user name and password is invalid.
* @throws ShurjopayVerificationException while order id is invalid or payment is not initiated properly or {#link HttpClient} exception
*/
this.paymentStatus = function (order_id, callback, error_handler) {
this.authentication((data) => {
axios({
method: "post",
url: this.credentials.payment_status_url,
headers: {
"content-type": "application/json",
Authorization: data.token_type + " " + data.token,
},
data: { order_id: order_id },
})
.then(function (response) {
callback(response.data);
})
.catch(function (verify_error) {
error_handler(verify_error);
});
});
};
//validate token expiring time
this.token_valid = function () {
let create_time_obj = moment(
_this.data.sp_token.token_create_time,
"YYYY-MM-DD hh:mm:ssa"
);
let dur_seconds = moment().subtract(create_time_obj).format("ss");
return dur_seconds < _this.data.sp_token.token_valid_duration;
};
}
module.exports = function () {
return new Shurjopay();
};