Skip to content

Commit ba792fe

Browse files
committed
Initial ecourier package
0 parents  commit ba792fe

File tree

11 files changed

+437
-0
lines changed

11 files changed

+437
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "codeboxr/ecourier-courier",
3+
"description": "Bangladeshi ecourier service api package",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Codeboxr\\EcourierCourier\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Codeboxr"
14+
}
15+
],
16+
"minimum-stability": "dev",
17+
"require": {
18+
"php": "^7.2|^7.3|^8.0|^8.1",
19+
"illuminate/support": "~6|~7|~8|~9",
20+
"guzzlehttp/guzzle": "^7.0.1"
21+
},
22+
"extra": {
23+
"laravel": {
24+
"providers": [
25+
"Codeboxr\\EcourierCourier\\EcourierServiceProvider"
26+
]
27+
}
28+
}
29+
}

config/ecourier.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
"sandbox" => env("ECOURIER_SANDBOX", false),
5+
"app_key" => env("ECOURIER_API_KEY", ""),
6+
"app_secret" => env("ECOURIER_API_SECRET", ""),
7+
"user_id" => env("ECOURIER_USER_ID", "")
8+
];

src/Apis/AreaApi.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Apis;
4+
5+
use GuzzleHttp\Exception\GuzzleException;
6+
use Codeboxr\EcourierCourier\Exceptions\EcourierException;
7+
8+
class AreaApi extends BaseApi
9+
{
10+
/**
11+
* get city List
12+
*
13+
* @return mixed
14+
* @throws GuzzleException
15+
* @throws EcourierException
16+
*/
17+
public function city()
18+
{
19+
$response = $this->authorization()->send("POST", "/api/city-list");
20+
return $response;
21+
}
22+
23+
/**
24+
* get thana List
25+
*
26+
* @param string $cityName
27+
*
28+
* @return mixed
29+
* @throws EcourierException
30+
* @throws GuzzleException
31+
*/
32+
public function thana($cityName)
33+
{
34+
$response = $this->authorization()->send("POST", "/api/thana-list", ["city" => $cityName]);
35+
return $response->message;
36+
}
37+
38+
/**
39+
* Postcode list
40+
*
41+
* @param string $cityName
42+
* @param string $thanaName
43+
*
44+
* @return mixed
45+
* @throws EcourierException
46+
* @throws GuzzleException
47+
*/
48+
public function postcode($cityName, $thanaName)
49+
{
50+
$response = $this->authorization()->send("POST", "/api/postcode-list", ["city" => $cityName, "thana" => $thanaName]);
51+
return $response->message;
52+
}
53+
54+
55+
/**
56+
* Area list
57+
*
58+
* @param int $postcode
59+
*
60+
* @return mixed
61+
* @throws EcourierException
62+
* @throws GuzzleException
63+
*/
64+
public function areaList($postcode)
65+
{
66+
$response = $this->authorization()->send("POST", "/api/area-list", ["postcode" => $postcode]);
67+
return $response->message;
68+
}
69+
70+
/**
71+
* Branch list
72+
*
73+
* @return mixed
74+
* @throws EcourierException
75+
* @throws GuzzleException
76+
*/
77+
public function branch()
78+
{
79+
$response = $this->authorization()->send("POST", "api/branch-list");
80+
return $response;
81+
}
82+
83+
84+
}

src/Apis/BaseApi.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Apis;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use GuzzleHttp\Exception\ClientException;
8+
use Codeboxr\EcourierCourier\Exceptions\EcourierException;
9+
10+
class BaseApi
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $baseUrl;
16+
17+
/**
18+
* @var Client
19+
*/
20+
private $request;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $headers;
26+
27+
public function __construct()
28+
{
29+
$this->setBaseUrl();
30+
$this->setHeaders();
31+
$this->request = new Client([
32+
'base_uri' => $this->baseUrl,
33+
'headers' => $this->headers
34+
]);
35+
}
36+
37+
/**
38+
* Set Base Url on sandbox mode
39+
*/
40+
private function setBaseUrl()
41+
{
42+
if (config("ecourier.sandbox") == true) {
43+
$this->baseUrl = "https://staging.ecourier.com.bd";
44+
} else {
45+
$this->baseUrl = "https://backoffice.ecourier.com.bd";
46+
}
47+
}
48+
49+
/**
50+
* Set Default Headers
51+
*/
52+
private function setHeaders()
53+
{
54+
$this->headers = [
55+
"Accept" => "application/json",
56+
"Content-Type" => "application/json",
57+
];
58+
}
59+
60+
61+
/**
62+
* Authorization set to header
63+
*
64+
* @return $this
65+
*/
66+
public function authorization()
67+
{
68+
$this->headers = [
69+
"Accept" => "application/json",
70+
"Content-Type" => "application/json",
71+
'API-KEY' => config("ecourier.app_key"),
72+
'API-SECRET' => config("ecourier.app_secret"),
73+
'USER-ID' => config("ecourier.user_id")
74+
];
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* Sending Request
81+
*
82+
* @param string $method
83+
* @param string $uri
84+
* @param array $body
85+
*
86+
* @return mixed
87+
* @throws EcourierException
88+
* @throws GuzzleException
89+
*/
90+
public function send($method, $uri, $body = [])
91+
{
92+
try {
93+
$response = $this->request->request($method, $uri, [
94+
"headers" => $this->headers,
95+
"body" => json_encode($body)
96+
]);
97+
return json_decode($response->getBody());
98+
} catch (ClientException $e) {
99+
$response = json_decode($e->getResponse()->getBody()->getContents());
100+
$message = implode(",", $response->errors);
101+
throw new EcourierException($message, $e->getCode(), $response->errors);
102+
}
103+
}
104+
105+
}

src/Apis/OrderApi.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Apis;
4+
5+
use Illuminate\Http\JsonResponse;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use Codeboxr\EcourierCourier\Exceptions\EcourierException;
8+
9+
class OrderApi extends BaseApi
10+
{
11+
/**
12+
* Package list
13+
*
14+
* @return mixed
15+
* @throws EcourierException
16+
* @throws GuzzleException
17+
*/
18+
public function packageList()
19+
{
20+
$response = $this->authorization()->send("POST", "api/packages");
21+
return $response;
22+
}
23+
24+
/**
25+
* Create Order
26+
*
27+
* @param array $array
28+
*
29+
* @return JsonResponse
30+
* @throws EcourierException
31+
* @throws GuzzleException
32+
*/
33+
public function create($array)
34+
{
35+
$response = $this->authorization()->send("POST", "api/order-place-reseller", $array);
36+
return response()->json([
37+
"success" => $response->success,
38+
"response_code" => $response->response_code,
39+
"message" => $response->message,
40+
"ID" => $response->ID,
41+
]);
42+
}
43+
44+
45+
public function tracking($trackingId)
46+
{
47+
$response = $this->authorization()->send("POST", "api/track", ["ecr" => $trackingId]);
48+
return $response;
49+
}
50+
}

src/Apis/StoreApi.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Apis;
4+
5+
use GuzzleHttp\Exception\GuzzleException;
6+
7+
class StoreApi extends BaseApi
8+
{
9+
10+
}

src/EcourierServiceProvider.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Codeboxr\EcourierCourier\Apis\AreaApi;
7+
use Codeboxr\EcourierCourier\Manage\Manage;
8+
use Codeboxr\EcourierCourier\Apis\StoreApi;
9+
use Codeboxr\EcourierCourier\Apis\OrderApi;
10+
11+
class EcourierServiceProvider extends ServiceProvider
12+
{
13+
/**
14+
* Bootstrap services.
15+
*
16+
* @return void
17+
*/
18+
public function boot()
19+
{
20+
$this->publishes([
21+
__DIR__ . "/../config/ecourier.php" => config_path("ecourier.php")
22+
]);
23+
}
24+
25+
/**
26+
* Register application services
27+
*
28+
* @return void
29+
*/
30+
public function register()
31+
{
32+
$this->mergeConfigFrom(__DIR__ . "/../config/ecourier.php", "ecourier");
33+
34+
$this->app->bind("ecourier", function () {
35+
return new Manage(new AreaApi(), new StoreApi(), new OrderApi());
36+
});
37+
}
38+
39+
}

src/Exceptions/EcourierException.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Exceptions;
4+
5+
use Throwable;
6+
use Exception;
7+
8+
class EcourierException extends Exception
9+
{
10+
private $errors;
11+
12+
public function __construct($message = "", $code = 0, $errors = [], Throwable $previous = null)
13+
{
14+
parent::__construct($message, $code, $previous);
15+
$this->errors = $errors;
16+
}
17+
18+
/**
19+
* @return array
20+
*/
21+
public function render()
22+
{
23+
return [
24+
'code' => $this->code,
25+
'message' => $this->getMessage(),
26+
'errors' => $this->errors
27+
];
28+
}
29+
}

src/Facade/Ecourier.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Facade;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use Codeboxr\EcourierCourier\Manage\Manage;
7+
8+
/**
9+
* @method static area()
10+
* @method static store()
11+
* @method static order()
12+
* @see Manage
13+
*/
14+
class Ecourier extends Facade
15+
{
16+
/**
17+
* Get the registered name of the component.
18+
*
19+
* @return string
20+
*/
21+
protected static function getFacadeAccessor()
22+
{
23+
return 'ecourier';
24+
}
25+
}

0 commit comments

Comments
 (0)