-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaincash.js
83 lines (78 loc) · 2.14 KB
/
zaincash.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
const axios = require("axios").default;
const jwt = require("jsonwebtoken");
class ZainCash {
constructor({
production,
msisdn,
secret,
merchantId,
lang,
exp,
redirectUrl,
serviceType,
}) {
this.Account = {
msisdn,
secret,
merchantId,
lang,
exp,
redirectUrl,
time: Date.now(),
serviceType,
};
if (production) {
this.Account.initUrl = "https://api.zaincash.iq/transaction/init";
this.Account.requestUrl = "https://api.zaincash.iq/transaction/pay?id=";
} else {
this.Account.initUrl = "https://test.zaincash.iq/transaction/init";
this.Account.requestUrl = "https://test.zaincash.iq/transaction/pay?id=";
}
}
checkout = async ({ amount, orderId }) => {
// Building the transaction data to be encoded in a JWT token
const data = {
serviceType: this.Account.serviceType,
amount,
msisdn: this.Account.msisdn,
orderId,
redirectUrl: this.Account.redirectUrl,
iat: this.Account.time,
exp: this.Account.time + 60 * 60 * this.Account.exp,
};
// Encoding the data
const token = jwt.sign(data, this.Account.secret);
// Preparing the payment data to be sent to ZC api
const postData = {
token: token,
merchantId: this.Account.merchantId,
lang: this.Account.lang,
};
// Initilizing a ZC order by sending a request with the tokens
let response = await axios.post(this.Account.initUrl, postData);
const OperationId = response.data.id;
if (OperationId) {
return {
status: true,
url: this.Account.requestUrl + OperationId,
transactionId: OperationId,
};
}
return { status: false, error: { mesage: response.data.err } };
};
verifyToken = (token) => {
if (token) {
try {
var decoded = jwt.verify(token, this.Account.secret);
} catch (err) {
return { status: false, error: err };
}
if (decoded.status == "success") {
return { status: true, decoded };
} else {
return { status: false, error: decoded };
}
}
};
}
module.exports = ZainCash;