Skip to content

Commit

Permalink
Update response and handle ErrorException when env value is empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
enuenan committed May 5, 2024
1 parent 93ec587 commit 10de9e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
32 changes: 28 additions & 4 deletions src/APIBase/PathaoBaseAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Commands/PathaoCourierCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
18 changes: 9 additions & 9 deletions src/PathaoCourier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -92,7 +92,7 @@ public function GET_STORES(int $page = 1)
* @param $zone_id <required, numeric>
* @param $area_id <required, numeric>
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @return array
*/
public function CREATE_STORE(PathaoStoreRequest $request)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -137,7 +137,7 @@ public function VIEW_ORDER(string $consignment_id)
* @param $amount_to_collect <required, numeric>
* @param $item_description <nullable, string>
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @return array
*/
public function CREATE_ORDER(PathaoOrderRequest $request)
{
Expand All @@ -158,7 +158,7 @@ public function CREATE_ORDER(PathaoOrderRequest $request)
* @param $recipient_city <required, numeric>
* @param $recipient_zone <required, numeric>
* @param $store_id <required, numeric>
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @return array
*/
public function GET_PRICE_CALCULATION(PathaoOrderPriceCalculationRequest $request)
{
Expand Down
10 changes: 4 additions & 6 deletions src/Services/StandardResponseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
];
}
}

0 comments on commit 10de9e0

Please sign in to comment.