-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsepay-webhooks.php
349 lines (306 loc) · 13.3 KB
/
sepay-webhooks.php
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* NukeViet Content Management System
* @version 4.x
* @author VINADES.,JSC <[email protected]>
* @copyright (C) 2009-2023 VINADES.,JSC. All rights reserved
* @license GNU/GPL version 2 or any later version
* @see https://github.com/nukeviet The NukeViet CMS GitHub project
*/
/*
* https://domain.com/sepay-webhooks.php
*/
define('NV_MAINFILE', true);
define('NV_SYSTEM', true);
define('NV_ROOTDIR', pathinfo(str_replace(DIRECTORY_SEPARATOR, '/', __FILE__), PATHINFO_DIRNAME));
$global_config = [];
require NV_ROOTDIR . '/includes/constants.php';
require realpath(NV_ROOTDIR . '/' . NV_CONFIG_FILENAME);
date_default_timezone_set('Asia/Ho_Chi_Minh');
define('NV_START_TIME', microtime(true));
define('NV_CURRENTTIME', time());
$_time_zone_db = preg_replace('/^([\+|\-]{1}\d{2})(\d{2})$/', '$1:$2', date('O'));
$driver_options = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_PERSISTENT => $db_config['persistent'],
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$returnData = [];
// Bắt buộc cho Sepay biết nếu là thành công
$returnData['success'] = false;
$returnData['message'] = 'No information!';
$dsn = $db_config['dbtype'] . ':dbname=' . $db_config['dbname'] . ';host=' . $db_config['dbhost'] . ';charset=' . $db_config['charset'];
if (!empty($db_config['dbport'])) {
$dsn .= ';port=' . $db_config['dbport'];
}
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
try {
$db = new PDO($dsn, $db_config['dbuname'], $db_config['dbpass'], $driver_options);
$db->exec("SET SESSION time_zone='" . $_time_zone_db . "'");
} catch (PDOException $e) {
$returnData['message'] = $e->getMessage();
jsonOut($returnData, 500);
}
$module_data = 'wallet';
// Lấy cổng thanh toán sepay
$sql = "SELECT * FROM " . $db_config['prefix'] . "_" . $module_data . "_payment WHERE payment='sepay'";
$row_payment = $db->query($sql)->fetch();
if (empty($row_payment)) {
$returnData['message'] = 'SePay config not found!!!';
jsonOut($returnData, 404);
}
$payment_config = empty($row_payment['config']) ? [] : unserialize(base64_decode(strtr($row_payment['config'], '-_,', '+/='), true));
if (empty($payment_config) or !is_array($payment_config)) {
$returnData['message'] = 'SePay config wrong!!!';
jsonOut($returnData, 404);
}
$payment_config['account_no'] = empty($payment_config['account_no']) ? [] : array_map('trim', explode(',', $payment_config['account_no']));
$payment_config['account_name'] = empty($payment_config['account_no']) ? [] : array_map('trim', explode(',', $payment_config['account_name']));
$payment_config['acq_id'] = empty($payment_config['account_no']) ? [] : array_map('trim', explode(',', $payment_config['acq_id']));
$payment_config['bank_branch'] = empty($payment_config['account_no']) ? [] : array_map('trim', explode(';', $payment_config['bank_branch']));
if (empty($payment_config['account_no'])) {
$returnData['message'] = 'No account allowed!!!';
jsonOut($returnData, 404);
}
// Kiểm tra key xác thực đúng
$http_authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
$apikey_receive = '';
if (preg_match('/^Apikey[\s]+([0-9a-zA-Z]{32})$/', $http_authorization, $m)) {
$apikey_receive = $m[1];
}
if (empty($apikey_receive)) {
$returnData['message'] = 'No APIKey found!';
jsonOut($returnData, 401);
}
if ($apikey_receive !== $payment_config['apikey']) {
$returnData['message'] = 'Wrong APIKey!';
jsonOut($returnData, 401);
}
// Xác định IP máy chủ sepay
$client_ip = $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_VIA'] ?? $_SERVER['HTTP_X_COMING_FROM'] ?? $_SERVER['HTTP_COMING_FROM'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['HTTP_X_FORWARDED'] ?? $_SERVER['HTTP_FORWARDED_FOR'] ?? $_SERVER['HTTP_FORWARDED'] ?? $_SERVER['REMOTE_ADDR'] ?? '';
$allowed_ips = empty($payment_config['sepay_ips']) ? [] : array_filter(array_unique(array_map('trim', explode(',', $payment_config['sepay_ips']))));
if (!empty($allowed_ips) and !in_array($client_ip, $allowed_ips, true)) {
$returnData['message'] = 'IP not allowed!';
jsonOut($returnData, 403);
}
$array = [];
$array['id'] = intval($_POST['id'] ?? 0);
$array['gateway'] = htmlspecialchars($_POST['gateway'] ?? '');
$array['transactionDate'] = intval(strtotime($_POST['transactionDate'] ?? ''));
$array['accountNumber'] = htmlspecialchars($_POST['accountNumber'] ?? '');
$array['transferType'] = $_POST['transferType'] ?? '';
$array['transferAmount'] = round(floatval($_POST['transferAmount'] ?? 0));
$array['accumulated'] = round(floatval($_POST['accumulated'] ?? 0));
$array['transferContent'] = trim(htmlspecialchars($_POST['content'] ?? ''));
$array['subAccount'] = trim(htmlspecialchars($_POST['subAccount'] ?? ''));
$array['referenceCode'] = trim(htmlspecialchars($_POST['referenceCode'] ?? ''));
$array['description'] = trim(htmlspecialchars($_POST['description'] ?? ''));
if (preg_match('/(GD|WP)([0-9]{10})/', $array['transferContent'], $m)) {
$transaction_type = $m[1] == 'WP' ? 'pay' : 'recharge';
$transaction_id = intval($m[2]);
$array['status'] = 1;
} else {
$transaction_type = '';
$transaction_id = 0;
$array['status'] = 0;
}
// Kiểm tra ID
if (empty($array['id'])) {
$returnData['message'] = 'No id found!';
jsonOut($returnData, 400);
}
// Kiểm tra số tài khoản nhận đúng
if (empty($array['accountNumber'])) {
$returnData['message'] = 'No accountNumber found!';
jsonOut($returnData, 400);
}
if (!in_array($array['accountNumber'], $payment_config['account_no'])) {
$returnData['message'] = 'Wrong accountNumber!';
jsonOut($returnData, 400);
}
// Lấy kiểu, chỉ chấp nhận kiểu tiền vào
if (empty($array['transferType'])) {
$returnData['message'] = 'No transferType found!';
jsonOut($returnData, 400);
}
if ($array['transferType'] !== 'in') {
$returnData['message'] = 'Wrong transferType!';
jsonOut($returnData, 400);
}
// Lưu log giao dịch
try {
$sql = "INSERT IGNORE INTO " . $db_config['prefix'] . "_" . $module_data . "_sepay_logs (
id, gateway, banktime, addtime, content, transfer_amount, accumulated, sub_account, reference_code, description, status
) VALUES (
" . $array['id'] . ",
" . $db->quote($array['gateway']) . ",
" . $array['transactionDate'] . ",
" . NV_CURRENTTIME . ",
" . $db->quote($array['transferContent']) . ",
" . $array['transferAmount'] . ",
" . $array['accumulated'] . ",
" . $db->quote($array['subAccount']) . ",
" . $db->quote($array['referenceCode']) . ",
" . $db->quote($array['description']) . ",
" . $array['status'] . "
)";
$db->query($sql);
} catch (Exception $e) {
// Nothing
}
// Số tiền giao dịch
if (empty($array['transferAmount'])) {
$returnData['message'] = 'No transferAmount found!';
jsonOut($returnData, 400);
}
if ($array['transferAmount'] < 0) {
$returnData['message'] = 'Wrong transferAmount!';
jsonOut($returnData, 400);
}
// Message giao dịch
if (empty($array['transferContent'])) {
$returnData['message'] = 'No content found!';
jsonOut($returnData, 400);
}
if (empty($array['status'])) {
$returnData['message'] = 'The transaction code is not recognized!';
jsonOut($returnData, 400);
}
// Lấy giao dịch đã lưu vào CSDL trước đó
$stmt = $db->prepare("SELECT * FROM " . $db_config['prefix'] . "_" . $module_data . "_transaction WHERE id = :id");
$stmt->bindParam(':id', $transaction_id, PDO::PARAM_INT);
$stmt->execute();
$transaction = $stmt->fetch();
if (empty($transaction)) {
// Không tìm thấy giao dịch
$returnData['message'] = 'Transaction not found!';
jsonOut($returnData, 404);
}
// Kiểm tra trạng thái giao dịch phải bằng 0
if ($transaction['transaction_status'] != 0) {
$returnData['message'] = 'This transaction has been processed!';
jsonOut($returnData, 403);
}
// Kiểm tra số tiền giao dịch mà ít hơn số tiền trong hóa đơn
if (floatval($transaction['money_net']) > $array['transferAmount']) {
$returnData['message'] = 'Transaction amount is not enough!';
jsonOut($returnData, 403);
}
$transaction['transaction_data'] = unserialize($transaction['transaction_data']);
if (empty($transaction['transaction_data']) or !is_array($transaction['transaction_data']) or empty($transaction['transaction_data']['to_account'])) {
$returnData['message'] = 'Transaction data is wrong!';
jsonOut($returnData, 404);
}
if ($transaction['transaction_data']['to_account'] !== $array['accountNumber']) {
$returnData['message'] = 'Account not equal!';
jsonOut($returnData, 403);
}
$referenceCode = mb_substr(htmlspecialchars($_POST['referenceCode'] ?? ''), 0, 200);
unset($_POST['accumulated'], $_POST['description']);
$transaction_data = serialize($_POST);
if ($transaction_type == 'pay') {
/*
* Thanh toán đơn hàng
*/
// Tìm đơn hàng
$stmt = $db->prepare("SELECT * FROM " . $db_config['prefix'] . "_" . $module_data . "_orders WHERE id = :id");
$stmt->bindParam(':id', $transaction['order_id'], PDO::PARAM_STR);
$stmt->execute();
$order_info = $stmt->fetch();
if (empty($order_info)) {
// Không tìm thấy đơn hàng
$returnData['message'] = 'Order not found!';
jsonOut($returnData, 404);
}
// Giao dịch đã được xử lý
if ($order_info['paid_status'] != 0) {
// Đơn hàng đã được xử lý
$returnData['message'] = 'This order has been processed!';
jsonOut($returnData, 403);
}
// Cập nhật trạng thái giao dịch
$sql = 'UPDATE ' . $db_config['prefix'] . "_" . $module_data . '_transaction SET
transaction_id = ' . $db->quote($referenceCode) . ', transaction_status=4,
transaction_time=' . NV_CURRENTTIME . ', transaction_data=' . $db->quote($transaction_data) . '
WHERE id=' . $transaction_id;
if (!$db->exec($sql)) {
$returnData['message'] = 'Can not update transaction!';
jsonOut($returnData, 500);
}
// Cập nhật trạng thái đơn hàng
$check = $db->exec("UPDATE " . $db_config['prefix'] . "_" . $module_data . "_orders SET
paid_status=4,
paid_id=" . $db->quote(sprintf('WP%010s', $transaction['id'])) . ",
paid_time=" . NV_CURRENTTIME . "
WHERE id=" . $order_info['id']);
if (!$check) {
$returnData['message'] = 'Can not update order!';
jsonOut($returnData, 500);
}
// Cập nhật lại trạng thái xử lý
$sql = "UPDATE " . $db_config['prefix'] . "_" . $module_data . "_sepay_logs SET mapping_status=1 WHERE id=" . $array['id'];
$db->query($sql);
$returnData['success'] = true;
$returnData['message'] = 'Success!';
jsonOut($returnData, 201);
}
// Nạp tiền vào ví
$sql = 'UPDATE ' . $db_config['prefix'] . "_" . $module_data . '_transaction SET
transaction_id = ' . $db->quote($referenceCode) . ', transaction_status=4,
transaction_time=' . NV_CURRENTTIME . ', transaction_data=' . $db->quote($transaction_data) . '
WHERE id=' . $transaction_id;
if (!$db->exec($sql)) {
$returnData['message'] = 'Can not update transaction!';
jsonOut($returnData, 500);
}
$sql = "SELECT * FROM " . $db_config['prefix'] . "_" . $module_data . "_money WHERE
userid=" . $transaction['userid'] . " AND money_unit=" . $db->quote($transaction['money_unit']);
$result = $db->query($sql);
$check = false;
if ($result->rowCount()) {
$sql = "UPDATE " . $db_config['prefix'] . "_" . $module_data . "_money SET
money_in= money_in+" . $transaction['money_total'] . ",
money_total = money_total+" . $transaction['money_total'] . "
WHERE userid= " . $transaction['userid'] . " AND money_unit=" . $db->quote($transaction['money_unit']);
$check = $db->exec($sql);
} else {
$sql = "INSERT INTO " . $db_config['prefix'] . "_" . $module_data . "_money (
userid, created_time, created_userid, status, money_unit, money_in, money_out, money_total, note, tokenkey
) VALUES (
" . $transaction['userid'] . ", " . NV_CURRENTTIME . ", 0, 1, " . $db->quote($transaction['money_unit']) . ",
'" . $transaction['money_total'] . "', 0, '" . $transaction['money_total'] . "',
" . $db->quote('System auto creat account') . ", ''
)";
$check = $db->exec($sql);
}
if (!$check) {
$returnData['message'] = 'Can not update money!';
jsonOut($returnData, 500);
}
// Cập nhật lại trạng thái xử lý
$sql = "UPDATE " . $db_config['prefix'] . "_" . $module_data . "_sepay_logs SET mapping_status=1 WHERE id=" . $array['id'];
$db->query($sql);
$returnData['success'] = true;
$returnData['message'] = 'Success!';
jsonOut($returnData, 201);
/**
* @param array $json
* @param int $code
*/
function jsonOut($json, $code = 200)
{
http_response_code($code);
$html_headers = [];
$html_headers['X-Frame-Options'] = 'SAMEORIGIN';
$html_headers['Content-Type'] = 'application/json';
$html_headers['Last-Modified'] = gmdate('D, d M Y H:i:s', strtotime('-1 day')) . ' GMT';
$html_headers['Cache-Control'] = 'max-age=0, no-cache, no-store, must-revalidate'; // HTTP 1.1.
$html_headers['Pragma'] = 'no-cache'; // HTTP 1.0.
$html_headers['Expires'] = '-1'; // Proxies.
$html_headers['X-Content-Type-Options'] = 'nosniff';
$html_headers['X-XSS-Protection'] = '1; mode=block';
ob_start('ob_gzhandler');
echo json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit(0);
}