Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ You can use below snippet code to use our check balance service on prepaid API a
// import autoload
require_once __DIR__ . "/vendor/autoload.php";

// import IAKPrepaid Class
use IakID\IakApiPHP\Services\IAKPrepaid;
use IakID\IakApiPHP\IAK;

$iakPrepaid = new IAKPrepaid([
'userHp' => 'your-username',
'apiKey' => 'your-api-key-depending-on-stage',
'stage' => 'sandbox-or-production'
$iak = new IAK([
'userHp' => 'your-username',
'apiKey' => 'your-api-key-depending-on-stage',
'stage' => 'sandbox-or-production'
]);

$balanceResult = $iakPrepaid->checkBalance();
echo $balanceResult;
$prepaid = $iak->PrePaid();

echo '<pre>';
print_r($prepaid->checkBalance());
echo '</pre>';
```

### Postpaid
Expand All @@ -44,17 +46,44 @@ echo $balanceResult;
// import autoload
require_once __DIR__ . "/vendor/autoload.php";

// Import IAKPostpaid Class
use IakID\IakApiPHP\Services\IAKPostpaid;
use IakID\IakApiPHP\IAK;

$iak = new IAK([
'userHp' => 'your-username',
'apiKey' => 'your-api-key-depending-on-stage',
'stage' => 'sandbox-or-production'
]);

$postpaid = $iak->PostPaid();

echo '<pre>';
print_r($postpaid->pricelist());
echo '</pre>';

```

### Callback
```php
<?php
// import autoload
require __DIR__ . '/vendor/autoload.php';

$iakPostpaid = new IAKPostpaid([
'userHp' => 'your-username',
'apiKey' => 'your-api-key-depending-on-stage',
'stage' => 'sandbox-or-production'
use IakID\IakApiPHP\IAK;

$iak = new IAK([
'userHp' => 'your-username',
'apiKey' => 'your-api-key-depending-on-stage',
'stage' => 'sandbox-or-production'
]);

$pricelistResult = $iakPostpaid->pricelist();
echo $pricelistResult;
$init = $iak->initCallback();


if ($init->validateSignature() && $init->validateIPNotifications()) {
file_put_contents(__DIR__ . '/callback.json', $init->get() . PHP_EOL . PHP_EOL, FILE_APPEND | LOCK_EX);
} else {
file_put_contents(__DIR__ . '/callback.json', 'Sign :' . $init->validateSignature() . 'IP :' . $init->validateIPNotifications() . PHP_EOL . PHP_EOL, FILE_APPEND | LOCK_EX);
}

```

Expand Down
32 changes: 21 additions & 11 deletions app/IAK.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@
use IakID\IakApiPHP\Helpers\FileHelper;
use IakID\IakApiPHP\Helpers\Validations\IAKValidator;

abstract class IAK
class IAK
{
const DOT_ENV = '.env';

protected $credential, $stage, $url, $headers;
protected $credential, $stage;

public function __construct(array $data = [])
{
IAKValidator::validateCredentialRequest($data);

$this->setEnvironmentFile();
$this->setCredential($data);

$this->headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
];
}

private function setEnvironmentFile()
{
$envDirectory = FileHelper::getAbsolutePathOfAncestorFile(self::DOT_ENV);

if (file_exists($envDirectory . '/' . self::DOT_ENV)) {
$dotEnv = Dotenv::createMutable(FileHelper::getAbsolutePathOfAncestorFile(self::DOT_ENV));
$dotEnv->load();
Expand Down Expand Up @@ -58,8 +53,23 @@ private function setCredential($data)
}
}

protected function generateSign($sign)
public static function generateSign($username, $apikey, $sign)
{
return md5($username . $apikey . $sign);
}

public function PostPaid()
{
return new \IakID\IakApiPHP\Services\IAKPostpaid($this->credential, $this->stage);
}

public function PrePaid()
{
return new \IakID\IakApiPHP\Services\IAKPrepaid($this->credential, $this->stage);
}

public function initCallback()
{
return md5($this->credential['userHp'] . $this->credential['apiKey'] . $sign);
return new \IakID\IakApiPHP\Services\IAKCallback($this->credential, $this->stage);
}
}
}
63 changes: 63 additions & 0 deletions app/Services/IAKCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace IakID\IakApiPHP\Services;

use Symfony\Component\HttpFoundation\Request;

class IAKCallback
{

public $request, $privateKey, $_ip;

const API_NOTIFICATIONS = [
'SANDBOX' => '52.76.169.212',
'PRODUCTION' => '52.220.38.194'
];


public function __construct($credential, $stage)
{
$this->iak = $credential;
$this->_ip = self::API_NOTIFICATIONS[$stage];
$this->request = Request::createFromGlobals();
}


public function get()
{
return $this->request->getContent();
}

public function signature()
{
$data = json_decode($this->get(), true);
return md5($this->iak["userHp"] . $this->iak["apiKey"] . $data['data']['ref_id']);
}



public function validateSignature(): bool
{
$data = json_decode($this->get(), true);

if ($data['data']['sign'] !== $this->signature()) {
return false;
}

return true;
}

public function callbackIPNotifications()
{
return isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
}

public function validateIPNotifications(): bool
{
if ($this->_ip !== $this->callbackIPNotifications()) {
return false;
}

return true;
}
}
34 changes: 20 additions & 14 deletions app/Services/IAKPostpaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
use IakID\IakApiPHP\Helpers\Validations\IAKPostpaidValidator;
use IakID\IakApiPHP\IAK;

class IAKPostpaid extends IAK
class IAKPostpaid
{
public function __construct($data = [])
private $iak, $url, $headers;

public function __construct($credential, $stage)
{
parent::__construct($data);
$this->iak = $credential;

$this->url = Url::URL_POSTPAID[$this->stage];
$this->url = Url::URL_POSTPAID[$stage];
$this->headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
];
}

public function checkStatus($request = [])
Expand All @@ -29,8 +35,8 @@ public function checkStatus($request = [])

$request = array_merge($request, [
'commands' => 'checkstatus',
'username' => $this->credential['userHp'],
'sign' => $this->generateSign('cs')
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], 'cs')
]);

$response = Guzzle::sendRequest($this->url . '/api/v1/bill/check', 'POST', $this->headers, $request);
Expand Down Expand Up @@ -73,8 +79,8 @@ public function inquiry($request = [])

$request = array_merge($request, [
'commands' => 'inq-pasca',
'username' => $this->credential['userHp'],
'sign' => $this->generateSign($request['ref_id'])
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], $request['ref_id'])
]);

$response = Guzzle::sendRequest($this->url . '/api/v1/bill/check', 'POST', $this->headers, $request);
Expand All @@ -95,8 +101,8 @@ public function payment($request = [])

$request = array_merge($request, [
'commands' => 'pay-pasca',
'username' => $this->credential['userHp'],
'sign' => $this->generateSign(strval($request['tr_id']))
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], strval($request['tr_id']))
]);

$response = Guzzle::sendRequest($this->url . '/api/v1/bill/check', 'POST', $this->headers, $request);
Expand All @@ -115,8 +121,8 @@ public function pricelist($request = [])

$request = array_merge($request, [
'commands' => 'pricelist-pasca',
'username' => $this->credential['userHp'],
'sign' => $this->generateSign('pl')
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], 'pl')
]);

$postpaidUrl = $this->url . '/api/v1/bill/check';
Expand All @@ -127,10 +133,10 @@ public function pricelist($request = [])

$response = Guzzle::sendRequest($postpaidUrl, 'POST', $this->headers, $request);
$response = $response['data'];

return ResponseFormatter::formatResponse($response);
} catch (Exception $e) {
return Guzzle::handleException($e);
}
}
}
}
44 changes: 25 additions & 19 deletions app/Services/IAKPrepaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@
use IakID\IakApiPHP\Helpers\Validations\IAKPrepaidValidator;
use IakID\IakApiPHP\IAK;

class IAKPrepaid extends IAK
class IAKPrepaid
{
public function __construct($data = [])
private $iak, $url, $headers;

public function __construct($credential, $stage)
{
parent::__construct($data);
$this->iak = $credential;
$this->url = Url::URL_PREPAID[$stage];

$this->url = Url::URL_PREPAID[$this->stage];
$this->headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
];
}

public function checkBalance()
{
try {
$request = [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign('bl')
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], 'bl')
];

$response = Guzzle::sendRequest($this->url . '/api/check-balance', 'POST', $this->headers, $request);
Expand All @@ -44,8 +50,8 @@ public function checkStatus($request = [])
$request = RequestFormatter::formatArrayKeysToSnakeCase($request);

$request = array_merge($request, [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign($request['ref_id'])
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], $request['ref_id'])
]);

$response = Guzzle::sendRequest($this->url . '/api/check-status', 'POST', $this->headers, $request);
Expand All @@ -65,8 +71,8 @@ public function inquiryGameID($request = [])
$request = RequestFormatter::formatArrayKeysToSnakeCase($request);

$request = array_merge($request, [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign($request['game_code'])
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], $request['game_code'])
]);

$response = Guzzle::sendRequest($this->url . '/api/inquiry-game', 'POST', $this->headers, $request);
Expand All @@ -86,8 +92,8 @@ public function inquiryGameServer($request = [])
$request = RequestFormatter::formatArrayKeysToSnakeCase($request);

$request = array_merge($request, [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign($request['game_code'])
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], $request['game_code'])
]);

$response = Guzzle::sendRequest($this->url . '/api/inquiry-game-server', 'POST', $this->headers, $request);
Expand All @@ -107,8 +113,8 @@ public function inquiryPLN($request = [])
$request = RequestFormatter::formatArrayKeysToSnakeCase($request);

$request = array_merge($request, [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign($request['customer_id'])
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], $request['customer_id'])
]);

$response = Guzzle::sendRequest($this->url . '/api/inquiry-pln', 'POST', $this->headers, $request);
Expand All @@ -126,8 +132,8 @@ public function pricelist($request = [])
IAKPrepaidValidator::validatePricelistRequest($request);

$request = array_merge($request, [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign('pl')
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], 'pl')
]);

$prepaidUrl = $this->url . '/api/pricelist';
Expand Down Expand Up @@ -157,8 +163,8 @@ public function topUp($request = [])
$request = RequestFormatter::formatArrayKeysToSnakeCase($request);

$request = array_merge($request, [
'username' => $this->credential['userHp'],
'sign' => $this->generateSign($request['ref_id'])
'username' => $this->iak["userHp"],
'sign' => IAK::generateSign($this->iak["userHp"], $this->iak["apiKey"], $request['ref_id'])
]);

$response = Guzzle::sendRequest($this->url . '/api/top-up', 'POST', $this->headers, $request);
Expand All @@ -169,4 +175,4 @@ public function topUp($request = [])
return Guzzle::handleException($e);
}
}
}
}
Loading