Skip to content

Commit

Permalink
Documenting the Telegram payments, HTTP Error codes on authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Muaath5 committed Oct 7, 2021
1 parent 72cc30d commit 0a50044
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ You can see a full example [here](https://muaath5.github.io/SimpleBotAPI/FullExa
- [x] Add webhook authorization
- [ ] Auto handle for `callback_query` & `inline_query`
- [ ] Add JSON Storage can be used by bot
- [ ] Add DB Storage can be used by bot
- [ ] Auto-store for bot users
- [ ] Method that posts to all bot users
- [ ] Add DB Storage can be used by bot

## License
GPL-3.0, In LICENCE file.
15 changes: 14 additions & 1 deletion docs/TelegramPayments.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Telegram Payments
Coming soon..
Bots can receive payments via bots.

## How can I receive money?
You should set a payment provider in @BotFather, Then you'll need to fill your card info.
Then you'll receive money there.

## What updates should I use?
You can say that processing payments run in these steps:
1. Your bot send an invoice (Type of messages contains _Pay_ button and what you'll sell)
2. Bot receives shipping info (_Optional_, Your bot may not request the shipping info)
- If shipping info was incorrect or can't delivery, You can not accept it and let user retype the info
3. Your bot show what delivery options avaliable
4. Your bot receive update `pre_checkout_query`, Your bot should check if all info is good & If item still exists in the shop
5. Bot will receive the receipt!

## Example
There is a test payment bot in [Muaath5/MuaathBots](https://github.com/Muaath5/MuaathBots/bots/src/TestPaymentBot.php)
14 changes: 12 additions & 2 deletions src/TelegramBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ public function SaveSettings()
public function OnWebhookUpdate() : bool
{
$Update = json_decode(file_get_contents('php://input'));
if (empty($Update)) return false;
if (empty($Update))
{
http_response_code(400);
return false;
}

if ($this->Settings->AutoHandleDuplicateUpdates)
{
Expand All @@ -147,6 +151,7 @@ public function OnWebhookUpdate() : bool
if ($this->Settings->LastUpdateID >= $Update->update_id)
{
// This update is fake or duplicate by ID
http_response_code(400);
return false;
}
}
Expand All @@ -160,6 +165,7 @@ public function OnWebhookUpdate() : bool
if ($_GET['token_hash'] != hash($this->HashingMethod, $this->Token))
{
# Fake update
http_response_code(401);
return false;
}
}
Expand Down Expand Up @@ -218,6 +224,9 @@ public function WebhookResponse(string $method, array $params = []) : void
$payload = json_encode($params);
header('Content-Type: application/json');
header('Content-Length: ' . strlen($payload));

http_response_code(200);

echo $payload;
}

Expand All @@ -226,7 +235,8 @@ public function SetBotWebhook(string $host, int $max_connections = 40, bool $aut
$this->Settings->CheckUpdates = $auth;
return $this->SetWebhook([
'url' => $host . ($auth ? '?token_hash=' . hash($this->HashingMethod, $this->Token) : ''),
'max_connections' => $max_connections
'max_connections' => $max_connections,
'allowed_updates' => json_encode($this->Settings->AllowedUpdates)
]);
}

Expand Down

0 comments on commit 0a50044

Please sign in to comment.