-
Notifications
You must be signed in to change notification settings - Fork 1
/
sslcommerz.js
167 lines (165 loc) · 6.27 KB
/
sslcommerz.js
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
'use strict';
const crypto = require('crypto');
const got = require('got');
class SSLCommerz{
/**
* @constructor init SSLCommerz class
* @param {object} config
* @example config = { isSandboxMode: true, store_id: 'myid', store_passwd: 'mypasswd'}
*/
constructor(config){
this.store_mode = this.setSSLCommerzMode((typeof config.isSandboxMode === 'undefined') ? true : config.isSandboxMode);
this.store_id = config.store_id;
this.store_passwd = config.store_passwd;
this.submit_url = 'https://' + this.store_mode + '.sslcommerz.com/gwprocess/v4/api.php';
this.validation_url = 'https://' + this.store_mode + '.sslcommerz.com/validator/api/validationserverAPI.php';
this.transaction_url = 'https://' + this.store_mode + '.sslcommerz.com/validator/api/merchantTransIDvalidationAPI.php'
}
/**
* @function init_transaction get/create transaction request, POST Request
* @param {object} post_body check sslcommerz docs for all post_body params
* @returns {Promise} Response from SSLCommerz
*/
async init_transaction(post_body){
post_body['store_id'] = this.store_id;
post_body['store_passwd'] = this.store_passwd;
return this.post(this.submit_url, post_body);
}
/**
* @function hash_validate_ipn validate verify_sign with verify key hash resolved as promise
* @param {object} post_body check sslcommerz docs for all post_body params
* @returns {Promise} Boolean to match verify_sign and generated MD5 hash
*/
async hash_validate_ipn(post_body){
let verify_key = post_body.verify_key;
let verify_sign = post_body.verify_sign;
verify_key = verify_key.split(',');
verify_key = verify_key.sort();
Object.keys(post_body).forEach(key => {
if(verify_key.includes(key)){
let index = verify_key.indexOf(key);
verify_key[index] = verify_key[index] + '=' + post_body[key] + '&';
}
});
let hashed_passwd = 'store_passwd='+crypto.createHash('md5').update(this.store_passwd).digest('hex')+'&';
verify_key.push(hashed_passwd);
verify_key = verify_key.sort();
let before_verify_hash='';
Object.keys(verify_key).forEach(key => {
before_verify_hash += verify_key[key];
});
before_verify_hash = before_verify_hash.slice(0, -1);
let verify_hash = crypto.createHash('md5').update(before_verify_hash).digest('hex');
if(verify_hash === verify_sign){
return true;
}
else{
return false
}
}
/**
* @function validate_transaction_order validate order/transaction
* @param {string} validation_id val_id, sslcommerz docs
* @returns {Promise} Response from SSLCommerz
*/
async validate_transaction_order(validation_id){
let query = {
val_id: validation_id,
store_id: this.store_id,
store_passwd: this.store_passwd,
format: 'json'
}
return this.get(this.validation_url, query);
}
/**
*@function init_refund create a refund process
* @param {string} bank_transaction_id bank_tran_id check sslcommerz docs
* @param {number} refund_amount refund_amount check sslcommerz docs
* @param {string} refund_remarks refund_remarks check sslcommerz docs
* @returns {Promise} Response from SSLCommerz
*/
async init_refund(bank_transaction_id, refund_amount, refund_remarks){
let query = {
bank_tran_id: bank_transaction_id,
store_id: this.store_id,
store_passwd: this.store_passwd,
refund_amount: refund_amount,
refund_remarks: refund_remarks
}
return this.get(this.transaction_url, query);
}
/**
* @function refund_status get refund status by refund_reference_id, GET
* @param {string} refund_reference_id reund_ref_id check sslcommerz docs
* @returns {Promise} Response from SSLCommerz
*/
async refund_status(refund_reference_id){
let query = {
refund_ref_id: refund_reference_id,
store_id: this.store_id,
store_passwd: this.store_passwd
}
return this.get(this.transaction_url, query);
}
/**
* @function transaction_status_session get transaction status by sessionkey
* @param {string} sessionkey sessionkey check sslcommerz docs
* @returns {Promise} Response from SSLCommerz
*/
async transaction_status_session(sessionkey){
let query = {
sessionkey: sessionkey,
store_id: this.store_id,
store_passwd: this.store_passwd
}
return this.get(this.transaction_url, query);
}
/**
* @function transaction_status_id get transaction status by transaction_id
* @param {string} transaction_id tran_id check sslcommerz docs
* @returns {Promise} Response from SSLCommerz
*/
async transaction_status_id(transaction_id){
let query = {
tran_id: transaction_id,
store_id: this.store_id,
store_passwd: this.store_passwd
}
return this.get(this.transaction_url, query);
}
setSSLCommerzMode(isSandboxMode){
if(isSandboxMode){
return 'sandbox';
}
else{
return 'securepay';
}
}
async request(method, url, urlParams, post_body){
try{
let params = {
method: method || 'GET',
form: post_body ,
responseType: 'json'
};
params.searchParams = urlParams ? urlParams : undefined;
params.form = post_body ? post_body : undefined;
const response = await got(url, params);
return response.body;
} catch(error){
return error.response.body;
}
}
async get(url, query) {
let queryParams = [];
Object.keys(query).forEach(key => {
queryParams.push([key, query[key]]);
});
const urlParams = new URLSearchParams(queryParams);
return this.request('GET', url, urlParams, null);
}
async post(url, post_body){
return this.request('POST', url, null, post_body);
}
}
module.exports = SSLCommerz;