Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Commit

Permalink
Pathao Courier Successfully Added to Provider List
Browse files Browse the repository at this point in the history
  • Loading branch information
arif98741 committed Jan 19, 2022
1 parent 36b1173 commit 537b62d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 34 deletions.
26 changes: 18 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,35 @@ service provider.
use Xenon\MultiCourier\Provider\ECourier;
use Xenon\MultiCourier\Courier;


$courier = Courier::getInstance();
$courier->setProvider(ECourier::class,'production');
$courier->setConfig([
'API-KEY' => 'XXX',
'API-SECRET' => 'XXX',
'USER-ID' => 'XXXX',
]);
$courier->setRequestEndpoint('packages', ['city' => 'Tangail']);
$courier->setProvider(ECourier::class, 'local'); /* local/production */
$courier->setMethod('get');
$courier->setRequestEndpoint('city-list', []); //second param should be array. its optional. you should form params here
$response = $courier->send();
echo $response->getData();
</pre>


#### Sample Code Requesting to Pathao

<pre>
use Xenon\MultiCourier\Courier;
use Xenon\MultiCourier\Provider\Pathao;

$courier = Courier::getInstance();
$courier->setProvider(Pathao::class, 'local'); /* local/production */
$courier->setMethod('get');
$courier->setRequestEndpoint('cities/1/zone-list', []); //second param should be array. its optional. you should form params here
$response = $courier->send();
</pre>




#### Currently Supported Courier Gateways

* ECourier
* Pathao


We are continuously working in this open source library for adding more Bangladeshi courier companies. If you feel something
Expand Down
6 changes: 2 additions & 4 deletions src/Courier.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static function getInstance(): Courier
*/
public function getConfig()
{
//$providerConfiguration = config('courier')['providers'][get_class($this)];
return $this->config;
}

Expand Down Expand Up @@ -161,10 +162,6 @@ public function send()
throw new RequestException("Api endpoint missing for $providerClass");
}

if (!is_array($this->getConfig())) {
throw new ParameterException('config must be an array');
}


// $this->provider->errorException();

Expand All @@ -184,6 +181,7 @@ public function getRequestEndpoint()
}

/**
* This method accept request endpoint
* @param mixed $requestEndpoint
*/
public function setRequestEndpoint($requestEndpoint, array $params = []): void
Expand Down
11 changes: 6 additions & 5 deletions src/Provider/ECourier.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ public function __construct(Courier $courier, string $environment = 'local')
public function sendRequest()
{
$endpoint = $this->senderObject->getRequestEndpoint();
$headerConfig = $this->senderObject->getConfig();
$providerConfiguration = config('courier')['providers'][get_class($this)];
$headerConfig = [
'API-KEY' => $providerConfiguration['API-KEY'],
'API-SECRET' => $providerConfiguration['API-SECRET'],
'USER-ID' => $providerConfiguration['USER-ID'],
];

$courierConfig = config('courier');
if ($courierConfig == null) {
throw new RenderException("No courier.php file exist inside config directory. You should publish vendor Xenon\MultiCourier\MultiCourierServiceProvider");
}

$request = new Request($this->getBaseUrl(), $endpoint, 'post', $headerConfig, $this->senderObject->getParams());
return $request->executeRequest();
Expand Down
96 changes: 81 additions & 15 deletions src/Provider/Pathao.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

namespace Xenon\MultiCourier\Provider;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Storage;
use Xenon\MultiCourier\Courier;
use Xenon\MultiCourier\Handler\ParameterException;
use Xenon\MultiCourier\Handler\RenderException;
Expand All @@ -23,7 +27,7 @@ class Pathao extends AbstractProvider
/**
* @var string
*/
private $base_url = 'http://hermes-api.p-stageenv.xyz/aladdin/api/v1';
private $base_url = 'http://hermes-api.p-stageenv.xyz/aladdin/api/v1/';
/**
* @var mixed|string
*/
Expand All @@ -39,7 +43,7 @@ public function __construct(Courier $sender, string $environment = 'local')
$this->senderObject = $sender;

if ($this->senderObject->environment == 'production') {
$this->setBaseUrl('https://api-hermes.pathaointernal.com/aladdin/api/v1');
$this->setBaseUrl('https://api-hermes.pathaointernal.com/aladdin/api/v1/');
}
}

Expand All @@ -48,6 +52,7 @@ public function __construct(Courier $sender, string $environment = 'local')
* @throws GuzzleException
* @throws RequestException
* @throws RenderException
* @throws Exception
*/
public function sendRequest()
{
Expand All @@ -58,12 +63,57 @@ public function sendRequest()
throw new RenderException("No courier.php file exist inside config directory. You should publish vendor Xenon\MultiCourier\MultiCourierServiceProvider");
}

$providerConfiguration = config('courier')['providers'][get_class($this)];
$this->authorize();
$existance = Storage::disk('local')->exists('pathao_bearer_token.json');
$headers = [];

if ($existance) {
$bearerToken = Storage::get('pathao_bearer_token.json');
$bearerToken = json_decode($bearerToken);
$bearerToken = $bearerToken[0];
$headers = [
'Authorization' => $bearerToken
];

try {
$client = new Client();
$client->get(
$this->base_url . 'cities/1/zone-list',
array(
'verify' => false,
'headers' => $headers
),
);
} catch (ClientException $e) {

if ($e->getCode() == 401) {

$this->generateToken();
$bearerToken = Storage::get('pathao_bearer_token.json');
$bearerToken = json_decode($bearerToken);
$bearerToken = $bearerToken[0];
$headers = [
'Authorization' => $bearerToken
];
} else {
throw new RequestException($e->getMessage());
}
}
} else {
$this->generateToken();
$bearerToken = Storage::get('pathao_bearer_token.json');
$bearerToken = json_decode($bearerToken);
$bearerToken = $bearerToken[0];
$headers = [
'Authorization' => $bearerToken
];
}


$endpointData = $providerConfiguration['endpoints'][$endpoint];
$request = new Request($this->getBaseUrl(), $endpoint, $endpointData['method'], $config, $this->senderObject->getParams());
return $request->executeRequest();
$requestMethod = $this->senderObject->getMethod();
$request = new Request($this->getBaseUrl(), $endpoint, $requestMethod, $headers, $this->senderObject->getParams());
$response = $request->executeRequest();

return $response->getData();
}

/**
Expand Down Expand Up @@ -148,17 +198,16 @@ function getOrders()
*/
public function authorize()
{
//$providerConfiguration = config('courier')['providers'][get_class($this)];
$providerConfiguration = config('courier')['providers'][get_class($this)];
$params = [
'client_id' => env('PATHAO_CLIENT_ID'),
'client_secret' => env('PATHAO_CLIENT_SECRET'),
'username' => env('PATHAO_USERNAME'),
'password' => env('PATHAO_PASSWORD'),
'grant_type' => env('PATHAO_GRANT_TYPE'),
'client_id' => $providerConfiguration['PATHAO_CLIENT_ID'],
'client_secret' => $providerConfiguration['PATHAO_CLIENT_SECRET'],
'username' => $providerConfiguration['PATHAO_USERNAME'],
'password' => $providerConfiguration['PATHAO_PASSWORD'],
'grant_type' => $providerConfiguration['PATHAO_GRANT_TYPE'],
];


$request = new Request($this->getBaseUrl(), '/issue-token', 'post', [], $params);
$request = new Request($this->getBaseUrl(), 'issue-token', 'post', [], $params);
try {
$response = $request->executeRequest();

Expand All @@ -175,4 +224,21 @@ public function authorize()
}

}

/**
* @return void
* @throws RequestException
* @throws Exception
*/
private function generateToken(): void
{
$accessToken = $this->authorize();
$accessTokenArray = ['Bearer' . ' ' . $accessToken];
$accessTokenJson = json_encode($accessTokenArray);
try {
Storage::disk('local')->put('pathao_bearer_token.json', $accessTokenJson);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}
4 changes: 2 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct($base_url, $endpoint, $method, array $headers, $para
* @throws GuzzleException
* @throws RequestException
*/
private function get($requestUrl, array $query = [], bool $verify = false, $timeout = 10.0)
private function get($requestUrl, $query = [], bool $verify = false, $timeout = 10.0)
{
$client = new Client([
'base_uri' => $this->base_url,
Expand Down Expand Up @@ -112,7 +112,7 @@ private function post($requestUrl, array $formParams = null, bool $verify = fals
* @throws GuzzleException
* @throws RequestException
*/
public function executeRequest()
public function executeRequest(): Request
{
$requestUrl = $this->base_url . $this->endpoint;

Expand Down

0 comments on commit 537b62d

Please sign in to comment.