Last active
October 28, 2024 02:11
-
-
Save catwhocode/945af3ce17437e1e7da8a644584f3e67 to your computer and use it in GitHub Desktop.
Laravel - BRI API
This file contains hidden or 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
<?php | |
// config/bri.php | |
return [ | |
'sandbox_url' => env('BRI_SANDBOX_URL', ''), | |
'production_url' => env('BRI_PRODUCTION_URL', ''), | |
'consumer_key' => env('BRI_CONSUMER_KEY', ''), | |
'consumer_secret' => env('BRI_CONSUMER_SECRET', ''), | |
'rsa_public_key_path' => storage_path() . '/app/' . env('RSA_PUBLIC_KEY_PATH'), | |
'rsa_private_key_path' => storage_path() . '/app/' . env('RSA_PRIVATE_KEY_PATH'), | |
]; |
This file contains hidden or 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
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\App; | |
use Illuminate\Support\Carbon; | |
use GuzzleHttp\Psr7; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\ClientException; | |
use GuzzleHttp\Exception\ServerException; | |
class BriController extends Controller | |
{ | |
protected $path; | |
protected $url; | |
protected $token; | |
protected $rsaPublicKeyPath; | |
protected $rsaPrivateKeyPath; | |
protected $rsaPublicKey; | |
protected $rsaPrivateKey; | |
protected $consumerKey; | |
protected $consumerSecret; | |
protected $timestamp; | |
public function __construct() | |
{ | |
if (App::environment() == 'production'){ | |
$this->url = config('bri.production_url'); | |
} else { | |
$this->url = config('bri.sandbox_url'); | |
} | |
$this->consumerKey = config('bri.consumer_key'); | |
$this->consumerSecret = config('bri.consumer_secret'); | |
$this->rsaPublicKeyPath = config('bri.rsa_public_key_path'); | |
$this->rsaPrivateKeyPath = config('bri.rsa_private_key_path'); | |
$this->rsaPublicKey = file_get_contents($this->rsaPublicKeyPath); | |
$this->rsaPrivateKey = file_get_contents($this->rsaPrivateKeyPath); | |
} | |
public function getTimestamp() | |
{ | |
return date("Y-m-d") . 'T' . date("H:i:s.000P"); // 2024-07-23T10:05:41.000+07:00 | |
} | |
public function brivaGetAccessToken($consumerKey, $timestamp) | |
{ | |
$url = $this->url . '/snap/v1.0/access-token/b2b'; | |
$stringToSign = $consumerKey . '|' . $timestamp; | |
$privateKey = $this->rsaPrivateKey; | |
$signature = hash_hmac("sha256", $stringToSign, $privateKey, true); | |
$headers = [ | |
'X-SIGNATURE' => base64_encode($signature), | |
'X-CLIENT-KEY' => $consumerKey, | |
'X-TIMESTAMP' => $timestamp, | |
'Content-Type' => 'application/json' | |
]; | |
$body = [ | |
'grantType' => 'client_credentials' | |
]; | |
try { | |
$client = new Client(); | |
$response = $client->request('POST', $url,[ | |
'headers' => $headers, | |
'body' => json_encode($body) | |
]); | |
if ($response->getStatusCode() == 200) { | |
$responseBody = $response->getBody(); | |
$objBody = json_decode($responseBody); | |
$arr = [ | |
'status' => true, | |
'accessToken' => $objBody->accessToken | |
]; | |
return $arr; | |
} | |
} catch (ClientException $e) { | |
$guzzleRequest = Psr7\Message::toString($e->getRequest()); | |
$guzzleResponse = Psr7\Message::toString($e->getResponse()); | |
$arr = [ | |
'status' => false, | |
'message' => 'Client Exception', | |
'request' => $guzzleRequest, | |
'response' => $guzzleResponse | |
]; | |
return $arr; | |
} catch (ServerException $e) { | |
$guzzleRequest = Psr7\Message::toString($e->getRequest()); | |
$guzzleResponse = Psr7\Message::toString($e->getResponse()); | |
$arr = [ | |
'status' => false, | |
'message' => 'Server Exception', | |
'request' => $guzzleRequest, | |
'response' => $guzzleResponse | |
]; | |
return $arr; | |
} | |
} | |
} |
This file contains hidden or 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
BRI_SANDBOX_URL="https://sandbox.partner.api.bri.co.id" | |
BRI_PRODUCTION_URL="https://partner.api.bri.co.id" | |
BRI_CONSUMER_KEY="blablabla" | |
BRI_CONSUMER_SECRET="blablabla" | |
RSA_PUBLIC_KEY_PATH="rsa_2048_public_key.pem" | |
RSA_PRIVATE_KEY_PATH="rsa_2048_private_key.pem" |
This file contains hidden or 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
HTTP/1.1 401 Unauthorized | |
Strict-Transport-Security: max-age=31536000; includeSubDomains | |
Date: Tue, 23 Jul 2024 10:05:41 GMT | |
Content-Type: application/json | |
Content-Length: 206 | |
Connection: keep-alive | |
{ | |
"responseCode": "4017300", | |
"responseMessage": "Unauthorized. stringToSign" | |
} |
This file contains hidden or 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
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\App; | |
use Illuminate\Support\Carbon; | |
use GuzzleHttp\Psr7; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\ClientException; | |
use GuzzleHttp\Exception\ServerException; | |
class VirtualAccountController extends BriController | |
{ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
public function createVa() | |
{ | |
$consumerKey = $this->consumerKey; | |
$timestamp = $this->getTimestamp(); | |
$accessToken = $this->brivaGetAccessToken($consumerKey,$timestamp); | |
return $accessToken; | |
} | |
} |
This file contains hidden or 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
<?php | |
use Illuminate\Support\Facades\App; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Route; | |
use Illuminate\Support\Facades\URL; | |
use App\Http\Controllers\BriController; | |
use App\Http\Controllers\VirtualAccountController; | |
Route::controller(VirtualAccountController::class)->group(function (){ | |
Route::get('/va/create', 'createVa'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
udah bisa ga kak ngatasi error "responseCode": "4017300","responseMessage": "Unauthorized. stringToSign" ?