-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
108 lines (96 loc) · 4.19 KB
/
api.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
<?php
function get_transaction_status($transaction_hash) {
$api_url = "https://apilist.tronscanapi.com/api/transaction-info?hash={$transaction_hash}";
try {
$response = file_get_contents($api_url);
$data = json_decode($response, true);
if (isset($data['contractRet'])) {
return $data;
} else {
return null;
}
} catch (Exception $e) {
echo "An error occurred: {$e->getMessage()}";
return null;
}
}
function simulate_transaction_verification($transaction_datetime) {
$current_datetime = new DateTime();
$time_difference = $current_datetime->diff($transaction_datetime);
// If the transaction is sent within 5 minutes
if ($time_difference->i <= 5) {
return true;
} else {
return false;
}
}
function verify_payment_with_hash($expected_amount) {
$transaction_hash = $_GET['hash'];
$transaction_data = get_transaction_status($transaction_hash);
$receiver_address = '';
if (!empty($transaction_data['trc20TransferInfo']) && isset($transaction_data['trc20TransferInfo'][0]['to_address'])) {
$receiver_address = $transaction_data['trc20TransferInfo'][0]['to_address'];
} elseif (isset($transaction_data['contractData']['to_address'])) {
$receiver_address = $transaction_data['contractData']['to_address'];
}
$myTrxAddress = 'TUz2LvQsWr3wCBdwDGhwAV6FNBADKQ4npH';
$result = array(
"Status" => "",
"SentAmount" => "",
"ExpectedAmount" => "",
"ReceivedAmount" => "NOT",
"Message" => ""
);
if ($transaction_data !== null && isset($transaction_data['contractRet']) && $myTrxAddress == $receiver_address) {
$transaction_timestamp = intval($transaction_data['timestamp']);
$transaction_datetime = DateTime::createFromFormat('U', $transaction_timestamp / 1000.0);
$verification_result = simulate_transaction_verification($transaction_datetime);
if ($verification_result) {
$status = $transaction_data['contractRet'];
$result["Status"] = $status;
if ($status == "SUCCESS") {
if (isset($transaction_data['contractData']) && isset($transaction_data['contractData']['amount'])) {
$actual_amount = intval($transaction_data['contractData']['amount']) / 10**6;
$result["SentAmount"] = $actual_amount;
$expected_amount = floatval($expected_amount);
if ($actual_amount >= $expected_amount) {
$result["ExpectedAmount"] = $expected_amount;
$result["ReceivedAmount"] = "OK";
$result["Message"] = "Your TRX has been successfully received.";
} else {
$result["ExpectedAmount"] = $expected_amount;
$result["ReceivedAmount"] = "NOT";
$result["Message"] = "Please send the requested amount of TRX.";
}
} else {
$result["ExpectedAmount"] = $expected_amount;
$result["ReceivedAmount"] = "NOT";
$result["Message"] = "There might be a problem with the hash information, please try again.";
}
} else {
$result["Status"] = "NOT";
$result["ExpectedAmount"] = $expected_amount;
$result["ReceivedAmount"] = "NOT";
$result["Message"] = "Your TRX is still in the sending stage, we are waiting :)";
}
} else {
$result["Status"] = "NOT";
$result["ExpectedAmount"] = $expected_amount;
$result["ReceivedAmount"] = "NOT";
$result["Message"] = "Payment did not occur within the expected time. Transaction failed.";
}
} else {
$result["Status"] = "NOT";
$result["ExpectedAmount"] = $expected_amount;
$result["ReceivedAmount"] = "NOT";
$result["Message"] = "Invalid hash information or wrong address.";
}
echo json_encode($result);
}
if($_GET['request'] == 'CheckTransaction'){
$expected_amount = $_GET['amount']; // Adjust the expected amount as needed
verify_payment_with_hash($expected_amount);
}else{
echo "Bad Request";
}
?>