-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from IlhamriSKY/master
Menambah option ssl untuk endpoint api & Fix Typos
- Loading branch information
Showing
3 changed files
with
56 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,12 @@ composer require lintangtimur/cek-pajak-api | |
use Stelin\CekPajakApi\CekPajak; | ||
|
||
$form = [ | ||
"name"=> 'lintang', | ||
'name'=> 'lintang', | ||
'email'=> '[email protected]', | ||
'password'=>'123456' | ||
]; | ||
|
||
$reg = new CekPajak() | ||
$reg = new CekPajak(); | ||
$reg = (new CekPajak)->register($form); | ||
``` | ||
|
||
|
@@ -36,12 +36,12 @@ $reg = (new CekPajak)->register($form); | |
use Stelin\CekPajakApi\CekPajak; | ||
|
||
$form = [ | ||
"name"=> 'lintang', | ||
'name'=> 'lintang', | ||
'email'=> '[email protected]', | ||
'password'=>'123456' | ||
]; | ||
|
||
$login = new CekPajak() | ||
$login = new CekPajak(); | ||
$login = (new CekPajak)->login($form)->accessToken; | ||
``` | ||
|
||
|
@@ -50,9 +50,25 @@ $login = (new CekPajak)->login($form)->accessToken; | |
<?php | ||
use Stelin\CekPajakApi\CekPajak; | ||
|
||
$cp = new CekPajak($accessToken) | ||
$cp = new CekPajak($accessToken); | ||
$cp->cekPajak('H','1234','AA')->totalPkbPokok; | ||
|
||
//Melihat rincian akhir pajak | ||
$a->cekPajak('H','1234','AA')->rincian->masaAkhirBerlakuPajak; | ||
``` | ||
|
||
#### Contoh menggunakan SSL | ||
Menambah option penggunaan ssl untuk endpoint api untuk user yang mengalami error ```SSL certificate problem``` | ||
```php | ||
$form = [ | ||
'name'=> 'lintang', | ||
'email'=> '[email protected]', | ||
'password'=>'123456' | ||
]; | ||
|
||
$login = new CekPajak(); | ||
$accessToken = $login->ssl(true)->login($form)->accessToken; //registrasi terlebih dahulu sebelum login | ||
$accsess = new CekPajak($accessToken); | ||
$total_pokok = $accsess->ssl(true)->cekPajak('H','1234','AA')->totalPkbPokok; | ||
echo $total_pokok; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,17 +10,33 @@ | |
* Main class. | ||
* | ||
* @author lintangtimur <[email protected]> | ||
* @api 1.0 | ||
* @api 1.1.0 | ||
*/ | ||
class CekPajak | ||
{ | ||
private $token; | ||
private $ssl = false; | ||
|
||
public function __construct($token = null) | ||
{ | ||
$this->token = $token; | ||
} | ||
|
||
/** | ||
* generate url. | ||
* | ||
* @param bool $ssl | ||
* @return bool | ||
*/ | ||
public function ssl(bool $ssl) | ||
{ | ||
if ($ssl == 'true') { | ||
$this->ssl = true; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* cek pajak. | ||
* | ||
|
@@ -31,10 +47,11 @@ public function __construct($token = null) | |
*/ | ||
public function cekPajak(string $kode_wilayah, string $nomor, string $sub_wilayah) | ||
{ | ||
$client = new Client(); | ||
$baseurl = 'http' . ($this->ssl ? 's' : '') . '://' . Meta::BASE_URL; | ||
$client = new Client(); | ||
|
||
try { | ||
$res = $client->post(Meta::BASE_URL . 'api/check_pajak', [ | ||
$res = $client->post($baseurl . 'api/check_pajak', [ | ||
'headers' => ['Authorization' => 'Bearer ' . $this->token], | ||
'json' => [ | ||
'na' => $kode_wilayah, | ||
|
@@ -47,7 +64,6 @@ public function cekPajak(string $kode_wilayah, string $nomor, string $sub_wilaya | |
$data = json_decode($res->getBody()); | ||
|
||
return (new PajakResponse())->fromJson($data); | ||
|
||
} catch (\GuzzleHttp\Exception\ClientException $e) { | ||
$response = $e->getResponse(); | ||
|
||
|
@@ -67,7 +83,7 @@ public function cekPajak(string $kode_wilayah, string $nomor, string $sub_wilaya | |
* @param string $sub_wilayah | ||
* @return string | ||
*/ | ||
private function generateSignature(string $kode_wilayah, string $nomor, string $sub_wilayah) | ||
private function generateSignature(string $kode_wilayah, string $nomor, string $sub_wilayah): string | ||
{ | ||
return hash_hmac(Meta::ALGO, $kode_wilayah . $nomor . $sub_wilayah, Meta::SIGNKEY); | ||
} | ||
|
@@ -78,24 +94,21 @@ private function generateSignature(string $kode_wilayah, string $nomor, string $ | |
* @param array $formRegister | ||
* @return void|string | ||
*/ | ||
public function register(array $formRegister) | ||
public function register(array $formRegister): ?string | ||
{ | ||
$client = new Client(); | ||
$baseurl = 'http' . ($this->ssl ? 's' : '') . '://' . Meta::BASE_URL; | ||
$client = new Client(); | ||
|
||
try { | ||
$res = $client->post(Meta::BASE_URL . 'api/auth/register', [ | ||
$res = $client->post($baseurl . 'api/auth/register', [ | ||
'json' => $formRegister, | ||
])->withHeader('accept', 'application/json'); | ||
|
||
return $res->getBody(); | ||
} catch (\GuzzleHttp\Exception\ClientException $e) { | ||
$response = $e->getResponse(); | ||
|
||
return $response->getBody(); | ||
} catch (\GuzzleHttp\Exception\ServerException $e) { | ||
} catch (\GuzzleHttp\Exception\RequestException $e) { | ||
$response = $e->getResponse(); | ||
|
||
return $response->getBody(); | ||
return $response ? $response->getBody() : ''; | ||
} | ||
} | ||
|
||
|
@@ -105,29 +118,21 @@ public function register(array $formRegister) | |
* @param array $formLogin | ||
* @return LoginEntity | ||
*/ | ||
public function login(array $formLogin) | ||
public function login(array $formLogin): LoginEntity | ||
{ | ||
$client = new Client(); | ||
$baseurl = 'http' . ($this->ssl ? 's' : '') . '://' . Meta::BASE_URL; | ||
$client = new Client(); | ||
|
||
try { | ||
$res = $client->post(Meta::BASE_URL . 'api/auth/login', [ | ||
$res = $client->post($baseurl . 'api/auth/login', [ | ||
'json' => $formLogin, | ||
])->withHeader('accept', 'application/json'); | ||
|
||
$data = json_decode($res->getBody()); | ||
|
||
return (new LoginEntity())->fromJson($data); | ||
|
||
} catch (\GuzzleHttp\Exception\ClientException $e) { | ||
$response = $e->getResponse(); | ||
header('Content-Type: application/json'); | ||
|
||
return $response->getBody(); | ||
} catch (\GuzzleHttp\Exception\ServerException $e) { | ||
return (new LoginEntity())->fromJson(json_decode($res->getBody())); | ||
} catch (\GuzzleHttp\Exception\RequestException $e) { | ||
$response = $e->getResponse(); | ||
header('Content-Type: application/json'); | ||
|
||
return $response->getBody(); | ||
return $response ? $response->getBody() : ''; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters