-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSadadPaymentGateway.js
114 lines (104 loc) · 2.67 KB
/
SadadPaymentGateway.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
const helper = require('./lib/helper')
const moment = require('moment')
/**
* @typedef {object} Config
* @property {string} key
* @property {string} merchantId
* @property {string> terminalId
* @property {string> returnUrl
*/
class SadadPaymentGateway {
/**
* @constructor
* @class SadadPaymentGateway
* @param {Config} config required Sadad Payment gateway configuration
*/
constructor(config) {
if (!config) {
throw new Error('Missed configs')
}
else if (!helper.validateConfig(config)) {
throw new Error('Invalid config')
}
else {
this._config = config
}
}
/**
* @description get token for make payment
* @memberOf SadadPaymentGateway
* @param {number} amount In rials.
* @param {string} orderId
* @return {Promise<{ResCode:string,Token:string,Description:string,redirectURL:string}>} @see {@link module:PaymentRequestResCode}
*/
async getPaymentToken(amount, orderId) {
const dateTime = moment().format('M/D/Y h:m:s a')
const signData = helper.encryptPkcs7(`${this._config.terminalId};${orderId};${amount}`, this._config.key)
const data = {
TerminalId: this._config.terminalId,
MerchantId: this._config.merchantId,
Amount: amount,
SignData: signData,
ReturnUrl: this._config.returnUrl,
LocalDateTime: dateTime,
OrderId: orderId,
}
try {
const results = await helper.getToken(data)
return {
results: results,
status: true,
redirectURL: `https://sadad.shaparak.ir/VPG/Purchase?Token=${results.Token}`,
}
}
catch (err) {
return {
results: {
message: err.message,
},
status: false,
}
}
}
/**
* @description verify the payment
* @memberOf SadadPaymentGateway
* @param {string} orderId
* @param {string} token
* @param {number} resCode
* @return {Promise<{status:boolean,results:object,message:string}>} @see {@link module:VerifyResCode}
*/
async verifyPayment(orderId, token, resCode) {
if (resCode.toString() === '0') {
const verifyData = {
Token: token,
SignData: helper.encryptPkcs7(token, this._config.key),
}
const res = await helper.verifyPayment(verifyData)
if (res.ResCode.toString() === '-1') {
return {
message: 'تراکنش نا موفق بود در صورت کسر مبلغ از حساب شما حداکثر پس از 72 ساعت مبلغ به حسابتان برمی گردد.',
results: res,
status: false,
}
}
else {
return {
status: true,
message: 'تراکنش موفق',
results: res,
}
}
}
else {
return {
status: false,
message: 'invalid res code',
results: {
invalidResponseSendFromBank: resCode,
},
}
}
}
}
module.exports = SadadPaymentGateway