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.

80 lines
2.5 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. * Shipping query required for "/payment" command with flexible shipping method.
  13. *
  14. * In this command, you can perform any necessary verifications and checks
  15. * to adjust the available shipping options of the payment.
  16. *
  17. * For example, if the user has a "Free Delivery" subscription or something like that.
  18. */
  19. namespace Longman\TelegramBot\Commands\SystemCommands;
  20. use Longman\TelegramBot\Commands\SystemCommand;
  21. use Longman\TelegramBot\Entities\Payments\LabeledPrice;
  22. use Longman\TelegramBot\Entities\Payments\ShippingOption;
  23. use Longman\TelegramBot\Entities\ServerResponse;
  24. class ShippingqueryCommand extends SystemCommand
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $name = 'shippingquery';
  30. /**
  31. * @var string
  32. */
  33. protected $description = 'Shipping Query Handler';
  34. /**
  35. * @var string
  36. */
  37. protected $version = '0.1.0';
  38. /**
  39. * Main command execution
  40. *
  41. * @return ServerResponse
  42. */
  43. public function execute(): ServerResponse
  44. {
  45. // Here you can check the shipping details and adjust the Shipping Options accordingly.
  46. // For this demo, let's simply define some fixed shipping options, a "Basic" and "Premium" shipping method.
  47. return $this->getShippingQuery()->answer(true, [
  48. 'shipping_options' => [
  49. new ShippingOption([
  50. 'id' => 'basic',
  51. 'title' => 'Basic Shipping',
  52. 'prices' => [
  53. new LabeledPrice(['label' => 'Basic Shipping', 'amount' => 800]),
  54. ],
  55. ]),
  56. new ShippingOption([
  57. 'id' => 'premium',
  58. 'title' => 'Premium Shipping',
  59. 'prices' => [
  60. new LabeledPrice(['label' => 'Premium Shipping', 'amount' => 1500]),
  61. new LabeledPrice(['label' => 'Extra speedy', 'amount' => 300]),
  62. ],
  63. ]),
  64. ],
  65. ]);
  66. // If we do make certain checks, you can define the error message displayed to the user like this.
  67. // return $this->getShippingQuery()->answer(false, [
  68. // 'error_message' => 'We do not ship to your location :-(',
  69. // ]);
  70. }
  71. }