-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
postback.php
113 lines (88 loc) · 3.58 KB
/
postback.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
<?php
include "global.php";
define('TOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('FILE_NAME', "tm_response.txt");
define('PAYMENT_SUCCESS_STATUS', 1);
define('MARKUP_PERCENTAGE', 105);
class Database {
private $link;
public function __construct($hostName, $userName, $password, $databaseName) {
$this->link = mysqli_connect($hostName, $userName, $password, $databaseName) or die ("Error connect to database");
}
public function execute($query) {
return mysqli_query($this->link, $query);
}
public function fetchObject($result) {
return @mysqli_fetch_object($result);
}
public function rowsCount($result) {
return mysqli_num_rows($result);
}
}
function logPostData($fileName) {
$toFile = '============' . PHP_EOL . date('d/m/Y G:i') . PHP_EOL;
foreach($_POST as $key => $value) {
$toFile .= "{$key}:{$value}" . PHP_EOL;
}
file_put_contents($fileName, $toFile);
}
function calculateTGR($amount, $tgrRate) {
$sumInTGR = $amount / $tgrRate;
return $sumInTGR / MARKUP_PERCENTAGE * 100;
}
function sendIt($response, $type){
$ch = curl_init('https://api.telegram.org/bot' . TOKEN . '/'.$type);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);
}
function send($id, $message, $keyboard = null) {
$data = [
'chat_id' => $id,
'text' => $message,
'parse_mode' => 'HTML',
'disable_web_page_preview' => true,
];
if ($keyboard) {
if ($keyboard == "DEL") {
$keyboard = ['remove_keyboard' => true];
}
$encodedMarkup = json_encode($keyboard);
$data['reply_markup'] = $encodedMarkup;
}
sendIt($data, 'sendMessage');
}
function getTGRrate() {
// Implement fetch rate logic here
return 0;
}
function saveTransaction($sum, $asset, $network, $type, $address) {
// Implement save transaction logic here
}
// Start here
$db = new Database($hostName, $userName, $password, $databaseName);
logPostData(FILE_NAME);
list($chatId, $rowId) = explode(":", $_POST['order_id']);
$status = (int) $_POST['status'];
if (!empty($chatId) && !empty($rowId) && $status === PAYMENT_SUCCESS_STATUS) {
$selectPaylinksQuery = "SELECT * FROM `paylinks` WHERE `rowid`='$rowId' AND (`chatid` = '$chatId' AND `status` = '0')";
$result = $db->execute($selectPaylinksQuery);
if ($db->rowsCount($result) > 0) {
$updatePaylinksQuery = "UPDATE `paylinks` SET `status`='1' WHERE `rowid`='$rowId'";
$db->execute($updatePaylinksQuery);
$tgrRate = getTGRrate();
$cleanSumTGR = calculateTGR($_POST['amount'], $tgrRate);
$selectUserQuery = "SELECT * FROM `users` WHERE `chatid`='$chatId'";
$userResult = $db->execute($selectUserQuery);
$userData = $db->fetchObject($userResult);
$newBalanceTGR = $userData->tgr_ton_full + $cleanSumTGR;
$updateUserQuery = "UPDATE `users` SET `tgr_ton_full`='$newBalanceTGR' WHERE `chatid`='$chatId'";
$db->execute($updateUserQuery);
saveTransaction($cleanSumTGR, "TGR", "TON", "buy", 0);
$message = "Поступил платеж в сумме {$_POST['amount']} USD на покупку токенов TGR. $cleanSumTGR TGR зачислено на твой баланс.";
send($chatId, $message);
}
}