diff --git a/src/APIBase/PathaoBaseAPI.php b/src/APIBase/PathaoBaseAPI.php index 53f47cd..4f740f6 100644 --- a/src/APIBase/PathaoBaseAPI.php +++ b/src/APIBase/PathaoBaseAPI.php @@ -3,8 +3,9 @@ namespace Enan\PathaoCourier\APIBase; - +use ErrorException; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Log; use Illuminate\Http\Response; use Illuminate\Support\Facades\Http; use GuzzleHttp\Exception\ClientException; @@ -14,8 +15,8 @@ class PathaoBaseAPI { - public string $base_url = config('pathao-courier.pathao_base_url'); - // public string $base_url = 'https://api-hermes.pathao.com/'; + // public string $base_url = config('pathao-courier.pathao_base_url'); + public string $base_url = 'https://api-hermes.pathao.com/'; public $secret_token; public $table_name; @@ -44,7 +45,30 @@ protected function setHeaders(bool $auth = false) ]; if ($auth) { - $headers["authorization"] = "Bearer " . $this->pathao_token_data->token; + try { + $headers["authorization"] = "Bearer " . $this->pathao_token_data->token; + } catch (ErrorException $e) { + Log::error($e->getMessage()); + $common_message = "READ CAREFULLY: This error is from enan/pathao-courier package."; + + if (empty(config('pathao-courier.pathao_client_id'))) { + throw new ErrorException($common_message . "Please update your env value with `PATHAO_CLIENT_ID`. + You can find it on the developers api -> Merchant API Credentials section in Pathao Merchant (https://merchant.pathao.com/courier/developer-api). + You Have to enable it from there."); + } else if (empty(config('pathao-courier.pathao_client_secret'))) { + throw new ErrorException($common_message . "Please update your env value with `PATHAO_CLIENT_SECRET`. + You can find it on the developers api -> Merchant API Credentials section in Pathao Merchant (https://merchant.pathao.com/courier/developer-api). + You Have to enable it from there."); + } else if (empty(config('pathao-courier.pathao_secret_token'))) { + throw new ErrorException($common_message . "Please update your env value with `PATHAO_SECRET_TOKEN`. + This value was provided to you while setting up the credentials. + If you miss it you can get it in the database table `" . config('pathao-courier.pathao_db_table_name') . "` column `secret_token`. + Or you can setup a new token by simply running a command `php artisan set:pathao-courier`."); + } else { + throw new ErrorException($common_message . "Please check your env or database. + If the credentials is missing please setup it with running the command `php artisan set:pathao-courier`."); + } + } }; return $headers; diff --git a/src/Commands/PathaoCourierCommand.php b/src/Commands/PathaoCourierCommand.php index 3927ae6..d7eed72 100644 --- a/src/Commands/PathaoCourierCommand.php +++ b/src/Commands/PathaoCourierCommand.php @@ -77,6 +77,7 @@ private function steps() $data = $response->getData(); if ($response->isSuccess()) { $this->successMessage("Your secret uniqe token is " . Arr::get($data, 'secret_token')); + $this->successMessage("Please update your env value `PATHAO_SECRET_TOKEN` with it."); } else { $this->errorMessage(Arr::get($data, 'message')); } diff --git a/src/PathaoCourier.php b/src/PathaoCourier.php index 5c98143..4089f98 100644 --- a/src/PathaoCourier.php +++ b/src/PathaoCourier.php @@ -23,7 +23,7 @@ class PathaoCourier * This will return the remaining days left for access token * And also return the expected last date of the access token expiration * - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function GET_ACCESS_TOKEN_EXPIRY_DAYS_LEFT() { @@ -34,7 +34,7 @@ public function GET_ACCESS_TOKEN_EXPIRY_DAYS_LEFT() * Usage: PathaoCourier::GET_CITIES() * * This will return the city list - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function GET_CITIES() { @@ -46,7 +46,7 @@ public function GET_CITIES() * * This will return the zone list under a city * @param int $city_id - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function GET_ZONES(int $city_id) { @@ -58,7 +58,7 @@ public function GET_ZONES(int $city_id) * * This will return the area list under a zone * @param int $zone_id - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function GET_AREAS(int $zone_id) { @@ -70,7 +70,7 @@ public function GET_AREAS(int $zone_id) * * This will return the store list * @param int $page - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function GET_STORES(int $page = 1) { @@ -92,7 +92,7 @@ public function GET_STORES(int $page = 1) * @param $zone_id * @param $area_id * - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function CREATE_STORE(PathaoStoreRequest $request) { @@ -105,7 +105,7 @@ public function CREATE_STORE(PathaoStoreRequest $request) * * This will fetch the details of a order * @param string $consignment_id - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function VIEW_ORDER(string $consignment_id) { @@ -137,7 +137,7 @@ public function VIEW_ORDER(string $consignment_id) * @param $amount_to_collect * @param $item_description * - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function CREATE_ORDER(PathaoOrderRequest $request) { @@ -158,7 +158,7 @@ public function CREATE_ORDER(PathaoOrderRequest $request) * @param $recipient_city * @param $recipient_zone * @param $store_id - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ public function GET_PRICE_CALCULATION(PathaoOrderPriceCalculationRequest $request) { diff --git a/src/Services/StandardResponseService.php b/src/Services/StandardResponseService.php index 869e440..ca1def7 100644 --- a/src/Services/StandardResponseService.php +++ b/src/Services/StandardResponseService.php @@ -2,21 +2,19 @@ namespace Enan\PathaoCourier\Services; -use Symfony\Component\HttpFoundation\JsonResponse; - class StandardResponseService { /** * This will standardize the data for output * @param mixed $response - * @return \Symfony\Component\HttpFoundation\JsonResponse + * @return array */ - public static function RESPONSE_OUTPUT($response): JsonResponse + public static function RESPONSE_OUTPUT($response): array { - return response()->json([ + return [ 'data' => $response->getData(), 'message' => $response->getMessage(), 'status' => $response->getStatusCode(), - ], $response->getStatusCode()); + ]; } }