Skip to content

Commit a388d2b

Browse files
authored
Merge pull request #40 from noplanman/payment_command
Add example for Telegram Payments
2 parents a92474e + ca8e244 commit a388d2b

7 files changed

+356
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Bot example-bot package.
5+
* https://github.com/php-telegram-bot/example-bot/
6+
*
7+
* (c) PHP Telegram Bot Developers
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
/**
14+
* Generic message command
15+
*
16+
* Gets executed when any type of message is sent.
17+
*/
18+
19+
namespace Longman\TelegramBot\Commands\SystemCommands;
20+
21+
use Longman\TelegramBot\Commands\SystemCommand;
22+
use Longman\TelegramBot\Request;
23+
24+
class GenericmessageCommand extends SystemCommand
25+
{
26+
/**
27+
* @var string
28+
*/
29+
protected $name = 'genericmessage';
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $description = 'Handle generic message';
35+
36+
/**
37+
* @var string
38+
*/
39+
protected $version = '0.1.0';
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function execute()
45+
{
46+
$message = $this->getMessage();
47+
$user_id = $message->getFrom()->getId();
48+
49+
// Handle successful payment.
50+
if ($payment = $message->getSuccessfulPayment()) {
51+
return PaymentCommand::handleSuccessfulPayment($payment, $user_id);
52+
}
53+
54+
return Request::emptyResponse();
55+
}
56+
}

Commands/Payments/PaymentCommand.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Bot example-bot package.
5+
* https://github.com/php-telegram-bot/example-bot/
6+
*
7+
* (c) PHP Telegram Bot Developers
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
/**
14+
* User "/payment" command
15+
*
16+
* This command creates an invoice for the user using the Telegram Payments.
17+
*
18+
* You will have to set up a payment provider with @BotFather
19+
* Select your bot and then "Payments". Then choose the provider of your choice.
20+
*
21+
* @BotFather will then present you with a payment provider token.
22+
* Copy this token and add it to your command config in your hook file:
23+
*
24+
* $telegram->setCommandConfig('payment', ['payment_provider_token' => 'your_payment_provider_token_here']);
25+
*
26+
* You will also need to copy the `Precheckoutquerycommand.php` file.
27+
*/
28+
29+
namespace Longman\TelegramBot\Commands\UserCommands;
30+
31+
use Longman\TelegramBot\Commands\UserCommand;
32+
use Longman\TelegramBot\Entities\Payments\LabeledPrice;
33+
use Longman\TelegramBot\Entities\Payments\SuccessfulPayment;
34+
use Longman\TelegramBot\Entities\ServerResponse;
35+
use Longman\TelegramBot\Exception\TelegramException;
36+
use Longman\TelegramBot\Request;
37+
38+
class PaymentCommand extends UserCommand
39+
{
40+
/**
41+
* @var string
42+
*/
43+
protected $name = 'payment';
44+
45+
/**
46+
* @var string
47+
*/
48+
protected $description = 'Create an invoice for the user using Telegram Payments';
49+
50+
/**
51+
* @var string
52+
*/
53+
protected $usage = '/payment';
54+
55+
/**
56+
* @var string
57+
*/
58+
protected $version = '0.1.0';
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function execute()
64+
{
65+
// Who to send this invoice to. (Use the current user.)
66+
$chat_id = $this->getMessage()->getFrom()->getId();
67+
68+
// The currency of this invoice.
69+
// Supported currencies: https://core.telegram.org/bots/payments#supported-currencies
70+
$currency = 'EUR';
71+
72+
// List all items that will be shown on your invoice.
73+
// Amounts are in cents. So 1 Euro would be put as 100.
74+
$prices = [
75+
new LabeledPrice(['label' => 'Small thing', 'amount' => 100]), // 1€
76+
new LabeledPrice(['label' => 'Bigger thing', 'amount' => 2000]), // 20€
77+
new LabeledPrice(['label' => 'Huge thing', 'amount' => 50000]), // 500€
78+
];
79+
80+
// Request a shipping address if necessary.
81+
$need_shipping_address = false;
82+
83+
// If you have flexible pricing, depending on the shipping method chosen, set this to true.
84+
// You will also require the `ShippingqueryCommand.php` file and adapt it.
85+
$is_flexible = false;
86+
87+
// Send the actual invoice!
88+
// Adjust any parameters to your needs.
89+
return Request::sendInvoice([
90+
'chat_id' => $chat_id,
91+
'title' => 'Payment with PHP Telegram Bot',
92+
'description' => 'A simple invoice to test Telegram Payments',
93+
'payload' => 'payment_demo',
94+
'start_parameter' => 'payment_demo',
95+
'provider_token' => $this->getConfig('payment_provider_token'),
96+
'currency' => $currency,
97+
'prices' => $prices,
98+
'need_shipping_address' => $need_shipping_address,
99+
'is_flexible' => $is_flexible,
100+
]);
101+
}
102+
103+
/**
104+
* Send "Thank you" message to user who paid.
105+
*
106+
* You will need to add some code to your custom `GenericmessageCommand::execute()` method.
107+
* Check the `GenericmessageCommand.php` file included in this folder.
108+
*
109+
* @param SuccessfulPayment $payment
110+
* @param int $user_id
111+
*
112+
* @return ServerResponse
113+
* @throws TelegramException
114+
*/
115+
public static function handleSuccessfulPayment($payment, $user_id)
116+
{
117+
// Send a message to the user after they have completed the payment.
118+
return Request::sendMessage([
119+
'chat_id' => $user_id,
120+
'text' => 'Thank you for your order!',
121+
]);
122+
}
123+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Bot example-bot package.
5+
* https://github.com/php-telegram-bot/example-bot/
6+
*
7+
* (c) PHP Telegram Bot Developers
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
/**
14+
* Pre-checkout query required for "/payment" command.
15+
*
16+
* In this command you can perform any necessary verifications and checks
17+
* to allow or disallow the final checkout and payment of the invoice.
18+
*/
19+
20+
namespace Longman\TelegramBot\Commands\SystemCommands;
21+
22+
use Longman\TelegramBot\Commands\SystemCommand;
23+
24+
class PrecheckoutqueryCommand extends SystemCommand
25+
{
26+
/**
27+
* @var string
28+
*/
29+
protected $name = 'precheckoutquery';
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $description = 'Pre-Checkout Query Handler';
35+
36+
/**
37+
* @var string
38+
*/
39+
protected $version = '0.1.0';
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function execute()
45+
{
46+
// Simply approve, no need for any checks at this point.
47+
return $this->getPreCheckoutQuery()->answer(true);
48+
49+
// If we do make certain checks, you can define the error message displayed to the user like this.
50+
// return $this->getPreCheckoutQuery()->answer(false, [
51+
// 'error_message' => 'Registration (or whatever) required...',
52+
// ]);
53+
}
54+
}

Commands/Payments/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Telegram Payments
2+
3+
With the files in this folder, you can create and send invoices to your users, require their shipping details, define custom / flexible shipping methods and send a confirmation message after payment.
4+
5+
## Enable payments for your bot
6+
7+
Read all about Telegram Payments and follow the setup guide here:
8+
https://core.telegram.org/bots/payments
9+
10+
## Configuring the `/payment` command
11+
12+
First of all, as a bare minimum, you need to copy the `PaymentCommand.php` and `PrecheckoutqueryCommand.php` files in this folder to your custom commands folder.
13+
14+
If you want to allow flexible shipping options, you will also need to copy `ShippingqueryCommand.php` to your custom commands folder.
15+
16+
Should you want to send a message on a successful payment, you will need to copy the `GenericmessageCommand.php` file as well.
17+
If you already have a `GenericmessageCommand.php` file, you'll need to copy the code from the `execute` method into your file.
18+
19+
Next, you will need to add the Payment Provider Token (that you received in the previous step when linking your bot), to your `hook.php` or `manager.php` config.
20+
21+
For `hook.php`:
22+
```php
23+
$telegram->setCommandConfig('payment', ['payment_provider_token' => 'your_payment_provider_token_here']);
24+
```
25+
26+
For `manager.php`, in the config array add:
27+
```php
28+
...
29+
'commands' => [
30+
'configs' => [
31+
'payment' => ['payment_provider_token' => 'your_payment_provider_token_here'],
32+
],
33+
],
34+
...
35+
```
36+
37+
Now, when sending the `/payment` command to your bot, you should receive an invoice.
38+
39+
Have fun with Telegram Payments!
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Bot example-bot package.
5+
* https://github.com/php-telegram-bot/example-bot/
6+
*
7+
* (c) PHP Telegram Bot Developers
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
/**
14+
* Shipping query required for "/payment" command with flexible shipping method.
15+
*
16+
* In this command, you can perform any necessary verifications and checks
17+
* to adjust the available shipping options of the payment.
18+
*
19+
* For example, if the user has a "Free Delivery" subscription or something like that.
20+
*/
21+
22+
namespace Longman\TelegramBot\Commands\SystemCommands;
23+
24+
use Longman\TelegramBot\Commands\SystemCommand;
25+
use Longman\TelegramBot\Entities\Payments\LabeledPrice;
26+
use Longman\TelegramBot\Entities\Payments\ShippingOption;
27+
28+
class ShippingqueryCommand extends SystemCommand
29+
{
30+
/**
31+
* @var string
32+
*/
33+
protected $name = 'shippingquery';
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $description = 'Shipping Query Handler';
39+
40+
/**
41+
* @var string
42+
*/
43+
protected $version = '0.1.0';
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function execute()
49+
{
50+
// Here you can check the shipping details and adjust the Shipping Options accordingly.
51+
// For this demo, let's simply define some fixed shipping options, a "Basic" and "Premium" shipping method.
52+
return $this->getShippingQuery()->answer(true, [
53+
'shipping_options' => [
54+
new ShippingOption([
55+
'id' => 'basic',
56+
'title' => 'Basic Shipping',
57+
'prices' => [
58+
new LabeledPrice(['label' => 'Basic Shipping', 'amount' => 800]),
59+
],
60+
]),
61+
new ShippingOption([
62+
'id' => 'premium',
63+
'title' => 'Premium Shipping',
64+
'prices' => [
65+
new LabeledPrice(['label' => 'Premium Shipping', 'amount' => 1500]),
66+
new LabeledPrice(['label' => 'Extra speedy', 'amount' => 300]),
67+
],
68+
]),
69+
],
70+
]);
71+
72+
// If we do make certain checks, you can define the error message displayed to the user like this.
73+
// return $this->getShippingQuery()->answer(false, [
74+
// 'error_message' => 'We do not ship to your location :-(',
75+
// ]);
76+
}
77+
}

hook.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@
6565
//$telegram->setUploadPath(__DIR__ . '/Upload');
6666

6767
// Here you can set some command specific parameters
68-
// e.g. Google geocode/timezone api key for /date command
69-
//$telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);
68+
// - Google geocode/timezone API key for /date command
69+
// $telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);
70+
// - Payment Provider Token for /payment command.
71+
// $telegram->setCommandConfig('payment', ['payment_provider_token' => 'your_payment_provider_token_here']);
7072

7173
// Requests Limiter (tries to prevent reaching Telegram API limits)
7274
$telegram->enableLimiter();

manager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
// ],
4040
// // Here you can set some command specific parameters
4141
// 'configs' => [
42-
// // e.g. Google geocode/timezone api key for /date command
42+
// - Google geocode/timezone API key for /date command
4343
// 'date' => ['google_api_key' => 'your_google_api_key_here'],
44+
// - Payment Provider Token for /payment command.
45+
// 'payment' => ['payment_provider_token' => 'your_payment_provider_token_here'],
4446
// ],
4547
//],
4648

0 commit comments

Comments
 (0)