From 10e1cb03a5f7923df619af9da203630a110a58eb Mon Sep 17 00:00:00 2001 From: IRFA Date: Fri, 21 Feb 2020 11:31:36 +0700 Subject: [PATCH] V 1.0 --- README.md | 147 +++++++++++++++++++++++++ composer.json | 19 ++++ config/config.php | 6 + resources/config/irfa/rajaongkir.php | 10 ++ src/Facades/Ongkir.php | 20 ++++ src/Ongkir/Func/Api.php | 158 +++++++++++++++++++++++++++ src/Ongkir/Ongkir.php | 36 ++++++ src/RajaOngkirServiceProvider.php | 32 ++++++ src/routes.php | 4 + 9 files changed, 432 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 config/config.php create mode 100644 resources/config/irfa/rajaongkir.php create mode 100644 src/Facades/Ongkir.php create mode 100644 src/Ongkir/Func/Api.php create mode 100644 src/Ongkir/Ongkir.php create mode 100644 src/RajaOngkirServiceProvider.php create mode 100644 src/routes.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..b27803f --- /dev/null +++ b/README.md @@ -0,0 +1,147 @@ + + + + +# Raja Ongkir Laravel +

Installation with Composer

+ + composer require irfa/raja-ongkir + +*** +

PHP Native Setup

+ + + Configuration File
+**File Location :** vendor/irfa/raja-ongkir/config/config.php + + 'your-account-type', + + 'api_key' => 'your-api-key', + ]; + +*** +

Laravel Setup

+

Add to config/app.php

+ + + + 'providers' => [ + .... + Irfa\RajaOngkir\RajaOngkirServiceProvider::class, + ]; + + + +

Add to config/app.php

+ + 'aliases' => [ + .... + 'RajaOngkir' => Irfa\RajaOngkir\Facades\Ongkir::class, + + ], + +

Publish Config File

+ + + php artisan vendor:publish + +Open .env file add + + .... + RAJAONGKIR_ACCOUNT_TYPE = starter + RAJAONGKIR_API_KEY = your-api-key + *** +

Usage

+ + use RajaOngkir; + + **Get all province** + + $get = RajaOngkir::getProvince(); + foreach($get as $prov): + + echo $prov->province_id."
"; // value = 1 + echo $prov->province."
";// value = Bali + + + endforeach; +**Search province** + + + + $get = RajaOngkir::find(['province_id' => 1])->getProvince(); + + echo $get->province_id."
"; // value = 1 + echo $get->province."
";// value = Bali + + +**Get all City** + + $get = RajaOngkir::getCity(); + foreach($get as $city): + + echo $city->city_id."
"; // value = 17 + echo $city->province_id."
";// value = 1 + echo $city->province."
";// value = Bali + echo $city->type."
"; // value = Kabupaten + echo $city->city_name."
"; // value = Badung + echo $city->postal_code."
"; // value = 80351 + + endforeach; + +**Get all City in province** + + + $get = RajaOngkir::find(['province_id' => 1])->getCity(); + foreach($get as $city): + + echo $city->city_id."
"; // value = 17 + echo $city->province_id."
";// value = 1 + echo $city->province."
";// value = Bali + echo $city->type."
"; // value = Kabupaten + echo $city->city_name."
"; // value = Badung + echo $city->postal_code."
"; // value = 80351 + + endforeach; + **Get courier** + + + $get = RajaOngkir::find(['origin'=>1, + 'destination'=>2, + 'weight'=>1000,//1000gr + 'courier' => 'jne' + ]) + ->getCourier(); + foreach($get as $city): + + echo $city->code."
"; // value = jne + echo $city->name."
";// value = Jalur Nugraha Ekakurir (JNE) + + endforeach; + +**Example cost Courier** + + + $params = ['origin'=>1,'destination'=>2,'weight'=>1000,'courier' => 'jne' + ] + foreach(RajaOngkir::find($params)->getCostDetails() as $cost): + + echo "Service: ".$cost->service."
"; + echo "Description: ".$cost->description."
"; + + foreach($cost->cost as $detail){ + echo "Value: ".$detail->value."
"; + echo "Estimasi: ".$detail->etd."
"; + echo "Note: ".$detail->note."
"; + echo "
"; + } + endforeach; + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..96030ae --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "irfa/raja-ongkir", + "description": "\"Laravel Raja Ongkir Package\"", + "license": "MIT", + "authors": [ + { + "name": "Irfa A", + "email": "irfa.backend@protonmail.com" + } + ], + "require": { + "php": ">=5.4.0" + }, + "autoload": { + "psr-4": { + "Irfa\\RajaOngkir\\": "src/" + } + } +} diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..ff6c041 --- /dev/null +++ b/config/config.php @@ -0,0 +1,6 @@ + 'starter', + + 'api_key' => 'your-api-key', + ]; \ No newline at end of file diff --git a/resources/config/irfa/rajaongkir.php b/resources/config/irfa/rajaongkir.php new file mode 100644 index 0000000..c18681a --- /dev/null +++ b/resources/config/irfa/rajaongkir.php @@ -0,0 +1,10 @@ + env('RAJAONGKIR_ACCOUNT_TYPE','starter'), + + 'api_key' => env('RAJAONGKIR_API_KEY','your-api-key'), + + + +]; \ No newline at end of file diff --git a/src/Facades/Ongkir.php b/src/Facades/Ongkir.php new file mode 100644 index 0000000..7c19beb --- /dev/null +++ b/src/Facades/Ongkir.php @@ -0,0 +1,20 @@ + +*/ +namespace Irfa\RajaOngkir\Facades; + +use Illuminate\Support\Facades\Facade; + +class Ongkir extends Facade +{ + /** + * Get the registered name of the component. + * + * @return string + */ + protected static function getFacadeAccessor() + { + return \Irfa\RajaOngkir\Ongkir\Ongkir::class; + } +} diff --git a/src/Ongkir/Func/Api.php b/src/Ongkir/Func/Api.php new file mode 100644 index 0000000..b227219 --- /dev/null +++ b/src/Ongkir/Func/Api.php @@ -0,0 +1,158 @@ + +*/ +namespace Irfa\RajaOngkir\Ongkir\Func; + +class Api { + + private static $account_type; + private static $api_key; + private static $count=0; + + function __construct(){ + + + } + private static function setup_option(){ + if(function_exists('config') AND function_exists('app')){//Load Config For Laravel + self::$account_type = config('irfa.rajaongkir.account_type'); + self::$api_key = config('irfa.rajaongkir.api_key'); + } else{//For PHP Native + require(__DIR__."../../../../config/config.php"); + self::$account_type =$config['account_type']; + self::$api_key = $config['api_key']; + } + + } + protected static function get_province($arr = null){ + if($arr != null){ + $province_id = array_key_exists('province_id', $arr)?"?id=".$arr['province_id']:null; + } else { + $province_id = null; + } + self::setup_option(); + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => "https://api.rajaongkir.com/".self::$account_type."/province".$province_id, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", + CURLOPT_HTTPHEADER => array( + "key: ".self::$api_key + ), + )); + $response = curl_exec($curl); + $err = curl_error($curl); + + curl_close($curl); + + if ($err) { + echo "cURL Error #:" . $err; + } else { + $res = json_decode($response,false)->rajaongkir->results; + + + return $res; + } + } + protected static function get_city($arr=null){ + if($arr != null){ + $province_id = array_key_exists('province_id', $arr)?"?province=".$arr['province_id']:null; + } else { + $province_id = null; + } + self::setup_option(); + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => "https://api.rajaongkir.com/".self::$account_type."/city".$province_id, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", + CURLOPT_HTTPHEADER => array( + "key: ".self::$api_key + ), + )); + $response = curl_exec($curl); + $err = curl_error($curl); + + curl_close($curl); + + if ($err) { + echo "cURL Error #:" . $err; + } else { + $res = json_decode($response,false)->rajaongkir->results; + + + return $res; + } + } + protected static function get_courier($arr){ + $origin = $arr['origin']; + $destination = $arr['destination']; + $weight = $arr['weight']; + $courier = $arr['courier']; + $res = self::curl_cost_get($origin,$destination,$weight,$courier); + + return $res; + + + } + + protected static function get_cost_details($arr){ + $origin = $arr['origin']; + $destination = $arr['destination']; + $weight = $arr['weight']; + $courier = $arr['courier']; + $res = self::curl_cost_get($origin,$destination,$weight,$courier); + + return $res{0}->costs; + } + + private static function curl_cost_get($origin,$destination,$weight,$courier){ + $curl = curl_init(); + + curl_setopt_array($curl, self::curl_cost_option($origin,$destination,$weight,$courier)); + + $response = curl_exec($curl); + $err = curl_error($curl); + + curl_close($curl); + + if ($err) { + return "cURL Error #:" . $err; + } else { + + + $res = json_decode($response,false)->rajaongkir->results; + return $res; + } + } + private static function curl_cost_option($origin,$destination,$weight,$courier){ + self::setup_option(); + // self::$api_key = 'cd241c22aa5365c37e2464bdbe7b880e'; + return array( + CURLOPT_URL => 'https://api.rajaongkir.com/'.self::$account_type.'/cost', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => "origin=".$origin."&destination=".$destination."&weight=".$weight."&courier=".strtolower($courier), + CURLOPT_HTTPHEADER => array( + "content-type: application/x-www-form-urlencoded", + "key: ".self::$api_key + ), + ); + } +} + + + \ No newline at end of file diff --git a/src/Ongkir/Ongkir.php b/src/Ongkir/Ongkir.php new file mode 100644 index 0000000..1aaa3dd --- /dev/null +++ b/src/Ongkir/Ongkir.php @@ -0,0 +1,36 @@ + +*/ + +namespace Irfa\RajaOngkir\Ongkir; + +use Irfa\RajaOngkir\Ongkir\Func\Api; + +class Ongkir extends Api{ + + private static $arr; + + public static function find($arr){ + self::$arr = $arr; + return new static(); + + } + public static function getCostDetails(){ + return self::get_cost_details(self::$arr); + } + + public static function getCourier(){ + return self::get_courier(self::$arr); + } + + public static function getProvince(){ + + return self::get_province(self::$arr); + } + + public static function getCity(){ + + return self::get_city(self::$arr); + } +} diff --git a/src/RajaOngkirServiceProvider.php b/src/RajaOngkirServiceProvider.php new file mode 100644 index 0000000..6f1d59e --- /dev/null +++ b/src/RajaOngkirServiceProvider.php @@ -0,0 +1,32 @@ +loadRoutesFrom(__DIR__.'/routes.php'); + $this->publishes([ + __DIR__.'/../resources/config/irfa/rajaongkir.php' => config_path('irfa/rajaongkir.php'), + ]); + + } +} diff --git a/src/routes.php b/src/routes.php new file mode 100644 index 0000000..bba0be5 --- /dev/null +++ b/src/routes.php @@ -0,0 +1,4 @@ +