diff --git a/app/Http/Controllers/Auth/AddressController.php b/app/Http/Controllers/Auth/AddressController.php index 00c3b46..b3f55ba 100644 --- a/app/Http/Controllers/Auth/AddressController.php +++ b/app/Http/Controllers/Auth/AddressController.php @@ -32,6 +32,7 @@ class AddressController extends Controller 'postal_code' => $address->postal_code, 'latitude' => $address->latitude, 'longitude' => $address->longitude, + 'phone' => $address->phone, ]; }); diff --git a/app/Http/Controllers/CheckoutController.php b/app/Http/Controllers/CheckoutController.php index c04ba45..065cdce 100644 --- a/app/Http/Controllers/CheckoutController.php +++ b/app/Http/Controllers/CheckoutController.php @@ -10,6 +10,8 @@ use App\Repositories\Member\Transaction\TransactionRepository; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; +use Illuminate\Validation\ValidationException; + class CheckoutController extends Controller { public function index(Request $request, MemberCartRepository $memberCartRepository) @@ -20,7 +22,7 @@ class CheckoutController extends Controller $subtotal = $memberCartRepository->getSubtotal($request->input('location_id')); - $address_list = Address::where('user_id', $request->user()->id)->orderBy('is_primary','desc')->get(); + $address_list = Address::where('user_id', auth()->user()->id)->orderBy('is_primary','desc')->get(); $total = $subtotal; @@ -92,19 +94,32 @@ class CheckoutController extends Controller $request->merge(['location_id' => $location_id]); $carts = $memberCartRepository->getList($request); - $shipping_list = collect($shippingRepository->getList([ - "location_id" => $location_id, - "address_id" => $address_id, - "items" => $carts, - ])['pricing'])->map(function($row){ - return [ - 'courier' => $row['courier_code'], - 'service' => $row['courier_service_code'], - 'title' => $row['courier_name']." - ".$row['courier_service_name'], - 'description' => $row['duration'], - 'cost' => $row['shipping_fee'], - ]; - }); + + try{ + $shipping_list = collect($shippingRepository->getList([ + "location_id" => $location_id, + "address_id" => $address_id, + "items" => $carts, + ])['pricing'] ?? [])->map(function($row){ + return [ + 'courier' => $row['courier_code'], + 'service' => $row['courier_service_code'], + 'title' => $row['courier_name']." - ".$row['courier_service_name'], + 'description' => $row['duration'], + 'cost' => $row['shipping_fee'], + ]; + }); + + if (count($shipping_list) == 0) { + throw ValidationException::withMessages([ + "message" => "Tidak dapat menghitung ongkir" + ]); + } + }catch(\Exception $e){ + throw ValidationException::withMessages([ + "message" => $e->getMessage() + ]); + } return view('checkout.v1-delivery-1-shipping', [ 'carts' => $request->user()->carts, @@ -117,7 +132,7 @@ class CheckoutController extends Controller ]); } catch (\Exception $e) { Log::info($e); - return redirect()->route('checkout.delivery')->with('error', 'Invalid checkout data'); + return redirect()->route('checkout.delivery')->with('error', $e->getMessage() ?? 'Invalid checkout data'); } } diff --git a/app/Repositories/Member/ShippingRepository.php b/app/Repositories/Member/ShippingRepository.php index 6b6d924..7791617 100644 --- a/app/Repositories/Member/ShippingRepository.php +++ b/app/Repositories/Member/ShippingRepository.php @@ -96,13 +96,21 @@ class ShippingRepository }); if ($hasLatLong){ - $list = $biteship->rateByLatLong([ - "origin_latitude" => $location->latitude, - "origin_longitude" => $location->longitude, - "destination_latitude" => $address->latitude, - "destination_longitude" => $address->longitude, - "items" => $items - ]); + try{ + $list = $biteship->rateByLatLong([ + "origin_latitude" => $location->latitude, + "origin_longitude" => $location->longitude, + "destination_latitude" => $address->latitude, + "destination_longitude" => $address->longitude, + "items" => $items + ]); + }catch(ValidationException $e){ + $list = $biteship->rateByPostal([ + "origin_postal_code" => $location->postal_code, + "destination_postal_code" => $address->postal_code, + "items" => $items + ]); + } }else{ $list = $biteship->rateByPostal([ diff --git a/app/ThirdParty/Biteship/Rate.php b/app/ThirdParty/Biteship/Rate.php index 25b5a52..7728286 100644 --- a/app/ThirdParty/Biteship/Rate.php +++ b/app/ThirdParty/Biteship/Rate.php @@ -5,6 +5,7 @@ namespace App\ThirdParty\Biteship; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Cache; +use Illuminate\Validation\ValidationException; class Rate { @@ -28,19 +29,29 @@ class Rate $items) { $url = env("BITESHIP_URL"); $key = env("BITESHIP_KEY"); - $res = Http::withHeaders([ - "authorization" => $key - ]) - ->withBody(json_encode([ + + $body = [ "origin_latitude" => $origin_latitude, "origin_longitude" => $origin_longitude, "destination_latitude" => $destination_latitude, "destination_longitude" => $destination_longitude, - "couriers" => env("BITESHIP_COURIER_ALL","grab,gojek,tiki,jnt,anteraja"), + "couriers" => env("BITESHIP_COURIER","grab,gojek,tiki,jnt,anteraja"), "items" => $items - ]), 'application/json') + ]; + $res = Http::withHeaders([ + "authorization" => $key + ]) + ->withBody(json_encode($body), 'application/json') ->post($url."/v1/rates/couriers"); + if ($res->status() != 200 && $res->json()['error'] != null) { + Log::info(json_encode($res->json())); + Log::info($body); + throw ValidationException::withMessages([ + "message" => $res->json()['error'] + ]); + } + if ($res->status() == 200) return $res->json(); @@ -73,6 +84,12 @@ class Rate ]), 'application/json') ->post($url."/v1/rates/couriers"); + if ($res->status() != 200 && $res->json()['error'] != null) { + throw ValidationException::withMessages([ + "message" => $res->json()['error'] + ]); + } + if ($res->status() == 200) return $res->json(); diff --git a/resources/views/checkout/v1-cart.blade.php b/resources/views/checkout/v1-cart.blade.php index e6b64e6..5ed2592 100644 --- a/resources/views/checkout/v1-cart.blade.php +++ b/resources/views/checkout/v1-cart.blade.php @@ -83,7 +83,7 @@