Skip to content

Commit 2108b0f

Browse files
authored
Create Payments.php
1 parent 900ae99 commit 2108b0f

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

src/Payments/Payments.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/*
3+
the project created by @wizardloop
4+
*/
5+
namespace GetAnyMessage\Payments;
6+
7+
use GetAnyMessage\Locales\Lang;
8+
9+
class Payments
10+
{
11+
private object $context;
12+
public function __construct(object $context) {
13+
$this->context = $context;
14+
}
15+
16+
public function sendDonationOptions(int $senderId, int $replyToMsgId): void {
17+
try {
18+
19+
$lang = (new Lang($this->context))->getUserLang($senderId);
20+
$translate = (new Lang($this->context))->loadTranslations($lang);
21+
$payment1 = $translate['payment1'] ?? 'support me';
22+
$payment2 = $translate['payment2'] ?? 'Support me with:';
23+
$payment_text = $translate['payment_text'] ?? 'Hi, thank you for wanting to donate to me🥰<br>Choose the donation amount you would like to give👇';
24+
25+
$payload = (string) $senderId;
26+
$prices = [5, 25, 100, 150, 250, 400];
27+
$buttons = [];
28+
29+
foreach ($prices as $price) {
30+
$labeledPrice = ['_' => 'labeledPrice', 'label' => 'star', 'amount' => $price];
31+
$invoice = ['_' => 'invoice', 'currency' => 'XTR', 'prices' => [$labeledPrice]];
32+
$inputMediaInvoice = [
33+
'_' => 'inputMediaInvoice',
34+
'title' => $payment1,
35+
'description' => "$payment2 $price ⭐️",
36+
'invoice' => $invoice,
37+
'payload' => $payload,
38+
'provider_data' => 'test'
39+
];
40+
$exported = $this->context->payments->exportInvoice(invoice_media: $inputMediaInvoice);
41+
$buttons[] = ['text' => "⭐️ $price", 'url' => $exported['url']];
42+
}
43+
$inlineKeyboard = array_chunk($buttons, 3);
44+
$replyMarkup = ['inline_keyboard' => $inlineKeyboard];
45+
$inputReplyTo = ['_' => 'inputReplyToMessage', 'reply_to_msg_id' => $replyToMsgId];
46+
$this->context->messages->sendMessage(
47+
no_webpage: true,
48+
peer: $senderId,
49+
reply_to: $inputReplyTo,
50+
message: $payment_text,
51+
reply_markup: $replyMarkup,
52+
parse_mode: 'HTML',
53+
effect: 5159385139981059251
54+
);
55+
} catch (\Throwable $e) {
56+
$this->context->messages->sendMessage(peer: $senderId, message: $e->getMessage());
57+
}
58+
}
59+
60+
public function handlePreCheckout(array $update): void {
61+
try {
62+
$userId = $update['user_id'];
63+
$amount = $update['total_amount'];
64+
$queryId = $update['query_id'];
65+
$payload = $update['payload'];
66+
67+
$lang = (new Lang($this->context))->getUserLang($userId);
68+
$translate = (new Lang($this->context))->loadTranslations($lang);
69+
$payment_amount = $translate['payment_amount'] ?? 'payment_amount:';
70+
$payment_thanks = $translate['payment_thanks'] ?? '🎉 Thank you for your donation 🎉';
71+
$payment_send = $translate['payment_send'] ?? 'Donation received! 🎉';
72+
73+
$userInfo = $this->context->getInfo($userId);
74+
$firstName = $userInfo['User']['first_name'] ?? 'null';
75+
76+
$usernames = $userInfo['User']['usernames'] ?? [];
77+
$usernameList = array_map(fn($u) => "@" . $u['username'], $usernames);
78+
$usernameString = implode(" ", $usernameList);
79+
$username = $userInfo['User']['username'] ?? ($usernameString ?: '(null)');
80+
if ($username && !str_starts_with($username, '@')) {
81+
$username = "@" . $username;
82+
}
83+
84+
$success = $this->context->messages->setBotPrecheckoutResults(success: true, query_id: $queryId);
85+
if ($success) {
86+
$this->context->messages->sendMessage(
87+
peer: $userId,
88+
message: "$payment_amount $amount ⭐️\n$payment_thanks",
89+
parse_mode: 'HTML',
90+
effect: 5159385139981059251
91+
);
92+
93+
$this->sendMessageToAdmins("$payment_send\nFIRSTNAME: <a href='mention:$userId'>$firstName</a>\nID: <a href='mention:$userId'>$userId</a>\nUSERNAME: $username\n$payment_amount $amount ⭐️", 'HTML');
94+
}
95+
} catch (\Throwable $e) {
96+
}
97+
}
98+
99+
private function sendMessageToAdmins(string $text, string $parseMode): void {
100+
try{
101+
$admins = method_exists($this->context, 'getAdminIds') ? $this->context->getAdminIds() : [];
102+
foreach ($admins as $adminId) {
103+
try {
104+
$this->context->messages->sendMessage(peer: $adminId, message: $text, parse_mode: $parseMode);
105+
} catch (\Throwable $e) { continue; }
106+
}
107+
} catch (\Throwable $e) {}
108+
}
109+
110+
public function donate(int $senderId, int $msgid): void {
111+
try{
112+
$lang = (new Lang($this->context))->getUserLang($senderId);
113+
$translate = (new Lang($this->context))->loadTranslations($lang);
114+
115+
$payment1 = $translate['payment1'] ?? 'support me';
116+
$payment2 = $translate['payment2'] ?? 'Support me with:';
117+
$payment_text = $translate['payment_text'] ?? 'Hi, thank you for wanting to donate to me🥰<br>Choose the donation amount you would like to give👇';
118+
$back_menu_txt = $translate['back_menu_txt'] ?? '🔙 Back 🔙';
119+
$back_menu_data = $translate['back_menu_data'] ?? 'backStart';
120+
121+
$payload = (string) $senderId;
122+
$prices = [5, 25, 100, 150, 250, 400];
123+
$buttons = [];
124+
125+
foreach ($prices as $price) {
126+
$labeledPrice = ['_' => 'labeledPrice', 'label' => 'star', 'amount' => $price];
127+
$invoice = ['_' => 'invoice', 'currency' => 'XTR', 'prices' => [$labeledPrice]];
128+
$inputMediaInvoice = [
129+
'_' => 'inputMediaInvoice',
130+
'title' => $payment1,
131+
'description' => "$payment2 $price ⭐️",
132+
'invoice' => $invoice,
133+
'payload' => $payload,
134+
'provider_data' => 'test'
135+
];
136+
$exported = $this->context->payments->exportInvoice(invoice_media: $inputMediaInvoice);
137+
$buttons[] = ['text' => "⭐️ $price", 'url' => $exported['url']];
138+
}
139+
$inlineKeyboard = array_chunk($buttons, 3);
140+
$inlineKeyboard[] = [['text'=>$back_menu_txt,'callback_data'=>$back_menu_data]];
141+
$replyMarkup = ['inline_keyboard' => $inlineKeyboard];
142+
143+
$this->context->messages->editMessage(
144+
peer: $senderId,
145+
id: $msgid,
146+
message: $payment_text,
147+
reply_markup: $replyMarkup,
148+
parse_mode: 'HTML'
149+
);
150+
151+
} catch (\Throwable $e) {}
152+
}
153+
154+
}

0 commit comments

Comments
 (0)