Skip to content

Commit 41b2434

Browse files
authored
Merge pull request #9 from NikitaKharkov/support-send_invoice-method
Completed supporting sendInvoice method
2 parents 071efd0 + ee558f8 commit 41b2434

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ $bot = new Bot('telegramToken');
120120
$bot->sendDocument($sendDocument);
121121
```
122122

123+
### SendInvoice
124+
125+
```php
126+
<?php
127+
use Formapro\TelegramBot\Bot;
128+
use Formapro\TelegramBot\Update;
129+
use Formapro\TelegramBot\SendInvoice;
130+
131+
$requestBody = file_get_contents('php://input');
132+
$data = json_decode($requestBody, true);
133+
134+
$update = Update::create($data);
135+
$payload = []; // any params which you need to proccess in the transaction
136+
$providerToken = 'something:like:this'; // Token have to be taken from botFather
137+
138+
$sendInvoice = new SendInvoice(
139+
$update->getMessage()->getChat()->getId(),
140+
'Your title',
141+
'Your description of invoice',
142+
json_encode($payload),
143+
$providerToken,
144+
'12314czasdq', // unique id
145+
'UAH',
146+
[new LabeledPrice('PriceLabel_1', 3001)] // amount; here 30.01 UAH
147+
);
148+
149+
$bot = new Bot('telegramToken');
150+
$bot->sendInvoice($sendInvoice);
151+
```
152+
123153
### ReplyKeyboardMarkup
124154

125155
```php

src/Bot.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public function sendDocument(SendDocument $sendDocument): ResponseInterface
108108
]);
109109
}
110110

111+
public function sendInvoice(SendInvoice $sendInvoice)
112+
{
113+
return $this->httpClient->post($this->getMethodUrl('sendInvoice'), [
114+
'json' => get_values($sendInvoice),
115+
]);
116+
}
117+
111118
public function answerCallbackQuery(AnswerCallbackQuery $answerCallbackQuery): ResponseInterface
112119
{
113120
return $this->httpClient->post($this->getMethodUrl('answerCallbackQuery'), [

src/LabeledPrice.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Formapro\TelegramBot;
4+
5+
use function Makasim\Values\get_value;
6+
use function Makasim\Values\get_values;
7+
use function Makasim\Values\set_object;
8+
use function Makasim\Values\set_value;
9+
10+
class LabeledPrice
11+
{
12+
private $values = [];
13+
14+
public function __construct(string $label, int $amount)
15+
{
16+
set_value($this, 'label', $label);
17+
set_value($this, 'amount', $amount);
18+
}
19+
20+
public function getLabel(): string
21+
{
22+
return get_value($this, 'label');
23+
}
24+
25+
public function getAmount(): int
26+
{
27+
return get_value($this, 'amount');
28+
}
29+
}

src/SendInvoice.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Formapro\TelegramBot;
4+
5+
use function Makasim\Values\add_object;
6+
use function Makasim\Values\get_value;
7+
use function Makasim\Values\get_values;
8+
use function Makasim\Values\set_object;
9+
use function Makasim\Values\set_objects;
10+
use function Makasim\Values\set_value;
11+
12+
class SendInvoice
13+
{
14+
private $values = [];
15+
16+
private $objects = [];
17+
18+
public function __construct(
19+
int $chatId,
20+
string $title,
21+
string $description,
22+
string $payload,
23+
string $providerToken,
24+
string $startParameter,
25+
string $currency,
26+
array $prices
27+
) {
28+
set_value($this, 'chat_id', $chatId);
29+
set_value($this, 'title', $title);
30+
set_value($this, 'description', $description);
31+
set_value($this, 'payload', $payload);
32+
set_value($this, 'provider_token', $providerToken);
33+
set_value($this, 'start_parameter', $startParameter);
34+
set_value($this, 'currency', $currency);
35+
36+
if (empty($prices)) {
37+
throw new \InvalidArgumentException('Prices must be filled');
38+
}
39+
40+
foreach ($prices as $price) {
41+
$this->addPrice($price);
42+
}
43+
}
44+
45+
public function getChatId(): int
46+
{
47+
return get_value($this, 'chat_id');
48+
}
49+
50+
public function getTitle(): string
51+
{
52+
return get_value($this, 'title');
53+
}
54+
55+
public function getDescription(): string
56+
{
57+
return get_value($this, 'description');
58+
}
59+
60+
public function getPayload(): string
61+
{
62+
return get_value($this, 'payload');
63+
}
64+
65+
public function getProviderToken(): string
66+
{
67+
return get_value($this, 'provider_token');
68+
}
69+
70+
public function getStartParameter(): string
71+
{
72+
return get_value($this, 'start_parameter');
73+
}
74+
75+
public function getCurrency(): string
76+
{
77+
return get_value($this, 'currency');
78+
}
79+
80+
/**
81+
* @return LabeledPrice[]
82+
*/
83+
public function getPrices(): array
84+
{
85+
return get_value($this, 'prices');
86+
}
87+
88+
private function addPrice(LabeledPrice $price): void
89+
{
90+
add_object($this, 'prices', $price);
91+
}
92+
93+
public function setParseMode(string $parseMode): void
94+
{
95+
set_value($this, 'parse_mode', $parseMode);
96+
}
97+
98+
public function getParseMode(): ?string
99+
{
100+
return get_value($this, 'parse_mode');
101+
}
102+
103+
public function setReplyMarkup(ReplyMarkup $replyMarkup): void
104+
{
105+
set_value($this, 'reply_markup', json_encode(get_values($replyMarkup)));
106+
}
107+
}

0 commit comments

Comments
 (0)