diff --git a/examples/send-invoice.php b/examples/send-invoice.php new file mode 100644 index 0000000..f979fd6 --- /dev/null +++ b/examples/send-invoice.php @@ -0,0 +1,28 @@ +chat_id = A_USER_CHAT_ID; +$sendInvoice->title = 'My Special Invoice'; +$sendInvoice->description = 'This is the description of the invoice'; +$sendInvoice->payload = 'specialItem-001'; +$sendInvoice->provider_token = PAYMENT_TOKEN; +$sendInvoice->start_parameter = 'u4u-invoice-0001'; +$sendInvoice->currency = 'EUR'; +$sendInvoice->prices = [ new LabeledPrice(['amount' => 975, 'label' => 'That special item']) ]; + +var_dump($sendInvoice); + +$tgLog = new TgLog(BOT_TOKEN); +/** @var Message $result */ +$result = $tgLog->performApiRequest($sendInvoice); + +var_dump($result); diff --git a/src/Abstracts/TelegramTypes.php b/src/Abstracts/TelegramTypes.php index 1118d26..8656d6f 100644 --- a/src/Abstracts/TelegramTypes.php +++ b/src/Abstracts/TelegramTypes.php @@ -71,11 +71,10 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes 'The key "%s" does not exist in the class! Maybe a recent Telegram Bot API update? In any way, please '. 'submit an issue (bug report) at %s with this complete log line', $key, - 'https://core.telegram.org/bots/api-changelog', 'https://github.com/unreal4u/telegram-api/issues' ), [ 'object' => get_class($this), - 'data' => $data + 'data' => $data, ]); } diff --git a/src/Telegram/Types/PreCheckoutQuery.php b/src/Telegram/Types/PreCheckoutQuery.php index bb53e5f..3cdc184 100644 --- a/src/Telegram/Types/PreCheckoutQuery.php +++ b/src/Telegram/Types/PreCheckoutQuery.php @@ -66,6 +66,8 @@ protected function mapSubObjects(string $key, array $data): TelegramTypes switch ($key) { case 'order_info': return new OrderInfo($data, $this->logger); + case 'from': + return new User($data, $this->logger); } return parent::mapSubObjects($key, $data); diff --git a/tests/Mock/MockData/GetUpdates-preCheckoutQuery.json b/tests/Mock/MockData/GetUpdates-preCheckoutQuery.json new file mode 100644 index 0000000..661ed53 --- /dev/null +++ b/tests/Mock/MockData/GetUpdates-preCheckoutQuery.json @@ -0,0 +1 @@ +{"ok":true,"result":[{"update_id":816722710,"pre_checkout_query":{"id":"47054498177565117","from":{"id":10955729,"first_name":"Camilo","last_name":"Sperberg","username":"unreal4u","language_code":"en-NL"},"currency":"EUR","total_amount":975,"invoice_payload":"specialItem-001"}}]} diff --git a/tests/Mock/MockData/SendInvoice.json b/tests/Mock/MockData/SendInvoice.json new file mode 100644 index 0000000..4cf241b --- /dev/null +++ b/tests/Mock/MockData/SendInvoice.json @@ -0,0 +1 @@ +{"ok":true,"result":{"message_id":2796,"from":{"id":167423524,"first_name":"unreal4uBot","username":"unreal4uBot"},"chat":{"id":10955729,"first_name":"Camilo","last_name":"Sperberg","username":"unreal4u","type":"private"},"date":1497903034,"invoice":{"title":"My Special Invoice","description":"This is the description of the invoice","start_parameter":"u4u-invoice-0001","currency":"EUR","total_amount":975}}} diff --git a/tests/Telegram/Methods/GetUpdatesTest.php b/tests/Telegram/Methods/GetUpdatesTest.php index 2d708c5..f9edc4e 100644 --- a/tests/Telegram/Methods/GetUpdatesTest.php +++ b/tests/Telegram/Methods/GetUpdatesTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use unreal4u\TelegramAPI\Telegram\Types\Custom\UpdatesArray; +use unreal4u\TelegramAPI\Telegram\Types\PreCheckoutQuery; use unreal4u\TelegramAPI\Telegram\Types\Update; use unreal4u\TelegramAPI\Telegram\Types\Message; use unreal4u\TelegramAPI\Telegram\Types\User; @@ -133,4 +134,22 @@ public function testNewChatMemberInUpdate() $this->assertInstanceOf(User::class, $update->message->new_chat_member); } } + + public function testPreCheckoutQuery() + { + $this->tgLog->specificTest = 'preCheckoutQuery'; + + $getUpdates = new GetUpdates(); + + $updatesArray = $this->tgLog->performApiRequest($getUpdates); + /** @var Update $update */ + foreach ($updatesArray->traverseObject() as $update) { + $this->assertInstanceOf(Update::class, $update); + $this->assertInstanceOf(PreCheckoutQuery::class, $update->pre_checkout_query); + $this->assertInstanceOf(User::class, $update->pre_checkout_query->from); + $this->assertNull($update->pre_checkout_query->order_info); + $this->assertSame(975, $update->pre_checkout_query->total_amount); + $this->assertSame('EUR', $update->pre_checkout_query->currency); + } + } } diff --git a/tests/Telegram/Methods/SendInvoiceTest.php b/tests/Telegram/Methods/SendInvoiceTest.php new file mode 100644 index 0000000..e5116e5 --- /dev/null +++ b/tests/Telegram/Methods/SendInvoiceTest.php @@ -0,0 +1,57 @@ +tgLog = new MockTgLog('TEST-TEST'); + } + + /** + * Cleans up the environment after running a test. + */ + protected function tearDown() + { + $this->tgLog = null; + parent::tearDown(); + } + + public function testSendInvoice() + { + $sendInvoice = new SendInvoice(); + $sendInvoice->chat_id = 12341234; + $sendInvoice->title = 'My Special Invoice'; + $sendInvoice->description = 'This is the description of the invoice'; + $sendInvoice->payload = 'specialItem-001'; + $sendInvoice->provider_token = 'TEST-TOKEN-TEST'; + $sendInvoice->start_parameter = 'u4u-invoice-0001'; + $sendInvoice->currency = 'EUR'; + $sendInvoice->prices = [ new LabeledPrice(['amount' => 975, 'label' => 'That special item']) ]; + + /** @var Message $result */ + $result = $this->tgLog->performApiRequest($sendInvoice); + $this->assertInstanceOf(Message::class, $result); + $this->assertInstanceOf(Invoice::class, $result->invoice); + $this->assertSame(975, $result->invoice->total_amount); + $this->assertSame('EUR', $result->invoice->currency); + $this->assertSame('u4u-invoice-0001', $result->invoice->start_parameter); + } +}