Skip to content

Commit 3f8d203

Browse files
authored
feat : snap-bi for BRI
Implement Bri snap api
2 parents 9f228dd + 5bd0f01 commit 3f8d203

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

src/Services/BRI/BriConfig.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Otnansirk\SnapBI\Services\BRI;
4+
5+
use Otnansirk\SnapBI\Interfaces\ConfigInterface;
6+
use Otnansirk\SnapBI\Traits\HasConfig;
7+
use Otnansirk\SnapBI\Traits\HasSelfCall;
8+
9+
10+
final class BriConfig implements ConfigInterface
11+
{
12+
use HasConfig, HasSelfCall;
13+
}

src/Services/BRI/BriSnapApi.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Otnansirk\SnapBI\Services\BRI;
4+
5+
use Otnansirk\SnapBI\Core\SnapApiCore;
6+
use Otnansirk\SnapBI\Exception\SnapBiException;
7+
use Otnansirk\SnapBI\Interfaces\SnapApiInterface;
8+
use Otnansirk\SnapBI\Services\BRI\Traits\HasAccessToken;
9+
use Otnansirk\SnapBI\Services\BRI\Traits\HasTransaction;
10+
use Otnansirk\SnapBI\Traits\HasSelfCall;
11+
12+
class BriSnapApi extends SnapApiCore implements SnapApiInterface
13+
{
14+
use HasSelfCall;
15+
use HasAccessToken;
16+
use HasTransaction;
17+
18+
function __construct()
19+
{
20+
if (!count(BriConfig::all())) {
21+
throw new SnapBiException("Please register configuration first. See https://php-snap-bi.gitbook.io/docs/getting-started/configuration", 1);
22+
}
23+
}
24+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Otnansirk\SnapBI\Services\BRI\Traits;
4+
5+
use Carbon\Carbon;
6+
use Otnansirk\SnapBI\Exception\AuthenticateException;
7+
use Otnansirk\SnapBI\Interfaces\HttpResponseInterface;
8+
use Otnansirk\SnapBI\Services\BRI\BriConfig;
9+
use Otnansirk\SnapBI\Support\HttpResponse;
10+
use Otnansirk\SnapBI\Support\Signature;
11+
use Otnansirk\SnapBI\Support\Http;
12+
13+
14+
trait HasAccessToken
15+
{
16+
17+
protected static $token;
18+
19+
/**
20+
* Set token if needed
21+
*
22+
* @param string $token
23+
* @return self
24+
*/
25+
public static function withTokenB2b(string $token = null): self
26+
{
27+
if ($token) {
28+
self::$token = $token;
29+
} else {
30+
self::$token = self::accessTokenB2b()->object()->accessToken;
31+
}
32+
33+
return new self;
34+
}
35+
36+
/**
37+
* Throw error if not authenticated
38+
*
39+
* @return void
40+
*/
41+
public static function authenticated()
42+
{
43+
if (!self::$token) {
44+
throw new AuthenticateException("Unauthorized: Please provide an access token", 400);
45+
}
46+
}
47+
48+
/**
49+
* Get access token
50+
*
51+
* @return HttpResponse
52+
*/
53+
public static function accessTokenB2b(): HttpResponseInterface
54+
{
55+
$carbon = Carbon::now('Asia/Jakarta');
56+
$timestamp = $carbon->format('Y-m-d\TH:i:s.000P'); // outputs: 2021-11-02T13:14:15.000+07:00
57+
$path = BriConfig::get('base_url') . "/snap/v1.0/access-token/b2b";
58+
$headers = [
59+
'X-TIMESTAMP' => $timestamp,
60+
'X-CLIENT-KEY' => BriConfig::get('client_id'),
61+
'X-SIGNATURE' => Signature::asymmetricBri(BriConfig::class,$timestamp),
62+
];
63+
64+
$body = ['grantType' => 'client_credentials'];
65+
return Http::withHeaders($headers)->post($path, $body);
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Otnansirk\SnapBI\Services\BRI\Traits;
4+
5+
use Otnansirk\SnapBI\Interfaces\HttpResponseInterface;
6+
use Otnansirk\SnapBI\Services\BRI\BriConfig;
7+
use Otnansirk\SnapBI\Support\Signature;
8+
use Otnansirk\SnapBI\Support\Http;
9+
use Ramsey\Uuid\Uuid;
10+
11+
12+
trait HasTransaction
13+
{
14+
/**
15+
* Get list of bank statment
16+
*
17+
* @param string $startDate | Format ISO-8601 "2023-08-22T00:00:00+07:00"
18+
* @param string $endDate | Format ISO-8601 "2023-08-22T00:00:00+07:00"
19+
* @return HttpResponseInterface
20+
*/
21+
public static function bankStatement(string $startDate, string $endDate): HttpResponseInterface
22+
{
23+
// Required access token
24+
self::authenticated();
25+
$path = "/openapi/transactions/v2.0/bank-statement";
26+
27+
$timestamp = currentTimestamp()->toIso8601String();
28+
$accessToken = self::$token;
29+
30+
$body = array_merge([
31+
"partnerReferenceNo" => Uuid::uuid4(),
32+
"accountNo" => BriConfig::get('account_id'),
33+
"fromDateTime" => $startDate,
34+
"toDateTime" => $endDate,
35+
"bankCardToken" => BriConfig::get('bank_card_token')
36+
], self::$body);
37+
38+
$headers = array_merge([
39+
'X-TIMESTAMP' => $timestamp,
40+
'X-SIGNATURE' => Signature::symmetric(BriConfig::class, 'POST', $path, $body, $timestamp, $accessToken),
41+
'CHANNEL-ID' => BriConfig::get('channel_id'),
42+
'X-PARTNER-ID' => BriConfig::get('partner_id'),
43+
'X-EXTERNAL-ID' => int_rand(16),
44+
], self::$headers);
45+
46+
$url = BriConfig::get('base_url') . $path;
47+
return Http::withToken($accessToken)
48+
->withHeaders($headers)
49+
->post($url, $body);
50+
}
51+
}

src/Support/Signature.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ final public static function asymmetric(string $config): string
2626
return base64_encode($signature);
2727
}
2828

29+
final public static function asymmetricBri(string $config,$timestamp) : String {
30+
$privateKey = $config::get('ssh_private_key');
31+
$stringToSign = $config::get('client_id').'|'.$timestamp;
32+
$signature = "";
33+
if (!openssl_sign($stringToSign, $signature, $privateKey, OPENSSL_ALGO_SHA256)) {
34+
throw new SignatureException("Failed to generate signature");
35+
}
36+
37+
// X-SIGNATURE
38+
return base64_encode($signature);
39+
}
40+
2941
/**
3042
* Generate signature symmetric
3143
*

0 commit comments

Comments
 (0)