Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Agung Jati Kusumo committed Jun 28, 2017
0 parents commit dd40203
Show file tree
Hide file tree
Showing 11 changed files with 4,499 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2010-2017 Google, Inc. http://angularjs.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Raja Ongkir API Wrapper for Laravel
> Mempermudah penggunaan API raja ongkir pada aplikasi berbasis laravel
Dengan pacakage ini anda dapat menggunakan API Raja Ongkir dengan mudah karena sudah terintegrasi dengan facade laravel 5+.

![](cover.png)

## Install Package Composer
1. Otomatis Menggunakan Composer:
```sh
composer require agungjk/rajaongkir
```

2. Tambahkan Manual ke **composer.json**:
```sh
{
"require": {
"agungjk/rajaongkir" : "dev-master"
}
}
```
## Integrasi Ke Laravel
1. Tambahkan service provider ke config/app.php
```php
'providers' => [
....
Agungjk\Rajaongkir\RajaOngkirServiceProvider::class,
]
```

2. Tambahkan juga aliasnya ke config/app.php
```php
'aliases' => [
....
'RajaOngkir' => Agungjk\Rajaongkir\RajaOngkirFacade::class,
]
```

## Publish Config Package Laravel
Jalankan command artisan berikut ```php artisan vendor:publish``` untuk publish secara otomatis, atau menggunakan cara manual seperti berikut ini:

1. Buat file **rajaongkir.php** di folder **/config** secara manual
2. Tambahkan Kodingan berikut ini:
```php
<?php
return [
'end_point_api' => env('RAJAONGKIR_ENDPOINT', 'http://rajaongkir.com/api/starter'),
'api_key' => env('RAJAONGKIR_KEY', 'SomeRandomString'),
];
```

## Setting Environment
Tambahkan kode berikut di file .env untuk konfigurasi API rajaongkir
```
RAJAONGKIR_ENDPOINT=isi_base_url_api_akun_anda_disini
RAJAONGKIR_KEY=isi_api_key_anda_disini
```
atau anda juga dapat langsung melakukan konfigurasi di file **rajaongkir.php** di folder **config** seperti kode berikut.
```php
'end_point_api' => 'isi_base_url_api_akun_anda_disini',
'api_key' => 'isi_api_key_anda_disini',
```

## Contoh Penggunaan
Berikut adalah beberpa fungsi yang terdapat dalam package ini:
1. Mengambil Data Provinsi
a. Semua Data Provinsi
```php
$list_provinsi = RajaOngkir::province();
```
b. Data Provinsi Berdasarkan ID
```php
$provinsi_id = 1;
$data_provinsi = RajaOngkir::province($provinsi_id);
```

2. Mengambil Data Kota
a. Semua Data Kota
```php
$list_kota = RajaOngkir::city();
```
b. Data Kota Berdasarkan ID
```php
$kota_id = 1;
$data_kota = RajaOngkir::city($kota_id);
```

3. Mengkalkulasi Biaya
```php
$kota_asal_id = 501;
$kota_tujuan_id = 114;
$berat = 1700; // dalam gram
$kurir = "jne";
$list_biaya = RajaOngkir::cost($kota_asal_id, $kota_tujuan_id, $berat, $kurir);
```

## Release History

* 0.2.0
* CHANGE: Rename function for more readable
* 0.1.0
* Initial fork version

## Meta

Agung Jati Kusumo – [@its_agungjk](https://twitter.com/its_agungjk) – [email protected]

Distributed under the MIT license. See ``LICENSE`` for more information.

[https://github.com/agungjk/rajaongkir](https://github.com/agungjk/rajaongkir)

## Contributing

1. Fork it (<https://github.com/agungjk/rajaongkir/fork>)
2. Create your feature branch (`git checkout -b feature/fooBar`)
3. Commit your changes (`git commit -am 'Add some fooBar'`)
4. Push to the branch (`git push origin feature/fooBar`)
5. Create a new Pull Request

Kunjungi [rajaongkir](http://rajaongkir.com/)

Documentasi akun [starter](http://rajaongkir.com/dokumentasi/starter)
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "agungjk/rajaongkir",
"description": "Raja Ongkir API Wrapper for Laravel 5",
"keywords": [
"rajaongkir", "application programming interface", "laravel", "wrapper"
],
"license": "MIT",
"type": "package",
"authors": [
{
"name": "Agung Jati Kusumo",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"agungjk\\rajaongkir\\": "src/"
}
},
"require": {
"illuminate/support": "~5"
}
}
Binary file added cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions src/RajaOngkir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Agungjk\Rajaongkir;

class RajaOngkir {
protected $endpoint;
protected $key;
private $error;

public function __construct(){
$this->endpoint = config('rajaongkir.end_point_api', 'http://rajaongkir.com/api/starter');
$this->key = config('rajaongkir.api_key');
$this->city = json_decode(file_get_contents(__DIR__ . '/config/city.json'));
$this->province = json_decode(file_get_contents(__DIR__ . '/config/province.json'));
}

private function _request($path, $options = null)
{
$url = $this->endpoint . "/" . $path;

$curl = curl_init();
$config = array(
CURLOPT_URL => $url,
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: " . $this->key
),
);
$config = array_merge($config, $options);
curl_setopt_array($curl, $config);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
throw new Exception($err, 1);
}

if (! isset($response->rajaongkir)) {
$this->error = 'Response not valid';
return false;
}

$rajaongkir = $response->rajaongkir;

if ( $rajaongkir->status->code == 400 ) {
$this->error = $rajaongkir->status->description;
}

if ( $rajaongkir->status->code == 200 ) {
return $rajaongkir->results;
}
}

public function province($id = null)
{
if ($id = null) {
return empty($this->province) ? self::_request('/province') : $this->province;
}

if (empty($this->province)) {
return self::_request('/province?id=' . $id);
}

foreach ($this->province as $key => $value) {
if ($value->province_id == $id) {
return $value;
}
}

return null;
}

public function city($id = null)
{
if ($id == null) {
return empty($this->city) ? self::_request('/city') : $this->city;
}

if (empty($this->city)) {
return self::_request('/city?id=' . $id);
}

foreach ($this->city as $key => $value) {
if ($value->city_id == $id) {
return $value;
}
}

return null;
}

public function cost($origin, $destination, $weight, $courier)
{
$options = [
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "origin=". $origin ."&destination=". $destination ."&weight=". $weight ."&courier=". $courier,
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded",
"key: " . self::key
),
];
return self::_request('/cost', $options);
}

}
9 changes: 9 additions & 0 deletions src/RajaOngkirFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Agungjk\Rajaongkir;

use Illuminate\Support\Facades\Facade;

class RajaOngkirFacade extends Facade{
protected static function getFacadeAccessor() { return 'rajaOngkir'; }
}
34 changes: 34 additions & 0 deletions src/RajaOngkirServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Agungjk\Rajaongkir;

use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;

class RajaOngkirServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/config/rajaongkir.php' => config_path().'/rajaongkir.php',
]);
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
App::bind('rajaOngkir', function()
{
return new RajaOngkir;
});
}
}
Loading

0 comments on commit dd40203

Please sign in to comment.