diff --git a/config/rajaongkir.php b/config/rajaongkir.php index 621a618..de0fb94 100644 --- a/config/rajaongkir.php +++ b/config/rajaongkir.php @@ -4,11 +4,11 @@ /** * api key yang di dapat dari akun raja ongkir */ - 'API_KEY' => env('APIKEY_RAJAONGKIR', 'SomeRandomString'), + 'API_KEY' => env('RAJAONGKIR_KEY', 'somerandomstring'), /** * tipe akun untuk menentukan api url - * Starter, Basic, Pro + * starter, basic, pro */ - 'TIPE_AKUN' => env('TIPE_AKUN_RAJAONGKIR', 'starter') + 'ACCOUNT_TYPE' => env('RAJAONGKIR_TYPE', 'starter') ]; diff --git a/src/LaravelRajaongkir.php b/src/LaravelRajaongkir.php index 3230603..1510c49 100755 --- a/src/LaravelRajaongkir.php +++ b/src/LaravelRajaongkir.php @@ -2,49 +2,36 @@ namespace Kodepintar\LaravelRajaongkir; +use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Http; class LaravelRajaongkir { - private static $baseurl_starter = 'https://api.rajaongkir.com/starter'; - private static $baseurl_basic = 'https://api.rajaongkir.com/basic'; - private static $baseurl_pro = 'https://pro.rajaongkir.com/api'; + public const BASE_URL = [ + 'starter' => 'https://api.rajaongkir.com/starter', + 'basic' => 'https://api.rajaongkir.com/basic', + 'pro' => 'https://pro.rajaongkir.com/api', + ]; protected $url = null; public function __construct() { - $config = config('ongkir.APIKEY_RAJAONGKIR', 'starter'); + $accountType = config('rajaongkir.ACCOUNT_TYPE'); - switch ($config) { - case 'starter': - return $this->url = $this->baseurl_starter; + $this->url = self::BASE_URL[$accountType]; - break; - - case 'basic': - return $this->url = $this->baseurl_basic; - - break; - - case 'pro': - return $this->url = $this->baseurl_pro; - - break; - - default: - return $this->url = $this->baseurl_starter; - - break; - } + return $this; } - protected function requestHeader() + protected function apiCall(string $urlPath, array $payload = [], string $method = 'GET'): Response { + $url = $this->url.'/'.ltrim($urlPath, '/'); + return Http::withHeaders([ - 'key' => config('ongkir.APIKEY_RAJAONGKIR'), + 'key' => config('rajangkir.API_KEY'), 'content-type' => 'application/x-www-form-urlencoded', - ]); + ])->{strtolower($method)}($url, $payload); } /** @@ -52,7 +39,7 @@ protected function requestHeader() */ public function getProvince() { - return $this->requestHeader()->get($this->url . '/province'); + return $this->apiCall('/province'); } /** @@ -60,7 +47,7 @@ public function getProvince() */ public function getCity() { - return $this->requestHeader()->get(self::$url . '/city'); + return $this->apiCall('/city'); } /** @@ -68,42 +55,30 @@ public function getCity() */ public function getSubdistrict() { - return $this->requestHeader()->get(self::$url . '/subdistrict'); + return $this->apiCall('/subdistrict'); } /** * Method “cost” digunakan untuk mengetahui tarif pengiriman (ongkos kirim) dari dan ke kecamatan tujuan tertentu dengan berat tertentu. - * - * @param [int] $origin - * @param [string] $originType - * @param [int] $destination - * @param [string] $destinationType - * @param [int] $weight - * @param [string] $courier - * @return void */ - public function getCost($origin, $originType, $destination, $destinationType, $weight, $courier) + public function getCost(int $origin, string $originType, int $destination, string $destinationType, int $weight, string $courier) { - return $this->requestHeader()->post(self::$url . '/subdistrict', [ + return $this->apiCall('/subdistrict', [ "origin" => $origin, "originType" => $originType, "destination" => $destination, "destinationType" => $destinationType, "weight" => $weight, "courier" => $courier, - ]); + ], 'POST'); } /** * Method "internationalOrigin" digunakan untuk mendapatkan daftar/nama kota yang mendukung pengiriman internasional. - * - * @param [int] $internationalOrigin - * @param [int] $province - * @return void */ public function getInternationalOrigin($internationalOrigin, $province) { - return $this->requestHeader()->get(self::$url . '/v2/internationalOrigin', [ + return $this->apiCall('/v2/internationalOrigin', [ "internationalOrigin" => $internationalOrigin, "province" => $province, ]); @@ -111,13 +86,10 @@ public function getInternationalOrigin($internationalOrigin, $province) /** * Method "internationalDestination" digunakan untuk mendapatkan daftar/nama negara tujuan pengiriman internasional. - * - * @param [int] $idCountry - * @return void */ public function getInternationalDestination($idCountry) { - return $this->requestHeader()->get(self::$url . '/v2/internationalDestination', [ + return $this->apiCall($this->url . '/v2/internationalDestination', [ "id" => $idCountry, ]); } @@ -125,21 +97,15 @@ public function getInternationalDestination($idCountry) /** * Method “internationalCost” digunakan untuk mengetahui tarif pengiriman (ongkos kirim) * internasional dari kota-kota di Indonesia ke negara tujuan di seluruh dunia. - * - * @param [int] $origin - * @param [int] $destination - * @param [int] $weight - * @param [string] $courier - * @return void */ - public function getInternationalCost($origin, $destination, $weight, $courier) + public function getInternationalCost(int $origin, int $destination, int $weight, string $courier) { - return $this->requestHeader()->post(self::$url . '/v2/internationalCost', [ + return $this->apiCall($this->url . '/v2/internationalCost', [ "origin" => $origin, "destination" => $destination, "weight" => $weight, "courier" => $courier, - ]); + ], 'POST'); } /** @@ -147,21 +113,17 @@ public function getInternationalCost($origin, $destination, $weight, $courier) */ public function getCurrency() { - return $this->requestHeader()->post(self::$url . '/currency'); + return $this->apiCall('/currency', [], 'POST'); } /** * Method “waybill” untuk digunakan melacak/mengetahui status pengiriman berdasarkan nomor resi. - * - * @param [string] $noResi - * @param [string] $courier - * @return void */ public function getTracking($noResi, $courier) { - return $this->requestHeader()->post(self::$url . '/waybill', [ + return $this->apiCall('/waybill', [ "waybill" => $noResi, "courier" => $courier, - ]); + ], 'POST'); } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..66ce04e --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,7 @@ +assertEquals(config('rajaongkir.ACCOUNT_TYPE'), 'starter'); + $this->assertEquals(config('rajaongkir.API_KEY'), 'somerandomstring'); +}); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 5d36321..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -});