diff --git a/module/oc_telegram_shop/upload/catalog/controller/extension/tgshop/handle.php b/module/oc_telegram_shop/upload/catalog/controller/extension/tgshop/handle.php index a0f2674..b6e88d3 100755 --- a/module/oc_telegram_shop/upload/catalog/controller/extension/tgshop/handle.php +++ b/module/oc_telegram_shop/upload/catalog/controller/extension/tgshop/handle.php @@ -38,6 +38,10 @@ class Controllerextensiontgshophandle extends Controller 'language_id' => (int) $this->config->get('config_language_id'), 'shop_base_url' => HTTPS_SERVER, 'dir_image' => DIR_IMAGE, + 'telegram' => [ + 'bot_token' => '7513204587:AAGvRL15OzzltESqwbL15vOEWi6ZZMikDpg', + 'chat_id' => 849193407, + ], 'db' => [ 'host' => DB_HOSTNAME, 'database' => DB_DATABASE, diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/composer.json b/module/oc_telegram_shop/upload/oc_telegram_shop/composer.json index c664a69..0b1b122 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/composer.json +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/composer.json @@ -22,7 +22,8 @@ "ext-json": "*", "intervention/image": "^2.7", "rakit/validation": "^1.4", - "vlucas/phpdotenv": "^5.6" + "vlucas/phpdotenv": "^5.6", + "guzzlehttp/guzzle": "^7.9" }, "require-dev": { "roave/security-advisories": "dev-latest" diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/composer.lock b/module/oc_telegram_shop/upload/oc_telegram_shop/composer.lock index c2b22ea..6f3ca39 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/composer.lock +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "39c53e4dd9f5fa4822c5c5c87b882768", + "content-hash": "7e1f9e747364eeaf0ebae0f7af3f8d2c", "packages": [ { "name": "graham-campbell/result-type", @@ -68,6 +68,215 @@ ], "time": "2024-07-20T21:45:45+00:00" }, + { + "name": "guzzlehttp/guzzle", + "version": "7.9.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "7b2f29fe81dc4da0ca0ea7d42107a0845946ea77" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/7b2f29fe81dc4da0ca0ea7d42107a0845946ea77", + "reference": "7b2f29fe81dc4da0ca0ea7d42107a0845946ea77", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/promises": "^1.5.3 || ^2.0.3", + "guzzlehttp/psr7": "^2.7.0", + "php": "^7.2.5 || ^8.0", + "psr/http-client": "^1.0", + "symfony/deprecation-contracts": "^2.2 || ^3.0" + }, + "provide": { + "psr/http-client-implementation": "1.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.2", + "ext-curl": "*", + "guzzle/client-integration-tests": "3.0.2", + "php-http/message-factory": "^1.1", + "phpunit/phpunit": "^8.5.39 || ^9.6.20", + "psr/log": "^1.1 || ^2.0 || ^3.0" + }, + "suggest": { + "ext-curl": "Required for CURL handler support", + "ext-intl": "Required for Internationalized Domain Name (IDN) support", + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Jeremy Lindblom", + "email": "jeremeamia@gmail.com", + "homepage": "https://github.com/jeremeamia" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "psr-18", + "psr-7", + "rest", + "web service" + ], + "support": { + "issues": "https://github.com/guzzle/guzzle/issues", + "source": "https://github.com/guzzle/guzzle/tree/7.9.3" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle", + "type": "tidelift" + } + ], + "time": "2025-03-27T13:37:11+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "7c69f28996b0a6920945dd20b3857e499d9ca96c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/7c69f28996b0a6920945dd20b3857e499d9ca96c", + "reference": "7c69f28996b0a6920945dd20b3857e499d9ca96c", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.2", + "phpunit/phpunit": "^8.5.39 || ^9.6.20" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "support": { + "issues": "https://github.com/guzzle/promises/issues", + "source": "https://github.com/guzzle/promises/tree/2.2.0" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", + "type": "tidelift" + } + ], + "time": "2025-03-27T13:27:01+00:00" + }, { "name": "guzzlehttp/psr7", "version": "2.7.1", @@ -396,6 +605,58 @@ }, "time": "2021-11-05T16:47:00+00:00" }, + { + "name": "psr/http-client", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-client.git", + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-client/zipball/bb5906edc1c324c9a05aa0873d40117941e5fa90", + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0", + "psr/http-message": "^1.0 || ^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP clients", + "homepage": "https://github.com/php-fig/http-client", + "keywords": [ + "http", + "http-client", + "psr", + "psr-18" + ], + "support": { + "source": "https://github.com/php-fig/http-client" + }, + "time": "2023-09-23T14:17:50+00:00" + }, { "name": "psr/http-factory", "version": "1.1.0", @@ -594,6 +855,73 @@ }, "time": "2019-03-08T08:55:37+00:00" }, + { + "name": "symfony/deprecation-contracts", + "version": "v2.5.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "605389f2a7e5625f273b53960dc46aeaf9c62918" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/605389f2a7e5625f273b53960dc46aeaf9c62918", + "reference": "605389f2a7e5625f273b53960dc46aeaf9c62918", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/contracts", + "name": "symfony/contracts" + }, + "branch-alias": { + "dev-main": "2.5-dev" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.4" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-25T14:11:13+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.32.0", diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Telegram/TelegramService.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Telegram/TelegramService.php new file mode 100644 index 0000000..73c752d --- /dev/null +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Telegram/TelegramService.php @@ -0,0 +1,47 @@ +logger = $logger; + $this->client = $this->createGuzzleClient("https://api.telegram.org/bot{$botToken}/"); + } + + public function sendMessage(int $chatId, string $text): bool + { + try { + $query = [ + 'chat_id' => $chatId, + 'text' => $text, + 'parse_mode' => 'MarkdownV2', + ]; + + $this->client->get('sendMessage', [ + 'query' => $query, + ]); + return true; + } catch (Exception $exception) { + $this->logger->error('Telegram sendMessage error: ' . json_encode($query)); + $this->logger->logException($exception); + return false; + } + } + + private function createGuzzleClient(string $uri): Client + { + return new Client([ + 'base_uri' => $uri, + 'timeout' => 5.0, + ]); + } +} diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Telegram/TelegramServiceProvider.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Telegram/TelegramServiceProvider.php new file mode 100644 index 0000000..3a15d69 --- /dev/null +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Telegram/TelegramServiceProvider.php @@ -0,0 +1,21 @@ +container->singleton(TelegramService::class, function (Application $app) { + $botToken = $app->getConfigValue('telegram.bot_token'); + return new TelegramService( + $botToken, + $app->get(Logger::class), + ); + }); + } +} diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/ApplicationFactory.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/ApplicationFactory.php index 6aeeb25..c88bf6a 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/ApplicationFactory.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/ApplicationFactory.php @@ -8,6 +8,7 @@ use Openguru\OpenCartFramework\Cache\CacheServiceProvider; use Openguru\OpenCartFramework\QueryBuilder\QueryBuilderServiceProvider; use Openguru\OpenCartFramework\Router\RouteServiceProvider; use Openguru\OpenCartFramework\Support\Arr; +use Openguru\OpenCartFramework\Telegram\TelegramServiceProvider; class ApplicationFactory { @@ -21,6 +22,7 @@ class ApplicationFactory CacheServiceProvider::class, RouteServiceProvider::class, AppServiceProvider::class, + TelegramServiceProvider::class, ]); } } diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/OrderCreateService.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/OrderCreateService.php index 650b8d8..5b5848b 100644 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/OrderCreateService.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/OrderCreateService.php @@ -5,9 +5,8 @@ namespace App\Services; use App\Decorators\OcRegistryDecorator; use App\Exceptions\OrderValidationFailedException; use Openguru\OpenCartFramework\Config\Settings; -use Openguru\OpenCartFramework\Http\JsonResponse; -use Openguru\OpenCartFramework\Http\Response; use Openguru\OpenCartFramework\QueryBuilder\Connections\ConnectionInterface; +use Openguru\OpenCartFramework\Telegram\TelegramService; use Rakit\Validation\Validator; use RuntimeException; @@ -17,20 +16,23 @@ class OrderCreateService private CartService $cartService; private OcRegistryDecorator $oc; private Settings $settings; + private TelegramService $telegramService; public function __construct( ConnectionInterface $database, CartService $cartService, OcRegistryDecorator $registry, - Settings $settings + Settings $settings, + TelegramService $telegramService ) { $this->database = $database; $this->cartService = $cartService; $this->oc = $registry; $this->settings = $settings; + $this->telegramService = $telegramService; } - public function create(array $data, array $meta = []) + public function create(array $data, array $meta = []): void { $this->validate($data); @@ -153,6 +155,23 @@ class OrderCreateService ); $this->cartService->flush(); + + $message = <<telegramService->sendMessage( + $this->settings->get('telegram.chat_id'), + sprintf( + $message, + $storeName, + $data['firstName'], + $data['lastName'], + $total, + ), + ); } private function validate(array $data): void