Skip to content

Commit 0f35079

Browse files
committed
add validation rules and cancel api
1 parent 568d70a commit 0f35079

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

src/Apis/BaseApi.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Exception\GuzzleException;
77
use GuzzleHttp\Exception\ClientException;
88
use Codeboxr\EcourierCourier\Exceptions\EcourierException;
9+
use Codeboxr\EcourierCourier\Exceptions\EcourierValidationException;
910

1011
class BaseApi
1112
{
@@ -102,4 +103,35 @@ public function send($method, $uri, $body = [])
102103
}
103104
}
104105

106+
/**
107+
* Ecourier validation
108+
*
109+
* @param array $data
110+
* @param array $requiredFileds
111+
*
112+
* @throws EcourierValidationException
113+
*/
114+
public function validation($data, $requiredFileds)
115+
{
116+
if (!is_array($data) || !is_array($requiredFileds)) {
117+
throw new \TypeError("Argument must be of the type array", 500);
118+
}
119+
120+
if (!count($data) || !count($requiredFileds)) {
121+
throw new EcourierValidationException("Invalid data!", 422);
122+
}
123+
124+
$requiredColumns = array_diff($requiredFileds, array_keys($data));
125+
if (count($requiredColumns)) {
126+
throw new EcourierValidationException($requiredColumns, 422);
127+
}
128+
129+
foreach ($requiredFileds as $filed) {
130+
if (isset($data[$filed]) && empty($data[$filed])) {
131+
throw new EcourierValidationException("$filed is required", 422);
132+
}
133+
}
134+
135+
}
136+
105137
}

src/Apis/OrderApi.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\JsonResponse;
66
use GuzzleHttp\Exception\GuzzleException;
77
use Codeboxr\EcourierCourier\Exceptions\EcourierException;
8+
use Codeboxr\EcourierCourier\Exceptions\EcourierValidationException;
89

910
class OrderApi extends BaseApi
1011
{
@@ -28,10 +29,30 @@ public function packageList()
2829
*
2930
* @return JsonResponse
3031
* @throws EcourierException
31-
* @throws GuzzleException
32+
* @throws GuzzleException|EcourierValidationException
3233
*/
3334
public function create($array)
3435
{
36+
$this->validation($array, [
37+
"pick_district",
38+
"pick_thana",
39+
"pick_union",
40+
"pick_address",
41+
"pick_mobile",
42+
"recipient_name",
43+
"recipient_mobile",
44+
"recipient_city",
45+
"recipient_area",
46+
"recipient_thana",
47+
"recipient_union",
48+
"recipient_address",
49+
"package_code",
50+
"product_price",
51+
"payment_method",
52+
"ep_id",
53+
"pick_hub"
54+
]);
55+
3556
$response = $this->authorization()->send("POST", "api/order-place-reseller", $array);
3657
return response()->json([
3758
"success" => $response->success,
@@ -41,10 +62,34 @@ public function create($array)
4162
]);
4263
}
4364

44-
65+
/**
66+
* @param $trackingId
67+
*
68+
* @return mixed
69+
* @throws EcourierException
70+
* @throws GuzzleException
71+
*/
4572
public function tracking($trackingId)
4673
{
4774
$response = $this->authorization()->send("POST", "api/track", ["ecr" => $trackingId]);
4875
return $response;
4976
}
77+
78+
79+
/**
80+
* Cancel Order
81+
*
82+
* @param array $array
83+
*
84+
* @return mixed
85+
* @throws EcourierException
86+
* @throws EcourierValidationException
87+
* @throws GuzzleException
88+
*/
89+
public function cancelOrder($array)
90+
{
91+
$this->validation($array, ["tracking", "comment"]);
92+
$response = $this->authorization()->send("POST", "api/cancel-order", $array);
93+
return $response;
94+
}
5095
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Codeboxr\EcourierCourier\Exceptions;
4+
5+
use Throwable;
6+
use Exception;
7+
8+
class EcourierValidationException extends Exception
9+
{
10+
public function __construct($message = "", $code = 0, Throwable $previous = null)
11+
{
12+
if (is_array($message)) {
13+
$requiredColumnsImplode = implode(",", $message);
14+
parent::__construct("$requiredColumnsImplode filed is required", $code, $previous);
15+
} else {
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}
19+
20+
/*public function render()
21+
{
22+
return [
23+
"code" => $this->code,
24+
"error" => $this->message
25+
];
26+
}*/
27+
}

0 commit comments

Comments
 (0)