Skip to content

Commit

Permalink
Initial ecourier package
Browse files Browse the repository at this point in the history
  • Loading branch information
dipudey committed Oct 8, 2022
0 parents commit ba792fe
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "codeboxr/ecourier-courier",
"description": "Bangladeshi ecourier service api package",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Codeboxr\\EcourierCourier\\": "src/"
}
},
"authors": [
{
"name": "Codeboxr"
}
],
"minimum-stability": "dev",
"require": {
"php": "^7.2|^7.3|^8.0|^8.1",
"illuminate/support": "~6|~7|~8|~9",
"guzzlehttp/guzzle": "^7.0.1"
},
"extra": {
"laravel": {
"providers": [
"Codeboxr\\EcourierCourier\\EcourierServiceProvider"
]
}
}
}
8 changes: 8 additions & 0 deletions config/ecourier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
"sandbox" => env("ECOURIER_SANDBOX", false),
"app_key" => env("ECOURIER_API_KEY", ""),
"app_secret" => env("ECOURIER_API_SECRET", ""),
"user_id" => env("ECOURIER_USER_ID", "")
];
84 changes: 84 additions & 0 deletions src/Apis/AreaApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Codeboxr\EcourierCourier\Apis;

use GuzzleHttp\Exception\GuzzleException;
use Codeboxr\EcourierCourier\Exceptions\EcourierException;

class AreaApi extends BaseApi
{
/**
* get city List
*
* @return mixed
* @throws GuzzleException
* @throws EcourierException
*/
public function city()
{
$response = $this->authorization()->send("POST", "/api/city-list");
return $response;
}

/**
* get thana List
*
* @param string $cityName
*
* @return mixed
* @throws EcourierException
* @throws GuzzleException
*/
public function thana($cityName)
{
$response = $this->authorization()->send("POST", "/api/thana-list", ["city" => $cityName]);
return $response->message;
}

/**
* Postcode list
*
* @param string $cityName
* @param string $thanaName
*
* @return mixed
* @throws EcourierException
* @throws GuzzleException
*/
public function postcode($cityName, $thanaName)
{
$response = $this->authorization()->send("POST", "/api/postcode-list", ["city" => $cityName, "thana" => $thanaName]);
return $response->message;
}


/**
* Area list
*
* @param int $postcode
*
* @return mixed
* @throws EcourierException
* @throws GuzzleException
*/
public function areaList($postcode)
{
$response = $this->authorization()->send("POST", "/api/area-list", ["postcode" => $postcode]);
return $response->message;
}

/**
* Branch list
*
* @return mixed
* @throws EcourierException
* @throws GuzzleException
*/
public function branch()
{
$response = $this->authorization()->send("POST", "api/branch-list");
return $response;
}


}
105 changes: 105 additions & 0 deletions src/Apis/BaseApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Codeboxr\EcourierCourier\Apis;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ClientException;
use Codeboxr\EcourierCourier\Exceptions\EcourierException;

class BaseApi
{
/**
* @var string
*/
private $baseUrl;

/**
* @var Client
*/
private $request;

/**
* @var array
*/
private $headers;

public function __construct()
{
$this->setBaseUrl();
$this->setHeaders();
$this->request = new Client([
'base_uri' => $this->baseUrl,
'headers' => $this->headers
]);
}

/**
* Set Base Url on sandbox mode
*/
private function setBaseUrl()
{
if (config("ecourier.sandbox") == true) {
$this->baseUrl = "https://staging.ecourier.com.bd";
} else {
$this->baseUrl = "https://backoffice.ecourier.com.bd";
}
}

/**
* Set Default Headers
*/
private function setHeaders()
{
$this->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json",
];
}


/**
* Authorization set to header
*
* @return $this
*/
public function authorization()
{
$this->headers = [
"Accept" => "application/json",
"Content-Type" => "application/json",
'API-KEY' => config("ecourier.app_key"),
'API-SECRET' => config("ecourier.app_secret"),
'USER-ID' => config("ecourier.user_id")
];

return $this;
}

/**
* Sending Request
*
* @param string $method
* @param string $uri
* @param array $body
*
* @return mixed
* @throws EcourierException
* @throws GuzzleException
*/
public function send($method, $uri, $body = [])
{
try {
$response = $this->request->request($method, $uri, [
"headers" => $this->headers,
"body" => json_encode($body)
]);
return json_decode($response->getBody());
} catch (ClientException $e) {
$response = json_decode($e->getResponse()->getBody()->getContents());
$message = implode(",", $response->errors);
throw new EcourierException($message, $e->getCode(), $response->errors);
}
}

}
50 changes: 50 additions & 0 deletions src/Apis/OrderApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Codeboxr\EcourierCourier\Apis;

use Illuminate\Http\JsonResponse;
use GuzzleHttp\Exception\GuzzleException;
use Codeboxr\EcourierCourier\Exceptions\EcourierException;

class OrderApi extends BaseApi
{
/**
* Package list
*
* @return mixed
* @throws EcourierException
* @throws GuzzleException
*/
public function packageList()
{
$response = $this->authorization()->send("POST", "api/packages");
return $response;
}

/**
* Create Order
*
* @param array $array
*
* @return JsonResponse
* @throws EcourierException
* @throws GuzzleException
*/
public function create($array)
{
$response = $this->authorization()->send("POST", "api/order-place-reseller", $array);
return response()->json([
"success" => $response->success,
"response_code" => $response->response_code,
"message" => $response->message,
"ID" => $response->ID,
]);
}


public function tracking($trackingId)
{
$response = $this->authorization()->send("POST", "api/track", ["ecr" => $trackingId]);
return $response;
}
}
10 changes: 10 additions & 0 deletions src/Apis/StoreApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Codeboxr\EcourierCourier\Apis;

use GuzzleHttp\Exception\GuzzleException;

class StoreApi extends BaseApi
{

}
39 changes: 39 additions & 0 deletions src/EcourierServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Codeboxr\EcourierCourier;

use Illuminate\Support\ServiceProvider;
use Codeboxr\EcourierCourier\Apis\AreaApi;
use Codeboxr\EcourierCourier\Manage\Manage;
use Codeboxr\EcourierCourier\Apis\StoreApi;
use Codeboxr\EcourierCourier\Apis\OrderApi;

class EcourierServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . "/../config/ecourier.php" => config_path("ecourier.php")
]);
}

/**
* Register application services
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . "/../config/ecourier.php", "ecourier");

$this->app->bind("ecourier", function () {
return new Manage(new AreaApi(), new StoreApi(), new OrderApi());
});
}

}
29 changes: 29 additions & 0 deletions src/Exceptions/EcourierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Codeboxr\EcourierCourier\Exceptions;

use Throwable;
use Exception;

class EcourierException extends Exception
{
private $errors;

public function __construct($message = "", $code = 0, $errors = [], Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->errors = $errors;
}

/**
* @return array
*/
public function render()
{
return [
'code' => $this->code,
'message' => $this->getMessage(),
'errors' => $this->errors
];
}
}
25 changes: 25 additions & 0 deletions src/Facade/Ecourier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Codeboxr\EcourierCourier\Facade;

use Illuminate\Support\Facades\Facade;
use Codeboxr\EcourierCourier\Manage\Manage;

/**
* @method static area()
* @method static store()
* @method static order()
* @see Manage
*/
class Ecourier extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'ecourier';
}
}
Loading

0 comments on commit ba792fe

Please sign in to comment.