Skip to content

Commit

Permalink
V 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
irfaardy committed Feb 21, 2020
0 parents commit 10e1cb0
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 0 deletions.
147 changes: 147 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@




# Raja Ongkir Laravel
<h3>Installation with Composer</h3>

composer require irfa/raja-ongkir

***
<h3>PHP Native Setup</h3>


<?php
require "vendor/autoload.php";
use Irfa\RajaOngkir\Ongkir\Ongkir as RajaOngkir;
....
<b>Configuration File</b><br>
**File Location :** vendor/irfa/raja-ongkir/config/config.php

<?php
$config = [
'account_type' => 'your-account-type',

'api_key' => 'your-api-key',
];

***
<h3> Laravel Setup </h3>
<h3>Add to config/app.php</h3>



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



<h3>Add to config/app.php</h3>

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

],

<h3>Publish Config File</h3>


php artisan vendor:publish

Open .env file add

....
RAJAONGKIR_ACCOUNT_TYPE = starter
RAJAONGKIR_API_KEY = your-api-key
***
<h3>Usage</h3>

use RajaOngkir;

**Get all province**

$get = RajaOngkir::getProvince();
foreach($get as $prov):

echo $prov->province_id."<br>"; // value = 1
echo $prov->province."<br>";// value = Bali
endforeach;
**Search province**



$get = RajaOngkir::find(['province_id' => 1])->getProvince();
echo $get->province_id."<br>"; // value = 1
echo $get->province."<br>";// value = Bali


**Get all City**

$get = RajaOngkir::getCity();
foreach($get as $city):

echo $city->city_id."<br>"; // value = 17
echo $city->province_id."<br>";// value = 1
echo $city->province."<br>";// value = Bali
echo $city->type."<br>"; // value = Kabupaten
echo $city->city_name."<br>"; // value = Badung
echo $city->postal_code."<br>"; // value = 80351
endforeach;

**Get all City in province**


$get = RajaOngkir::find(['province_id' => 1])->getCity();
foreach($get as $city):

echo $city->city_id."<br>"; // value = 17
echo $city->province_id."<br>";// value = 1
echo $city->province."<br>";// value = Bali
echo $city->type."<br>"; // value = Kabupaten
echo $city->city_name."<br>"; // value = Badung
echo $city->postal_code."<br>"; // 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."<br>"; // value = jne
echo $city->name."<br>";// 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."<br>";
echo "Description: ".$cost->description."<br>";
foreach($cost->cost as $detail){
echo "Value: ".$detail->value."<br>";
echo "Estimasi: ".$detail->etd."<br>";
echo "Note: ".$detail->note."<br>";
echo "<hr>";
}
endforeach;


19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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/"
}
}
}
6 changes: 6 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$config = [
'account_type' => 'starter',

'api_key' => 'your-api-key',
];
10 changes: 10 additions & 0 deletions resources/config/irfa/rajaongkir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
return [

'account_type' => env('RAJAONGKIR_ACCOUNT_TYPE','starter'),

'api_key' => env('RAJAONGKIR_API_KEY','your-api-key'),



];
20 changes: 20 additions & 0 deletions src/Facades/Ongkir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/*
Author: Irfa Ardiansyah <irfa.backend@protonmail.com>
*/
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;
}
}
158 changes: 158 additions & 0 deletions src/Ongkir/Func/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/*
Author: Irfa Ardiansyah <irfa.backend@protonmail.com>
*/
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
),
);
}
}



Loading

0 comments on commit 10e1cb0

Please sign in to comment.