· 5 years ago · May 16, 2020, 03:12 PM
1 public function shipping(Request $request)
2 {
3 $user = Auth::user();
4 $status = "error";
5 $message = "";
6 $data = null;
7 $code = 200;
8 if ($user) {
9 $this->validate($request, [
10 'name' => 'required',
11 'address' => 'required',
12 'phone' => 'required',
13 'province_id' => 'required',
14 'city_id' => 'required',
15 ]);
16 $user->name = $request->name;
17 $user->address = $request->address;
18 $user->phone = $request->phone;
19 $user->province_id = $request->province_id;
20 $user->city_id = $request->city_id;
21 if($user->save()){
22 $status = "success";
23 $message = "Update shipping success";
24 $data = $user->toArray();
25 }
26 else{
27 $message = "Update shipping failed";
28 }
29 }
30 else{
31 $message = "User not found";
32 }
33
34 return response()->json([
35 'status' => $status,
36 'message' => $message,
37 'data' => $data
38 ], $code);
39 }
40
41 public function couriers()
42 {
43 $couriers = [
44 ['id'=>'jne', 'text'=> 'JNE'],
45 ['id'=>'tiki', 'text'=> 'TIKI'],
46 ['id'=>'pos', 'text'=> 'POS'],
47 ];
48
49 return response()->json([
50 'status' => 'success',
51 'message' => 'courier',
52 'data' => $couriers
53 ], 200);
54 }
55
56 protected function getServices($data)
57 {
58 $url_cost = "https://api.rajaongkir.com/starter/cost";
59 $key="721efffac01429ff54c5258e2ea5177e";
60 $postdata = http_build_query($data);
61 $curl = curl_init();
62 curl_setopt_array($curl, [
63 CURLOPT_URL => $url_cost,
64 CURLOPT_RETURNTRANSFER => true,
65 CURLOPT_ENCODING => "",
66 CURLOPT_MAXREDIRS => 10,
67 CURLOPT_TIMEOUT => 30,
68 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
69 CURLOPT_CUSTOMREQUEST => "POST",
70 CURLOPT_POSTFIELDS => $postdata,
71 CURLOPT_HTTPHEADER => [
72 "content-type: application/x-www-form-urlencoded",
73 "key: ".$key
74 ],
75 ]);
76 $response = curl_exec($curl);
77 $error = curl_error($curl);
78 curl_close($curl);
79 return [
80 'error' => $error,
81 'response' => $response,
82 ];
83 }
84
85 protected function validateCart($carts)
86 {
87 $safe_carts = [];
88 $total = [
89 'quantity_before' => 0,
90 'quantity' => 0,
91 'price' => 0,
92 'weight' => 0,
93 ];
94 $idx = 0;
95 foreach($carts as $cart){
96 $id = (int)$cart['id'];
97 $quantity = (int)$cart['quantity'];
98 $total['quantity_before'] += $quantity;
99 $book = Book::find($id);
100 if($book){
101 if($book->stock>0){
102 $safe_carts[$idx]['id'] = $book->id;
103 $safe_carts[$idx]['title'] = $book->title;
104 $safe_carts[$idx]['cover'] = $book->cover;
105 $safe_carts[$idx]['price'] = $book->price;
106 $safe_carts[$idx]['weight'] = $book->weight;
107 if($book->stock < $quantity){
108 $quantity = (int) $book->stock;
109 }
110 $safe_carts[$idx]['quantity'] = $quantity;
111
112 $total['quantity'] += $quantity;
113 $total['price'] += $book->price * $quantity;
114 $total['weight'] += $book->weight * $quantity;
115 $idx++;
116 }
117 else{
118 continue;
119 }
120 }
121 }
122 return [
123 'safe_carts' => $safe_carts,
124 'total' => $total,
125 ];
126 }
127
128 public function services(Request $request)
129 {
130 $status = "error";
131 $message = "";
132 $data = [];
133 // validasi kelengkapan data
134 $this->validate($request, [
135 'courier' => 'required',
136 'carts' => 'required',
137 ]);
138
139 $user = Auth::user();
140 if($user){
141 $destination = $user->city_id;
142 if($destination>0){
143 // hardcode
144 $origin = 153; // Jakarta Selatan
145 $courier = $request->courier;
146 $carts = $request->carts;
147 $carts = json_decode($carts, true);
148 // validasi data belanja
149 $validCart = $this->validateCart($carts);
150 $data['safe_carts'] = $validCart['safe_carts'];
151 $data['total'] = $validCart['total'];
152 $quantity_different = $data['total']['quantity_before']<>$data['total']['quantity'];
153 $weight = $validCart['total']['weight'] * 1000;
154 if($weight > 0){
155 // request courier service API RajaOngkir
156 $parameter = [
157 "origin" => $origin,
158 "destination" => $destination,
159 "weight" => $weight,
160 "courier" => $courier
161 ];
162 $respon_services = $this->getServices($parameter);
163 if ($respon_services['error']==null) {
164 $services = [];
165 $response = json_decode($respon_services['response']);
166 $costs = $response->rajaongkir->results[0]->costs;
167 foreach($costs as $cost){
168 $service_name = $cost->service;
169 $service_cost = $cost->cost[0]->value;
170 $service_estimation = str_replace('hari', '', trim($cost->cost[0]->etd));
171 $services[] = [
172 'service' => $service_name,
173 'cost' => $service_cost,
174 'estimation' => $service_estimation,
175 'resume' => $service_name .' [ Rp. '.number_format($service_cost).', Etd: '.$cost->cost[0]->etd.' day(s) ]'
176 ];
177 }
178
179 // Response
180 if(count($services)>0){
181 $data['services'] = $services;
182 $status = "success";
183 $message = "getting services success";
184 }
185 else{
186 $message = "courier services unavailable";
187 }
188
189 if($quantity_different){
190 $status = "warning";
191 $message = "Check cart data, ".$message;
192 }
193
194 } else {
195 $message = "cURL Error #:" . $respon_services['error'];
196 }
197 }
198 else{
199 $message = "weight invalid";
200 }
201 }
202 else{
203 $message = "destination not set";
204 }
205 }
206 else{
207 $message = "user not found";
208 }
209
210 return response()->json([
211 'status' => $status,
212 'message' => $message,
213 'data' => $data
214 ], 200);
215 }