You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.1 KiB

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