Skip to content

Commit 99e8147

Browse files
authored
Create Payments.php
1 parent 5799372 commit 99e8147

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

src/Payments/Payments.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/*
3+
the project created by @wizardloop
4+
*/
5+
namespace JewishPulse\Payments;
6+
7+
use JewishPulse\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+
$this->context->messages->sendMessage(peer: $adminId, message: $text, parse_mode: $parseMode);
104+
}
105+
} catch (\Throwable $e) {
106+
}
107+
}
108+
109+
}

0 commit comments

Comments
 (0)