ECOMMERCE/app/ThirdParty/Biteship/Rate.php

83 lines
2.8 KiB
PHP

<?php
namespace App\ThirdParty\Biteship;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
class Rate
{
public function byLatLong($params)
{
$origin_latitude = $params["origin_latitude"];
$origin_longitude = $params["origin_longitude"];
$destination_latitude = $params["destination_latitude"];
$destination_longitude = $params["destination_longitude"];
$items = $params["items"];
$sha1 = sha1(json_encode($items));
$key = implode("_", [$origin_latitude, $origin_longitude, $destination_latitude, $destination_longitude, $sha1]);
return Cache::remember("rates_".$key, 60 * 60 * 24, function()
use ($origin_latitude,
$origin_longitude,
$destination_latitude,
$destination_longitude,
$items) {
$url = env("BITESHIP_URL");
$key = env("BITESHIP_KEY");
$res = Http::withHeaders([
"authorization" => $key
])
->withBody(json_encode([
"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"),
"items" => $items
]), 'application/json')
->post($url."/v1/rates/couriers");
if ($res->status() == 200)
return $res->json();
return null;
});
}
public function byPostal($params)
{
$destination_postal_code = $params["destination_postal_code"];
$origin_postal_code = $params["origin_postal_code"];
$items = $params["items"];
$sha1 = sha1(json_encode($items));
$key = $origin_postal_code."_".$destination_postal_code."_".$sha1;
return Cache::remember("rates_".$key, 60 * 60 * 24, function() use (
$origin_postal_code,
$destination_postal_code,
$items) {
$url = env("BITESHIP_URL");
$key = env("BITESHIP_KEY");
$res = Http::withHeaders([
"authorization" => $key
])
->withBody(json_encode([
"origin_postal_code" => $origin_postal_code,
"destination_postal_code" => $destination_postal_code,
"couriers" => env("BITESHIP_COURIER","tiki,jnt,anteraja"),
"items" => $items
]), 'application/json')
->post($url."/v1/rates/couriers");
if ($res->status() == 200)
return $res->json();
return null;
});
}
}