From 2723685d8c2618f08e2de7f3131071d923c8c964 Mon Sep 17 00:00:00 2001 From: altynbek07 Date: Sun, 19 Jul 2020 20:27:42 +0500 Subject: [PATCH] feat: uds base methods --- README.md | 16 ++++ composer.json | 2 +- config/uds.php | 14 +++ src/Commands/UdsCommand.php | 17 ---- src/Facades/Uds.php | 23 +++++ src/Http/Controllers/UdsController.php | 82 ++++++++++++++++ src/Http/routes.php | 12 +++ src/Uds.php | 125 +++++++++++++++++++++++++ src/UdsFacade.php | 16 ---- src/UdsServiceProvider.php | 48 +++++++++- 10 files changed, 316 insertions(+), 39 deletions(-) delete mode 100644 src/Commands/UdsCommand.php create mode 100644 src/Facades/Uds.php create mode 100644 src/Http/Controllers/UdsController.php create mode 100644 src/Http/routes.php delete mode 100644 src/UdsFacade.php diff --git a/README.md b/README.md index 05d1724..41c3103 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ php artisan vendor:publish --provider="Altynbek07\Uds\UdsServiceProvider" --tag= This is the contents of the published config file: ```php + env('UDS_KEY'), + /** + * This is the API URI path where Uds will be accessible from. Feel free + * to change this path to anything you like. + */ + 'path' => env('UDS_PATH', 'api/uds'), + /** + * These middleware will be assigned to every Uds route, giving you + * the chance to add your own middleware to this list or change any of + * the existing middleware. Or, you can simply stick with this list. + */ + 'middleware' => [ + 'api', + // 'auth:api' + ], ]; ``` diff --git a/composer.json b/composer.json index e76de15..263cfba 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,7 @@ "Altynbek07\\Uds\\UdsServiceProvider" ], "aliases": { - "Ud": "Altynbek07\\Uds\\UdsFacade" + "Uds": "Altynbek07\\Uds\\Facades\\Uds" } } }, diff --git a/config/uds.php b/config/uds.php index 0fc749c..261d110 100644 --- a/config/uds.php +++ b/config/uds.php @@ -9,4 +9,18 @@ * Your API Key from UDS */ 'key' => env('UDS_KEY'), + /** + * This is the API URI path where Uds will be accessible from. Feel free + * to change this path to anything you like. + */ + 'path' => env('UDS_PATH', 'api/uds'), + /** + * These middleware will be assigned to every Uds route, giving you + * the chance to add your own middleware to this list or change any of + * the existing middleware. Or, you can simply stick with this list. + */ + 'middleware' => [ + 'api', + // 'auth:api' + ], ]; diff --git a/src/Commands/UdsCommand.php b/src/Commands/UdsCommand.php deleted file mode 100644 index d420c58..0000000 --- a/src/Commands/UdsCommand.php +++ /dev/null @@ -1,17 +0,0 @@ -comment('All done'); - } -} diff --git a/src/Facades/Uds.php b/src/Facades/Uds.php new file mode 100644 index 0000000..4285c32 --- /dev/null +++ b/src/Facades/Uds.php @@ -0,0 +1,23 @@ +query(), true); + } + + /** + * Получение настроек компании + * @see https://docs.uds.app/#tag/Settings/paths/~1settings/get + * + * @return \Illuminate\Http\JsonResponse + */ + public function settings(): JsonResponse + { + return Uds::settings(true); + } + + /** + * Проведение операции + * @see https://docs.uds.app/#tag/Operations/paths/~1operations/post + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\JsonResponse + */ + public function createTransaction(Request $request): JsonResponse + { + return Uds::createTransaction($request->all(), true); + } + + /** + * Операция возврата + * @see https://docs.uds.app/#tag/Operations/paths/~1operations~1{id}~1refund/post + * + * @param @param int $param + * @return \Illuminate\Http\JsonResponse + */ + public function refundTransaction(int $id): JsonResponse + { + return Uds::refundTransaction($id, true); + } + + /** + * Получить информацию об операции + * @see https://docs.uds.app/#tag/Operations/paths/~1operations~1calc/post + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\JsonResponse + */ + public function getTransactionInformation(Request $request): JsonResponse + { + return Uds::getTransactionInformation($request->all(), true); + } +} diff --git a/src/Http/routes.php b/src/Http/routes.php new file mode 100644 index 0000000..648e44e --- /dev/null +++ b/src/Http/routes.php @@ -0,0 +1,12 @@ +getHttpClient()->get($this->baseUrl . '/customers/' . $id); + + return $this->handleResponse($response, $asJson); + } + + /** + * Поиск клиента + * @see https://docs.uds.app/#tag/Customers/paths/~1customers~1find/get + * + * @param array $requestData + * @param bool $asJson + * @return \Illuminate\Http\JsonResponse|array + */ + public function customersFind(array $requestData, bool $asJson = false) + { + $response = $this->getHttpClient()->get($this->baseUrl . '/customers/find', $requestData); + + return $this->handleResponse($response, $asJson); + } + + /** + * Получение настроек компании + * @see https://docs.uds.app/#tag/Settings/paths/~1settings/get + * + * @param bool $asJson + * @return \Illuminate\Http\JsonResponse|array + */ + public function settings(bool $asJson = false) + { + $response = $this->getHttpClient()->get($this->baseUrl . '/settings'); + + return $this->handleResponse($response, $asJson); + } + + /** + * Проведение операции + * @see https://docs.uds.app/#tag/Operations/paths/~1operations/post + * + * @param array $requestData + * @param bool $asJson + * @return \Illuminate\Http\JsonResponse|array + */ + public function createTransaction(array $requestData, bool $asJson = false) + { + $response = $this->getHttpClient()->post($this->baseUrl . '/operations', $requestData); + + return $this->handleResponse($response, $asJson); + } + + /** + * Операция возврата + * @see https://docs.uds.app/#tag/Operations/paths/~1operations~1{id}~1refund/post + * + * @param int $id + * @param bool $asJson + * @return \Illuminate\Http\JsonResponse|array + */ + public function refundTransaction(int $id, bool $asJson = false) + { + $response = $this->getHttpClient()->post($this->baseUrl . '/operations/' . $id . '/refund'); + + return $this->handleResponse($response, $asJson); + } + + /** + * Получить информацию об операции + * @see https://docs.uds.app/#tag/Operations/paths/~1operations~1calc/post + * + * @param array $requestData + * @param bool $asJson + * @return \Illuminate\Http\JsonResponse|array + */ + public function getTransactionInformation(array $requestData, bool $asJson = false) + { + $response = $this->getHttpClient()->post($this->baseUrl . '/operations/calc', $requestData); + + return $this->handleResponse($response, $asJson); + } + + /** + * HTTP Client + * + * @return PendingRequest + */ + protected function getHttpClient(): PendingRequest + { + return Http::withBasicAuth(config('uds.id'), config('uds.key'))->acceptJson(); + } + + /** + * Handling response + * + * @param \Illuminate\Http\Client\Response $response + * @param bool $asJson + * @return \Illuminate\Http\JsonResponse|array + */ + protected function handleResponse(Response $response, bool $asJson) + { + try { + $response->throw(); + } catch (RequestException $error) { + return $asJson ? response()->json($error->response->json(), $error->response->status()) : $error->response->json(); + } + + return $asJson ? response()->json($response->json(), $response->status()) : $response->json(); + } } diff --git a/src/UdsFacade.php b/src/UdsFacade.php deleted file mode 100644 index 8f6704e..0000000 --- a/src/UdsFacade.php +++ /dev/null @@ -1,16 +0,0 @@ -app->runningInConsole()) { $this->publishes([ __DIR__ . '/../config/uds.php' => config_path('uds.php'), ], 'config'); - - $this->commands([ - UdsCommand::class, - ]); } + + Route::middlewareGroup('uds', config('uds.middleware', [])); + + $this->registerRoutes(); } + /** + * Register any package services. + * + * @return void + */ public function register() { $this->mergeConfigFrom(__DIR__ . '/../config/uds.php', 'uds'); + + $this->app->alias(Uds::class, 'laravel-uds'); + } + + /** + * Register the package routes. + * + * @return void + */ + private function registerRoutes() + { + Route::group($this->routeConfiguration(), function () { + $this->loadRoutesFrom(__DIR__ . '/Http/routes.php'); + }); + } + + /** + * Get the Uds route group configuration array. + * + * @return array + */ + private function routeConfiguration() + { + return [ + 'namespace' => 'Altynbek07\Uds\Http\Controllers', + 'prefix' => config('uds.path'), + 'middleware' => 'uds', + ]; } }