1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Advanced RajaOngkir API PHP Library |
4
|
|
|
* |
5
|
|
|
* MIT License |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2018 Steeve Andrian Salim |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
* @author Steeve Andrian Salim |
28
|
|
|
* @copyright Copyright (c) 2018, Steeve Andrian Salim |
29
|
|
|
* @filesource |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
// ------------------------------------------------------------------------ |
33
|
|
|
|
34
|
|
|
namespace Steevenz; |
35
|
|
|
|
36
|
|
|
// ------------------------------------------------------------------------ |
37
|
|
|
|
38
|
|
|
use O2System\Curl; |
39
|
|
|
use O2System\Kernel\Http\Message\Uri; |
40
|
|
|
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Class Rajaongkir |
44
|
|
|
* @package Steevenz |
45
|
|
|
*/ |
46
|
|
|
class Rajaongkir { |
47
|
|
|
use ErrorCollectorTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constant Account Type |
51
|
|
|
* |
52
|
|
|
* @access public |
53
|
|
|
* @type string |
54
|
|
|
*/ |
55
|
|
|
const ACCOUNT_STARTER = 'starter'; |
56
|
|
|
const ACCOUNT_BASIC = 'basic'; |
57
|
|
|
const ACCOUNT_PRO = 'pro'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Rajaongkir::$accountType |
61
|
|
|
* |
62
|
|
|
* Rajaongkir Account Type. |
63
|
|
|
* |
64
|
|
|
* @access protected |
65
|
|
|
* @type string |
66
|
|
|
*/ |
67
|
|
|
protected $accountType = 'starter'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Rajaongkir::$apiKey |
71
|
|
|
* |
72
|
|
|
* Rajaongkir API key. |
73
|
|
|
* |
74
|
|
|
* @access protected |
75
|
|
|
* @type string |
76
|
|
|
*/ |
77
|
|
|
protected $apiKey = null; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* List of Supported Account Types |
81
|
|
|
* |
82
|
|
|
* @access protected |
83
|
|
|
* @type array |
84
|
|
|
*/ |
85
|
|
|
protected $supportedAccountTypes = [ |
86
|
|
|
'starter', |
87
|
|
|
'basic', |
88
|
|
|
'pro', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Supported Couriers |
93
|
|
|
* |
94
|
|
|
* @access protected |
95
|
|
|
* @type array |
96
|
|
|
*/ |
97
|
|
|
protected $supportedCouriers = [ |
98
|
|
|
'starter' => [ |
99
|
|
|
'jne', |
100
|
|
|
'pos', |
101
|
|
|
'tiki', |
102
|
|
|
], |
103
|
|
|
'basic' => [ |
104
|
|
|
'jne', |
105
|
|
|
'pos', |
106
|
|
|
'tiki', |
107
|
|
|
'pcp', |
108
|
|
|
'esl', |
109
|
|
|
'rpx', |
110
|
|
|
], |
111
|
|
|
'pro' => [ |
112
|
|
|
'jne', |
113
|
|
|
'pos', |
114
|
|
|
'tiki', |
115
|
|
|
'rpx', |
116
|
|
|
'pandu', |
117
|
|
|
'wahana', |
118
|
|
|
'sicepat', |
119
|
|
|
'jnt', |
120
|
|
|
'pahala', |
121
|
|
|
'sap', |
122
|
|
|
'jet', |
123
|
|
|
'indah', |
124
|
|
|
'dse', |
125
|
|
|
'slis', |
126
|
|
|
'first', |
127
|
|
|
'ncs', |
128
|
|
|
'star', |
129
|
|
|
'ninja', |
130
|
|
|
'lion', |
131
|
|
|
'idl', |
132
|
|
|
'rex', |
133
|
|
|
'ide', |
134
|
|
|
'sentral' |
135
|
|
|
], |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Rajaongkir::$supportedWaybills |
140
|
|
|
* |
141
|
|
|
* Rajaongkir supported couriers waybills. |
142
|
|
|
* |
143
|
|
|
* @access protected |
144
|
|
|
* @type array |
145
|
|
|
*/ |
146
|
|
|
protected $supportedWayBills = [ |
147
|
|
|
'starter' => [], |
148
|
|
|
'basic' => [ |
149
|
|
|
'jne', |
150
|
|
|
], |
151
|
|
|
'pro' => [ |
152
|
|
|
'jne', |
153
|
|
|
'pos', |
154
|
|
|
'tiki', |
155
|
|
|
'pcp', |
156
|
|
|
'rpx', |
157
|
|
|
'wahana', |
158
|
|
|
'sicepat', |
159
|
|
|
'j&t', |
160
|
|
|
'sap', |
161
|
|
|
'jet', |
162
|
|
|
'dse', |
163
|
|
|
'first', |
164
|
|
|
], |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Rajaongkir::$couriersList |
169
|
|
|
* |
170
|
|
|
* Rajaongkir courier list. |
171
|
|
|
* |
172
|
|
|
* @access protected |
173
|
|
|
* @type array |
174
|
|
|
*/ |
175
|
|
|
protected $couriersList = [ |
176
|
|
|
'jne' => 'Jalur Nugraha Ekakurir (JNE)', |
177
|
|
|
'pos' => 'POS Indonesia (POS)', |
178
|
|
|
'tiki' => 'Citra Van Titipan Kilat (TIKI)', |
179
|
|
|
'pcp' => 'Priority Cargo and Package (PCP)', |
180
|
|
|
'esl' => 'Eka Sari Lorena (ESL)', |
181
|
|
|
'rpx' => 'RPX Holding (RPX)', |
182
|
|
|
'pandu' => 'Pandu Logistics (PANDU)', |
183
|
|
|
'wahana' => 'Wahana Prestasi Logistik (WAHANA)', |
184
|
|
|
'sicepat' => 'SiCepat Express (SICEPAT)', |
185
|
|
|
'j&t' => 'J&T Express (J&T)', |
186
|
|
|
'pahala' => 'Pahala Kencana Express (PAHALA)', |
187
|
|
|
'cahaya' => 'Cahaya Logistik (CAHAYA)', |
188
|
|
|
'sap' => 'SAP Express (SAP)', |
189
|
|
|
'jet' => 'JET Express (JET)', |
190
|
|
|
'indah' => 'Indah Logistic (INDAH)', |
191
|
|
|
'slis' => 'Solusi Express (SLIS)', |
192
|
|
|
'expedito*' => 'Expedito*', |
193
|
|
|
'dse' => '21 Express (DSE)', |
194
|
|
|
'first' => 'First Logistics (FIRST)', |
195
|
|
|
'ncs' => 'Nusantara Card Semesta (NCS)', |
196
|
|
|
'star' => 'Star Cargo (STAR)', |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Rajaongkir::$response |
201
|
|
|
* |
202
|
|
|
* Rajaongkir response. |
203
|
|
|
* |
204
|
|
|
* @access protected |
205
|
|
|
* @type mixed |
206
|
|
|
*/ |
207
|
|
|
protected $response; |
208
|
|
|
|
209
|
|
|
// ------------------------------------------------------------------------ |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Rajaongkir::__construct |
213
|
|
|
* |
214
|
|
|
* @access public |
215
|
|
|
* @throws \InvalidArgumentException |
216
|
|
|
*/ |
217
|
|
|
public function __construct( $apiKey = null, $accountType = null ) { |
218
|
|
|
if ( isset( $apiKey ) ) { |
219
|
|
|
if ( is_array( $apiKey ) ) { |
220
|
|
|
if ( isset( $apiKey['api_key'] ) ) { |
221
|
|
|
$this->apiKey = $apiKey['api_key']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ( isset( $apiKey['account_type'] ) ) { |
225
|
|
|
$accountType = $apiKey['account_type']; |
226
|
|
|
} |
227
|
|
|
} elseif ( is_string( $apiKey ) ) { |
228
|
|
|
$this->apiKey = $apiKey; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ( isset( $accountType ) ) { |
233
|
|
|
$this->setAccountType( $accountType ); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// ------------------------------------------------------------------------ |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Rajaongkir::setApiKey |
241
|
|
|
* |
242
|
|
|
* Set Rajaongkir API Key. |
243
|
|
|
* |
244
|
|
|
* @param string $apiKey Rajaongkir API Key |
245
|
|
|
* |
246
|
|
|
* @access public |
247
|
|
|
* @return static |
248
|
|
|
*/ |
249
|
|
|
public function setApiKey( $apiKey ) { |
250
|
|
|
$this->apiKey = $apiKey; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// ------------------------------------------------------------------------ |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Rajaongkir::setAccountType |
259
|
|
|
* |
260
|
|
|
* Set Rajaongkir account type. |
261
|
|
|
* |
262
|
|
|
* @param string $accountType RajaOngkir Account Type, can be starter, basic or pro |
263
|
|
|
* |
264
|
|
|
* @access public |
265
|
|
|
* @return static |
266
|
|
|
* @throws \InvalidArgumentException |
267
|
|
|
*/ |
268
|
|
|
public function setAccountType( $accountType ) { |
269
|
|
|
$accountType = strtolower( $accountType ); |
270
|
|
|
|
271
|
|
|
if ( in_array( $accountType, $this->supportedAccountTypes ) ) { |
272
|
|
|
$this->accountType = $accountType; |
273
|
|
|
} else { |
274
|
|
|
throw new \InvalidArgumentException( 'Rajaongkir: Invalid Account Type' ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// ------------------------------------------------------------------------ |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Rajaongkir::request |
284
|
|
|
* |
285
|
|
|
* Curl request API caller. |
286
|
|
|
* |
287
|
|
|
* @param string $path |
288
|
|
|
* @param array $params |
289
|
|
|
* @param string $type |
290
|
|
|
* |
291
|
|
|
* @access protected |
292
|
|
|
* @return array|bool Returns FALSE if failed. |
293
|
|
|
*/ |
294
|
|
|
protected function request( $path, $params = [], $type = 'GET' ) { |
295
|
|
|
$apiUrl = 'https://api.rajaongkir.com'; |
296
|
|
|
|
297
|
|
|
switch ( $this->accountType ) { |
298
|
|
|
default: |
299
|
|
|
case 'starter': |
300
|
|
|
$path = 'starter/' . $path; |
301
|
|
|
break; |
302
|
|
|
|
303
|
|
|
case 'basic': |
304
|
|
|
$path = 'basic/' . $path; |
305
|
|
|
break; |
306
|
|
|
|
307
|
|
|
case 'pro': |
308
|
|
|
$apiUrl = 'https://pro.rajaongkir.com'; |
309
|
|
|
$path = 'api/' . $path; |
310
|
|
|
break; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
$uri = ( new Uri( $apiUrl ) )->withPath( $path ); |
314
|
|
|
$request = new Curl\Request(); |
315
|
|
|
$request->setHeaders( [ |
316
|
|
|
'key' => $this->apiKey, |
317
|
|
|
] ); |
318
|
|
|
|
319
|
|
|
switch ( $type ) { |
320
|
|
|
default: |
321
|
|
|
case 'GET': |
322
|
|
|
$this->response = $request->setUri( $uri )->get( $params ); |
323
|
|
|
break; |
324
|
|
|
|
325
|
|
|
case 'POST': |
326
|
|
|
$request->addHeader( 'content-type', 'application/x-www-form-urlencoded' ); |
327
|
|
|
$this->response = $request->setUri( $uri )->post( $params ); |
328
|
|
|
break; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// Try to get curl error |
332
|
|
|
if ( false !== ( $error = $this->response->getError() ) ) { |
|
|
|
|
333
|
|
|
$this->addErrors( $error->getArrayCopy() ); |
334
|
|
|
} else { |
335
|
|
|
$body = $this->response->getBody(); |
336
|
|
|
|
337
|
|
|
if ( $body instanceof \DOMDocument ) { |
338
|
|
|
$this->errors[404] = 'Page Not Found!'; |
339
|
|
|
} else { |
340
|
|
|
$body = $body->rajaongkir; |
341
|
|
|
$status = $body['status']; |
342
|
|
|
|
343
|
|
|
if ( $status['code'] == 200 ) { |
344
|
|
|
if ( isset( $body['results'] ) ) { |
345
|
|
|
if ( count( $body['results'] ) == 1 && isset( $body['results'][0] ) ) { |
346
|
|
|
return $body['results'][0]; |
347
|
|
|
} elseif ( count( $body['results'] ) ) { |
348
|
|
|
return $body['results']; |
349
|
|
|
} |
350
|
|
|
} elseif ( isset( $body['result'] ) ) { |
351
|
|
|
return $body['result']; |
352
|
|
|
} |
353
|
|
|
} else { |
354
|
|
|
$this->errors[ $status['code'] ] = $status['description']; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return false; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// ------------------------------------------------------------------------ |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Rajaongkir::getCouriersList |
366
|
|
|
* |
367
|
|
|
* Get list of supported couriers. |
368
|
|
|
* |
369
|
|
|
* @access public |
370
|
|
|
* @return array|bool Returns FALSE if failed. |
371
|
|
|
*/ |
372
|
|
|
public function getCouriersList() { |
373
|
|
|
return $this->couriersList; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
// ------------------------------------------------------------------------ |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Rajaongkir::getProvinces |
380
|
|
|
* |
381
|
|
|
* Get list of provinces. |
382
|
|
|
* |
383
|
|
|
* @access public |
384
|
|
|
* @return array|bool Returns FALSE if failed. |
385
|
|
|
*/ |
386
|
|
|
public function getProvinces() { |
387
|
|
|
return $this->request( 'province' ); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
// ------------------------------------------------------------------------ |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Rajaongkir::getProvince |
394
|
|
|
* |
395
|
|
|
* Get detail of single province. |
396
|
|
|
* |
397
|
|
|
* @param int $idProvince Province ID |
398
|
|
|
* |
399
|
|
|
* @access public |
400
|
|
|
* @return array|bool Returns FALSE if failed. |
401
|
|
|
*/ |
402
|
|
|
public function getProvince( $idProvince ) { |
403
|
|
|
return $this->request( 'province', [ 'id' => $idProvince ] ); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
// ------------------------------------------------------------------------ |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Rajaongkir::getCities |
410
|
|
|
* |
411
|
|
|
* Get list of province cities. |
412
|
|
|
* |
413
|
|
|
* @param int $idProvince Province ID |
414
|
|
|
* |
415
|
|
|
* @access public |
416
|
|
|
* @return array|bool Returns FALSE if failed. |
417
|
|
|
*/ |
418
|
|
|
public function getCities( $idProvince = null ) { |
419
|
|
|
$params = []; |
420
|
|
|
|
421
|
|
|
if ( ! is_null( $idProvince ) ) { |
422
|
|
|
$params['province'] = $idProvince; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $this->request( 'city', $params ); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
// ------------------------------------------------------------------------ |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Rajaongkir::getCity |
432
|
|
|
* |
433
|
|
|
* Get detail of single city. |
434
|
|
|
* |
435
|
|
|
* @param int $idCity City ID |
436
|
|
|
* |
437
|
|
|
* @access public |
438
|
|
|
* @return array|bool Returns FALSE if failed. |
439
|
|
|
*/ |
440
|
|
|
public function getCity( $idCity ) { |
441
|
|
|
return $this->request( 'city', [ 'id' => $idCity ] ); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
// ------------------------------------------------------------------------ |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Rajaongkir::getSubdistricts |
448
|
|
|
* |
449
|
|
|
* Get list of city subdisctricts. |
450
|
|
|
* |
451
|
|
|
* @param int $idCity City ID |
452
|
|
|
* |
453
|
|
|
* @access public |
454
|
|
|
* @return array|bool Returns FALSE if failed. |
455
|
|
|
*/ |
456
|
|
|
public function getSubdistricts( $idCity ) { |
457
|
|
|
if ( $this->accountType === 'starter' ) { |
458
|
|
|
$this->errors[302] = 'Unsupported Subdistricts Request. Tipe akun starter tidak mendukung hingga tingkat kecamatan.'; |
459
|
|
|
|
460
|
|
|
return false; |
461
|
|
|
} elseif ( $this->accountType === 'basic' ) { |
462
|
|
|
$this->errors[302] = 'Unsupported Subdistricts Request. Tipe akun basic tidak mendukung hingga tingkat kecamatan.'; |
463
|
|
|
|
464
|
|
|
return false; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
return $this->request( 'subdistrict', [ 'city' => $idCity ] ); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
// ------------------------------------------------------------------------ |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Rajaongkir::getSubdistrict |
474
|
|
|
* |
475
|
|
|
* Get detail of single subdistrict. |
476
|
|
|
* |
477
|
|
|
* @param int $idSubdistrict Subdistrict ID |
478
|
|
|
* |
479
|
|
|
* @access public |
480
|
|
|
* @return array|bool Returns FALSE if failed. |
481
|
|
|
*/ |
482
|
|
|
public function getSubdistrict( $idSubdistrict ) { |
483
|
|
|
if ( $this->accountType === 'starter' ) { |
484
|
|
|
$this->errors[302] = 'Unsupported Subdistricts Request. Tipe akun starter tidak mendukung hingga tingkat kecamatan.'; |
485
|
|
|
|
486
|
|
|
return false; |
487
|
|
|
} elseif ( $this->accountType === 'basic' ) { |
488
|
|
|
$this->errors[302] = 'Unsupported Subdistricts Request. Tipe akun basic tidak mendukung hingga tingkat kecamatan.'; |
489
|
|
|
|
490
|
|
|
return false; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
return $this->request( 'subdistrict', [ 'id' => $idSubdistrict ] ); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
// ------------------------------------------------------------------------ |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Rajaongkir::getInternationalOrigins |
500
|
|
|
* |
501
|
|
|
* Get list of supported international origins. |
502
|
|
|
* |
503
|
|
|
* @param int $idProvince Province ID |
504
|
|
|
* |
505
|
|
|
* @access public |
506
|
|
|
* @return array|bool Returns FALSE if failed. |
507
|
|
|
*/ |
508
|
|
|
public function getInternationalOrigins( $idProvince = null ) { |
509
|
|
|
if ( $this->accountType === 'starter' ) { |
510
|
|
|
$this->errors[301] = 'Unsupported International Origin Request. Tipe akun starter tidak mendukung tingkat international.'; |
511
|
|
|
|
512
|
|
|
return false; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
$params = []; |
516
|
|
|
|
517
|
|
|
if ( isset( $idProvince ) ) { |
518
|
|
|
$params['province'] = $idProvince; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
return $this->request( 'v2/internationalOrigin', $params ); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
// ------------------------------------------------------------------------ |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Rajaongkir::getInternationalOrigin |
528
|
|
|
* |
529
|
|
|
* Get list of supported international origins by city and province. |
530
|
|
|
* |
531
|
|
|
* @param int $idCity City ID |
532
|
|
|
* @param int $idProvince Province ID |
533
|
|
|
* |
534
|
|
|
* @access public |
535
|
|
|
* @return array|bool Returns FALSE if failed. |
536
|
|
|
*/ |
537
|
|
|
public function getInternationalOrigin( $idCity = null, $idProvince = null ) { |
538
|
|
|
if ( $this->accountType === 'starter' ) { |
539
|
|
|
$this->errors[301] = 'Unsupported International Origin Request. Tipe akun starter tidak mendukung tingkat international.'; |
540
|
|
|
|
541
|
|
|
return false; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
if ( isset( $idCity ) ) { |
545
|
|
|
$params['id'] = $idCity; |
|
|
|
|
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
if ( isset( $idProvince ) ) { |
549
|
|
|
$params['province'] = $idProvince; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
return $this->request( 'v2/internationalOrigin', $params ); |
|
|
|
|
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
// ------------------------------------------------------------------------ |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Rajaongkir::getInternationalDestinations |
559
|
|
|
* |
560
|
|
|
* Get list of international destinations. |
561
|
|
|
* |
562
|
|
|
* @param int $id_country Country ID |
563
|
|
|
* |
564
|
|
|
* @access public |
565
|
|
|
* @return array|bool Returns FALSE if failed. |
566
|
|
|
*/ |
567
|
|
|
public function getInternationalDestinations() { |
568
|
|
|
if ( $this->accountType === 'starter' ) { |
569
|
|
|
$this->errors[301] = 'Unsupported International Destination Request. Tipe akun starter tidak mendukung tingkat international.'; |
570
|
|
|
|
571
|
|
|
return false; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
return $this->request( 'v2/internationalDestination' ); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
// ------------------------------------------------------------------------ |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Rajaongkir::getInternationalDestination |
581
|
|
|
* |
582
|
|
|
* Get International Destination |
583
|
|
|
* |
584
|
|
|
* @param int $idCountry Country ID |
585
|
|
|
* |
586
|
|
|
* @access public |
587
|
|
|
* @return array|bool Returns FALSE if failed. |
588
|
|
|
*/ |
589
|
|
|
public function getInternationalDestination( $idCountry = null ) { |
590
|
|
|
if ( $this->accountType === 'starter' ) { |
591
|
|
|
$this->errors[301] = 'Unsupported International Destination Request. Tipe akun starter tidak mendukung tingkat international.'; |
592
|
|
|
|
593
|
|
|
return false; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
$params = []; |
597
|
|
|
|
598
|
|
|
if ( isset( $idCountry ) ) { |
599
|
|
|
$params['id'] = $idCountry; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
return $this->request( 'v2/internationalDestination', $params ); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
// ------------------------------------------------------------------------ |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Rajaongkir::getCost |
609
|
|
|
* |
610
|
|
|
* Get cost calculation. |
611
|
|
|
* |
612
|
|
|
* @param array $origin City, District or Subdistrict Origin |
613
|
|
|
* @param array $destination City, District or Subdistrict Destination |
614
|
|
|
* @param array $metrics Array of Specification |
615
|
|
|
* weight int weight in gram (required) |
616
|
|
|
* length number package length dimension |
617
|
|
|
* width number package width dimension |
618
|
|
|
* height number package height dimension |
619
|
|
|
* diameter number package diameter |
620
|
|
|
* @param string $courier Courier Code |
621
|
|
|
* |
622
|
|
|
* @access public |
623
|
|
|
* @return array|bool Returns FALSE if failed. |
624
|
|
|
* @see http://rajaongkir.com/dokumentasi/pro |
625
|
|
|
* |
626
|
|
|
* @example |
627
|
|
|
* $rajaongkir->getCost( |
628
|
|
|
* ['city' => 1], |
629
|
|
|
* ['subdistrict' => 12], |
630
|
|
|
* ['weight' => 100, 'length' => 100, 'width' => 100, 'height' => 100, 'diameter' => 100], |
631
|
|
|
* 'jne' |
632
|
|
|
* ); |
633
|
|
|
* |
634
|
|
|
*/ |
635
|
|
|
public function getCost( array $origin, array $destination, $metrics, $courier ) { |
636
|
|
|
$params['courier'] = strtolower( $courier ); |
|
|
|
|
637
|
|
|
|
638
|
|
|
$params['originType'] = strtolower( key( $origin ) ); |
639
|
|
|
$params['destinationType'] = strtolower( key( $destination ) ); |
640
|
|
|
|
641
|
|
|
if ( $params['originType'] !== 'city' ) { |
642
|
|
|
$params['originType'] = 'subdistrict'; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
if ( ! in_array( $params['destinationType'], [ 'city', 'country' ] ) ) { |
646
|
|
|
$params['destinationType'] = 'subdistrict'; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
if ( is_array( $metrics ) ) { |
|
|
|
|
650
|
|
|
if ( ! isset( $metrics['weight'] ) and |
651
|
|
|
isset( $metrics['length'] ) and |
652
|
|
|
isset( $metrics['width'] ) and |
653
|
|
|
isset( $metrics['height'] ) |
654
|
|
|
) { |
655
|
|
|
$metrics['weight'] = ( ( $metrics['length'] * $metrics['width'] * $metrics['height'] ) / 6000 ) * 1000; |
656
|
|
|
} elseif ( isset( $metrics['weight'] ) and |
657
|
|
|
isset( $metrics['length'] ) and |
658
|
|
|
isset( $metrics['width'] ) and |
659
|
|
|
isset( $metrics['height'] ) |
660
|
|
|
) { |
661
|
|
|
$weight = ( ( $metrics['length'] * $metrics['width'] * $metrics['height'] ) / 6000 ) * 1000; |
662
|
|
|
|
663
|
|
|
if ( $weight > $metrics['weight'] ) { |
664
|
|
|
$metrics['weight'] = $weight; |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
foreach ( $metrics as $key => $value ) { |
669
|
|
|
$params[ $key ] = $value; |
670
|
|
|
} |
671
|
|
|
} elseif ( is_numeric( $metrics ) ) { |
672
|
|
|
$params['weight'] = $metrics; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
switch ( $this->accountType ) { |
676
|
|
|
case 'starter': |
677
|
|
|
|
678
|
|
|
if ( $params['destinationType'] === 'country' ) { |
679
|
|
|
$this->errors[301] = 'Unsupported International Destination. Tipe akun starter tidak mendukung pengecekan destinasi international.'; |
680
|
|
|
|
681
|
|
|
return false; |
682
|
|
|
} elseif ( $params['originType'] === 'subdistrict' or $params['destinationType'] === 'subdistrict' ) { |
683
|
|
|
$this->errors[302] = 'Unsupported Subdistrict Origin-Destination. Tipe akun starter tidak mendukung pengecekan ongkos kirim sampai kecamatan.'; |
684
|
|
|
|
685
|
|
|
return false; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
if ( ! isset( $params['weight'] ) and |
689
|
|
|
isset( $params['length'] ) and |
690
|
|
|
isset( $params['width'] ) and |
691
|
|
|
isset( $params['height'] ) |
692
|
|
|
) { |
693
|
|
|
$this->errors[304] = 'Unsupported Dimension. Tipe akun starter tidak mendukung pengecekan biaya kirim berdasarkan dimensi.'; |
694
|
|
|
|
695
|
|
|
return false; |
696
|
|
|
} elseif ( isset( $params['weight'] ) and $params['weight'] > 30000 ) { |
697
|
|
|
$this->errors[305] = 'Unsupported Weight. Tipe akun starter tidak mendukung pengecekan biaya kirim dengan berat lebih dari 30000 gram (30kg).'; |
698
|
|
|
|
699
|
|
|
return false; |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
if ( ! in_array( $params['courier'], $this->supportedCouriers[ $this->accountType ] ) ) { |
703
|
|
|
$this->errors[303] = 'Unsupported Courier. Tipe akun starter tidak mendukung pengecekan biaya kirim dengan kurir ' . $this->couriersList[ $courier ] . '.'; |
704
|
|
|
|
705
|
|
|
return false; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
break; |
709
|
|
|
|
710
|
|
|
case 'basic': |
711
|
|
|
|
712
|
|
|
if ( $params['originType'] === 'subdistrict' or $params['destinationType'] === 'subdistrict' ) { |
713
|
|
|
$this->errors[302] = 'Unsupported Subdistrict Origin-Destination. Tipe akun basic tidak mendukung pengecekan ongkos kirim sampai kecamatan.'; |
714
|
|
|
|
715
|
|
|
return false; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
if ( ! isset( $params['weight'] ) and |
719
|
|
|
isset( $params['length'] ) and |
720
|
|
|
isset( $params['width'] ) and |
721
|
|
|
isset( $params['height'] ) |
722
|
|
|
) { |
723
|
|
|
$this->errors[304] = 'Unsupported Dimension. Tipe akun basic tidak mendukung pengecekan biaya kirim berdasarkan dimensi.'; |
724
|
|
|
|
725
|
|
|
return false; |
726
|
|
|
} elseif ( isset( $params['weight'] ) and $params['weight'] > 30000 ) { |
727
|
|
|
$this->errors[305] = 'Unsupported Weight. Tipe akun basic tidak mendukung pengecekan biaya kirim dengan berat lebih dari 30000 gram (30kg).'; |
728
|
|
|
|
729
|
|
|
return false; |
730
|
|
|
} elseif ( isset( $params['weight'] ) and $params['weight'] < 30000 ) { |
731
|
|
|
unset( $params['length'], $params['width'], $params['height'] ); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
if ( ! in_array( $params['courier'], $this->supportedCouriers[ $this->accountType ] ) ) { |
735
|
|
|
$this->errors[303] = 'Unsupported Courier. Tipe akun basic tidak mendukung pengecekan biaya kirim dengan kurir ' . $this->couriersList[ $courier ] . '.'; |
736
|
|
|
|
737
|
|
|
return false; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
break; |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
$params['origin'] = $origin[ key( $origin ) ]; |
744
|
|
|
$params['destination'] = $destination[ key( $destination ) ]; |
745
|
|
|
|
746
|
|
|
$path = key( $destination ) === 'country' ? 'internationalCost' : 'cost'; |
747
|
|
|
|
748
|
|
|
return $this->request( $path, $params, 'POST' ); |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
// ------------------------------------------------------------------------ |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Rajaongkir::getWaybill |
755
|
|
|
* |
756
|
|
|
* Get detail of waybill. |
757
|
|
|
* |
758
|
|
|
* @param int $idWaybill Receipt ID |
759
|
|
|
* @param null|string $courier Courier Code |
760
|
|
|
* |
761
|
|
|
* @access public |
762
|
|
|
* @return array|bool Returns FALSE if failed. |
763
|
|
|
*/ |
764
|
|
|
public function getWaybill( $idWaybill, $courier ) { |
765
|
|
|
$courier = strtolower( $courier ); |
766
|
|
|
|
767
|
|
|
if ( in_array( $courier, $this->supportedWayBills[ $this->accountType ] ) ) { |
768
|
|
|
return $this->request( 'waybill', [ |
769
|
|
|
'key' => $this->apiKey, |
770
|
|
|
'waybill' => $idWaybill, |
771
|
|
|
'courier' => $courier, |
772
|
|
|
], 'POST' ); |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
return false; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
// ------------------------------------------------------------------------ |
779
|
|
|
|
780
|
|
|
/** |
781
|
|
|
* Rajaongkir::getCurrency |
782
|
|
|
* |
783
|
|
|
* Get Rajaongkir currency. |
784
|
|
|
* |
785
|
|
|
* @access public |
786
|
|
|
* @return array|bool Returns FALSE if failed. |
787
|
|
|
*/ |
788
|
|
|
public function getCurrency() { |
789
|
|
|
if ( $this->accountType !== 'starter' ) { |
790
|
|
|
return $this->request( 'currency' ); |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
$this->errors[301] = 'Unsupported Get Currency. Tipe akun starter tidak mendukung pengecekan currency.'; |
794
|
|
|
|
795
|
|
|
return false; |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
// ------------------------------------------------------------------------ |
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* Rajaongkir::getSupportedCouriers |
802
|
|
|
* |
803
|
|
|
* Gets list of supported couriers by your account. |
804
|
|
|
* |
805
|
|
|
* @return array|bool Returns FALSE if failed. |
806
|
|
|
*/ |
807
|
|
|
public function getSupportedCouriers() { |
808
|
|
|
if ( isset( $this->supportedCouriers[ $this->accountType ] ) ) { |
809
|
|
|
return $this->supportedCouriers[ $this->accountType ]; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
return false; |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
// ------------------------------------------------------------------------ |
816
|
|
|
|
817
|
|
|
/** |
818
|
|
|
* Rajaongkir::getSupportedWayBills |
819
|
|
|
* |
820
|
|
|
* Gets list of supported way bills based on account type. |
821
|
|
|
* |
822
|
|
|
* @return array|bool Returns FALSE if failed. |
823
|
|
|
*/ |
824
|
|
|
public function getSupportedWayBills() { |
825
|
|
|
if ( isset( $this->supportedWayBills[ $this->accountType ] ) ) { |
826
|
|
|
return $this->supportedWayBills[ $this->accountType ]; |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
return false; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
// ------------------------------------------------------------------------ |
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* Rajaongkir::getResponse |
836
|
|
|
* |
837
|
|
|
* Get original response object. |
838
|
|
|
* |
839
|
|
|
* @param string $offset Response Offset Object |
840
|
|
|
* |
841
|
|
|
* @access public |
842
|
|
|
* @return \O2System\Curl\Response|bool Returns FALSE if failed. |
843
|
|
|
*/ |
844
|
|
|
public function getResponse() { |
845
|
|
|
return $this->response; |
846
|
|
|
} |
847
|
|
|
} |
848
|
|
|
|