Skip to content

Commit

Permalink
Merge pull request #1 from IlhamriSKY/master
Browse files Browse the repository at this point in the history
Menambah option ssl untuk endpoint api & Fix Typos
  • Loading branch information
lintangtimur authored Apr 28, 2023
2 parents bbc6834 + ecfdef2 commit 677c877
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

Expand All @@ -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;
```

Expand All @@ -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;
```
63 changes: 34 additions & 29 deletions src/CekPajak.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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,
Expand All @@ -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();

Expand All @@ -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);
}
Expand All @@ -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() : '';
}
}

Expand All @@ -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() : '';
}
}
}
2 changes: 1 addition & 1 deletion src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Meta
/**
* Sementara backend belum siap, masih di local.
*/
const BASE_URL = 'https://stelin.xyz/';
const BASE_URL = 'stelin.xyz/';
const SIGNKEY = 'lintangtimur';
const ALGO = 'sha256';
}

0 comments on commit 677c877

Please sign in to comment.