-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayeer.js
128 lines (104 loc) · 3.58 KB
/
payeer.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
const crypto = require('crypto');
class Payeer {
constructor(m_shop, m_key) {
this.m_shop = m_shop;
this.m_key = m_key;
}
async generatePayment(m_orderid, m_amount, m_curr, m_desc) {
const m_sign = this.generateSignature(m_orderid, m_amount, m_curr, Buffer.from(m_desc).toString('base64'));
const form = `https://payeer.com/merchant/?m_shop=${this.m_shop}&m_orderid=${m_orderid}&m_amount=${m_amount}&m_curr=${m_curr}&m_desc=${Buffer.from(m_desc).toString('base64')}&m_sign=${m_sign}&lang=en`;
return form;
}
validatePaymentStatus(paymentData) {
// const trustedIps = ['185.71.65.92', '185.71.65.189', '149.202.17.210'];
// // IP doğrulaması
// if (!trustedIps.includes(paymentData.ip)) {
// throw new Error('Invalid IP');
// }
const signHash = this.generatePaymentStatusSignature(paymentData);
if (paymentData.m_sign === signHash && paymentData.m_status === 'success') {
return true;
} else {
return false;
}
}
generateSignature(m_orderid, m_amount, m_curr, m_desc) {
const arHash = [
this.m_shop,
m_orderid,
m_amount,
m_curr,
m_desc,
this.m_key
];
const sign = crypto.createHash('sha256')
.update(arHash.join(':'))
.digest('hex')
.toUpperCase();
return sign;
}
generatePaymentStatusSignature(paymentData) {
const arHash = [
paymentData.m_operation_id,
paymentData.m_operation_ps,
paymentData.m_operation_date,
paymentData.m_operation_pay_date,
paymentData.m_shop,
paymentData.m_orderid,
paymentData.m_amount,
paymentData.m_curr,
paymentData.m_desc,
paymentData.m_status
];
if (paymentData.m_params) {
arHash.push(paymentData.m_params);
}
arHash.push(this.m_key);
return crypto.createHash('sha256')
.update(arHash.join(':'))
.digest('hex')
.toUpperCase();
}
}
const express = require('express');
const bodyParser = require('body-parser');
const { default: axios } = require('axios');
const app = express();
app.use(bodyParser.json({ limit: '50mb', extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(require('multer')().none());
app.use(async (req, res, next) => {
console.log(req.body);
console.log(req.url, req.method);
next();
});
const payeer = new Payeer('XXXXXX', 'XXXXXXXX');
app.get('/payeer_2175693165.txt', (req, res) => {
res.send('2175693165');
});
app.get('/', async (req, res) => {
const m_orderid = '123456';
const m_amount = '0.01';
const m_curr = 'RUB';
const m_desc = "Add funds to account";
const form = await payeer.generatePayment(m_orderid, m_amount, m_curr, m_desc);
console.log(form);
res.json({ form });
});
app.post('/status', (req, res) => {
try {
console.log(req.body);
const isValid = payeer.validatePaymentStatus(req.body);
console.log(isValid);
if (isValid) {
res.send(`${req.body.m_orderid}|success`);
} else {
res.send(`${req.body.m_orderid}|error`);
}
} catch (error) {
res.status(500).send('Payment validation failed');
}
});
app.listen(80, () => {
console.log('Server running on port 3000');
});