odenktools /
php-bca
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Bca; |
||||
| 4 | |||||
| 5 | use Carbon\Carbon; |
||||
| 6 | use Unirest\Request; |
||||
| 7 | use Unirest\Request\Body; |
||||
| 8 | |||||
| 9 | /** |
||||
| 10 | * BCA REST API Library. |
||||
| 11 | * |
||||
| 12 | * @author Pribumi Technology |
||||
| 13 | * @license MIT |
||||
| 14 | * @copyright (c) 2017, Pribumi Technology |
||||
| 15 | */ |
||||
| 16 | class BcaHttp |
||||
| 17 | { |
||||
| 18 | public static $VERSION = '2.3.1'; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Default Timezone. |
||||
| 22 | * |
||||
| 23 | * @var string |
||||
| 24 | */ |
||||
| 25 | private static $timezone = 'Asia/Jakarta'; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * Default BCA Port. |
||||
| 29 | * |
||||
| 30 | * @var int |
||||
| 31 | */ |
||||
| 32 | private static $port = 443; |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * Default BCA Host. |
||||
| 36 | * |
||||
| 37 | * @var string |
||||
| 38 | */ |
||||
| 39 | private static $hostName = 'sandbox.bca.co.id'; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Default BCA Host. |
||||
| 43 | * |
||||
| 44 | * @var string |
||||
| 45 | */ |
||||
| 46 | private static $scheme = 'https'; |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * Timeout curl. |
||||
| 50 | * |
||||
| 51 | * @var int |
||||
| 52 | */ |
||||
| 53 | private static $timeOut = 60; |
||||
| 54 | |||||
| 55 | /** |
||||
| 56 | * Default Curl Options. |
||||
| 57 | * |
||||
| 58 | * @var int |
||||
| 59 | */ |
||||
| 60 | private static $curlOptions = array( |
||||
| 61 | CURLOPT_SSL_VERIFYHOST => 0, |
||||
| 62 | CURLOPT_SSLVERSION => 6, |
||||
| 63 | CURLOPT_SSL_VERIFYPEER => false, |
||||
| 64 | CURLOPT_TIMEOUT => 60 |
||||
| 65 | ); |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * Default BCA Settings. |
||||
| 69 | * |
||||
| 70 | * @var array |
||||
| 71 | */ |
||||
| 72 | protected $settings = array( |
||||
| 73 | 'corp_id' => '', |
||||
| 74 | 'client_id' => '', |
||||
| 75 | 'client_secret' => '', |
||||
| 76 | 'api_key' => '', |
||||
| 77 | 'secret_key' => '', |
||||
| 78 | 'curl_options' => array(), |
||||
| 79 | // Backward compatible |
||||
| 80 | 'host' => 'sandbox.bca.co.id', |
||||
| 81 | 'scheme' => 'https', |
||||
| 82 | 'timeout' => 60, |
||||
| 83 | 'port' => 443, |
||||
| 84 | 'timezone' => 'Asia/Jakarta', |
||||
| 85 | // New Options |
||||
| 86 | 'options' => array( |
||||
| 87 | 'host' => 'sandbox.bca.co.id', |
||||
| 88 | 'scheme' => 'https', |
||||
| 89 | 'timeout' => 60, |
||||
| 90 | 'port' => 443, |
||||
| 91 | 'timezone' => 'Asia/Jakarta' |
||||
| 92 | ) |
||||
| 93 | ); |
||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * Default Constructor. |
||||
| 97 | * |
||||
| 98 | * @param string $corp_id nilai corp id |
||||
| 99 | * @param string $client_id nilai client key |
||||
| 100 | * @param string $client_secret nilai client secret |
||||
| 101 | * @param string $api_key niali oauth key |
||||
| 102 | * @param string $secret_key nilai oauth secret |
||||
| 103 | * @param array $options opsi ke server bca |
||||
| 104 | */ |
||||
| 105 | public function __construct($corp_id, $client_id, $client_secret, $api_key, $secret_key, array $options = []) |
||||
| 106 | { |
||||
| 107 | // Required parameters. |
||||
| 108 | $this->settings['corp_id'] = $corp_id; |
||||
| 109 | $this->settings['client_id'] = $client_id; |
||||
| 110 | $this->settings['client_secret'] = $client_secret; |
||||
| 111 | $this->settings['api_key'] = $api_key; |
||||
| 112 | $this->settings['secret_key'] = $secret_key; |
||||
| 113 | $this->settings['host'] = |
||||
| 114 | preg_replace('/http[s]?\:\/\//', '', $this->settings['host'], 1); |
||||
| 115 | |||||
| 116 | foreach ($options as $key => $value) { |
||||
| 117 | if (isset($this->settings[$key])) { |
||||
| 118 | $this->settings[$key] = $value; |
||||
| 119 | } |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | // Setup optional scheme, if scheme is empty |
||||
| 123 | if (isset($options['scheme'])) { |
||||
| 124 | $this->settings['scheme'] = $options['scheme']; |
||||
| 125 | $this->settings['options']['scheme'] = $options['scheme']; |
||||
| 126 | } else { |
||||
| 127 | $this->settings['scheme'] = self::getScheme(); |
||||
| 128 | $this->settings['options']['scheme'] = self::getScheme(); |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | // Setup optional host, if host is empty |
||||
| 132 | if (isset($options['host'])) { |
||||
| 133 | $this->settings['host'] = $options['host']; |
||||
| 134 | $this->settings['options']['host'] = $options['host']; |
||||
| 135 | } else { |
||||
| 136 | $this->settings['host'] = self::getHostName(); |
||||
| 137 | $this->settings['options']['host'] = self::getHostName(); |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | // Setup optional port, if port is empty |
||||
| 141 | if (isset($options['port'])) { |
||||
| 142 | $this->settings['port'] = $options['port']; |
||||
| 143 | $this->settings['options']['port'] = $options['port']; |
||||
| 144 | } else { |
||||
| 145 | $this->settings['port'] = self::getPort(); |
||||
| 146 | $this->settings['options']['port'] = self::getPort(); |
||||
| 147 | } |
||||
| 148 | |||||
| 149 | // Setup optional timezone, if timezone is empty |
||||
| 150 | if (isset($options['timezone'])) { |
||||
| 151 | $this->settings['timezone'] = $options['timezone']; |
||||
| 152 | $this->settings['options']['timezone'] = $options['timezone']; |
||||
| 153 | } else { |
||||
| 154 | $this->settings['timezone'] = self::getHostName(); |
||||
| 155 | $this->settings['options']['timezone'] = self::getHostName(); |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | // Setup optional timeout, if timeout is empty |
||||
| 159 | if (isset($options['timeout'])) { |
||||
| 160 | $this->settings['timeout'] = $options['timeout']; |
||||
| 161 | $this->settings['options']['timeout'] = $options['timeout']; |
||||
| 162 | } else { |
||||
| 163 | $this->settings['timeout'] = self::getTimeOut(); |
||||
| 164 | $this->settings['options']['timeout'] = self::getTimeOut(); |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | // Set Default Curl Options. |
||||
| 168 | Request::curlOpts(self::$curlOptions); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 169 | |||||
| 170 | // Set custom curl options |
||||
| 171 | if (!empty($this->settings['curl_options'])) { |
||||
| 172 | $data = self::mergeCurlOptions(self::$curlOptions, $this->settings['curl_options']); |
||||
|
0 ignored issues
–
show
self::curlOptions of type integer is incompatible with the type array expected by parameter $existing_options of Bca\BcaHttp::mergeCurlOptions().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 173 | Request::curlOpts($data); |
||||
| 174 | } |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | /** |
||||
| 178 | * Ambil Nilai settings. |
||||
| 179 | * |
||||
| 180 | * @return array |
||||
| 181 | */ |
||||
| 182 | public function getSettings() |
||||
| 183 | { |
||||
| 184 | return $this->settings; |
||||
| 185 | } |
||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * Build the ddn domain. |
||||
| 189 | * output = 'https://sandbox.bca.co.id:443' |
||||
| 190 | * scheme = http(s) |
||||
| 191 | * host = sandbox.bca.co.id |
||||
| 192 | * port = 80 ? 443 |
||||
| 193 | * |
||||
| 194 | * @return string |
||||
| 195 | */ |
||||
| 196 | private function ddnDomain() |
||||
| 197 | { |
||||
| 198 | return $this->settings['scheme'] . '://' . $this->settings['host'] . ':' . $this->settings['port'] . '/'; |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | /** |
||||
| 202 | * Generate authentifikasi ke server berupa OAUTH. |
||||
| 203 | * |
||||
| 204 | * @return \Unirest\Response |
||||
| 205 | */ |
||||
| 206 | public function httpAuth() |
||||
| 207 | { |
||||
| 208 | $client_id = $this->settings['client_id']; |
||||
| 209 | $client_secret = $this->settings['client_secret']; |
||||
| 210 | |||||
| 211 | $headerToken = base64_encode("$client_id:$client_secret"); |
||||
| 212 | |||||
| 213 | $headers = array('Accept' => 'application/json', 'Authorization' => "Basic $headerToken"); |
||||
| 214 | |||||
| 215 | $request_path = "api/oauth/token"; |
||||
| 216 | $domain = $this->ddnDomain(); |
||||
| 217 | $full_url = $domain . $request_path; |
||||
| 218 | |||||
| 219 | $data = array('grant_type' => 'client_credentials'); |
||||
| 220 | $body = Body::form($data); |
||||
| 221 | $response = Request::post($full_url, $headers, $body); |
||||
| 222 | |||||
| 223 | return $response; |
||||
| 224 | } |
||||
| 225 | |||||
| 226 | /** |
||||
| 227 | * Ambil informasi saldo berdasarkan nomor akun BCA. |
||||
| 228 | * |
||||
| 229 | * @param string $oauth_token nilai token yang telah didapatkan setelah login |
||||
| 230 | * @param array $sourceAccountId nomor akun yang akan dicek |
||||
| 231 | * |
||||
| 232 | * @throws BcaHttpException error |
||||
| 233 | * @return \Unirest\Response |
||||
| 234 | */ |
||||
| 235 | public function getBalanceInfo($oauth_token, $sourceAccountId = []) |
||||
| 236 | { |
||||
| 237 | $corp_id = $this->settings['corp_id']; |
||||
| 238 | |||||
| 239 | $this->validateArray($sourceAccountId); |
||||
| 240 | |||||
| 241 | ksort($sourceAccountId); |
||||
| 242 | $arraySplit = implode(",", $sourceAccountId); |
||||
| 243 | $arraySplit = urlencode($arraySplit); |
||||
| 244 | |||||
| 245 | $uriSign = "GET:/banking/v3/corporates/$corp_id/accounts/$arraySplit"; |
||||
| 246 | $isoTime = self::generateIsoTime(); |
||||
| 247 | $authSignature = self::generateSign($uriSign, $oauth_token, $this->settings['secret_key'], $isoTime, null); |
||||
| 248 | |||||
| 249 | $headers = array(); |
||||
| 250 | $headers['Accept'] = 'application/json'; |
||||
| 251 | $headers['Content-Type'] = 'application/json'; |
||||
| 252 | $headers['Authorization'] = "Bearer $oauth_token"; |
||||
| 253 | $headers['X-BCA-Key'] = $this->settings['api_key']; |
||||
| 254 | $headers['X-BCA-Timestamp'] = $isoTime; |
||||
| 255 | $headers['X-BCA-Signature'] = $authSignature; |
||||
| 256 | |||||
| 257 | $request_path = "banking/v3/corporates/$corp_id/accounts/$arraySplit"; |
||||
| 258 | $domain = $this->ddnDomain(); |
||||
| 259 | $full_url = $domain . $request_path; |
||||
| 260 | |||||
| 261 | $data = array('grant_type' => 'client_credentials'); |
||||
| 262 | |||||
| 263 | $body = Body::form($data); |
||||
| 264 | $response = Request::get($full_url, $headers, $body); |
||||
| 265 | |||||
| 266 | return $response; |
||||
| 267 | } |
||||
| 268 | |||||
| 269 | /** |
||||
| 270 | * Ambil Daftar transaksi pertanggal. |
||||
| 271 | * |
||||
| 272 | * @param string $oauth_token nilai token yang telah didapatkan setelah login |
||||
| 273 | * @param string $sourceAccount nomor akun yang akan dicek |
||||
| 274 | * @param string $startDate tanggal awal |
||||
| 275 | * @param string $endDate tanggal akhir |
||||
| 276 | * @param string $corp_id nilai CorporateID yang telah diberikan oleh pihak BCA |
||||
| 277 | * |
||||
| 278 | * @return \Unirest\Response |
||||
| 279 | */ |
||||
| 280 | public function getAccountStatement($oauth_token, $sourceAccount, $startDate, $endDate) |
||||
| 281 | { |
||||
| 282 | $corp_id = $this->settings['corp_id']; |
||||
| 283 | |||||
| 284 | $uriSign = "GET:/banking/v3/corporates/$corp_id/accounts/$sourceAccount/statements?EndDate=$endDate&StartDate=$startDate"; |
||||
| 285 | $isoTime = self::generateIsoTime(); |
||||
| 286 | $authSignature = self::generateSign($uriSign, $oauth_token, $this->settings['secret_key'], $isoTime, null); |
||||
| 287 | |||||
| 288 | $headers = array(); |
||||
| 289 | $headers['Accept'] = 'application/json'; |
||||
| 290 | $headers['Content-Type'] = 'application/json'; |
||||
| 291 | $headers['Authorization'] = "Bearer $oauth_token"; |
||||
| 292 | $headers['X-BCA-Key'] = $this->settings['secret_key']; |
||||
| 293 | $headers['X-BCA-Timestamp'] = $isoTime; |
||||
| 294 | $headers['X-BCA-Signature'] = $authSignature; |
||||
| 295 | |||||
| 296 | $request_path = "banking/v3/corporates/$corp_id/accounts/$sourceAccount/statements?EndDate=$endDate&StartDate=$startDate"; |
||||
| 297 | $domain = $this->ddnDomain(); |
||||
| 298 | $full_url = $domain . $request_path; |
||||
| 299 | |||||
| 300 | $data = array('grant_type' => 'client_credentials'); |
||||
| 301 | $body = Body::form($data); |
||||
| 302 | $response = Request::get($full_url, $headers, $body); |
||||
| 303 | |||||
| 304 | return $response; |
||||
| 305 | } |
||||
| 306 | |||||
| 307 | /** |
||||
| 308 | * Ambil informasi ATM berdasarkan lokasi GEO. |
||||
| 309 | * |
||||
| 310 | * @param string $oauth_token nilai token yang telah didapatkan setelah login |
||||
| 311 | * @param string $latitude Langitude GPS |
||||
| 312 | * @param string $longitude Longitude GPS |
||||
| 313 | * @param string $count Jumlah ATM BCA yang akan ditampilkan |
||||
| 314 | * @param string $radius Nilai radius dari lokasi GEO |
||||
| 315 | * |
||||
| 316 | * @throws BcaHttpException error |
||||
| 317 | * @return \Unirest\Response |
||||
| 318 | */ |
||||
| 319 | public function getAtmLocation( |
||||
| 320 | $oauth_token, |
||||
| 321 | $latitude, |
||||
| 322 | $longitude, |
||||
| 323 | $count = '10', |
||||
| 324 | $radius = '20' |
||||
| 325 | ) |
||||
| 326 | { |
||||
| 327 | $params = array(); |
||||
| 328 | $params['SearchBy'] = 'Distance'; |
||||
| 329 | $params['Latitude'] = $latitude; |
||||
| 330 | $params['Longitude'] = $longitude; |
||||
| 331 | $params['Count'] = $count; |
||||
| 332 | $params['Radius'] = $radius; |
||||
| 333 | ksort($params); |
||||
| 334 | |||||
| 335 | $auth_query_string = self::arrayImplode('=', '&', $params); |
||||
| 336 | |||||
| 337 | $uriSign = "GET:/general/info-bca/atm?$auth_query_string"; |
||||
| 338 | $isoTime = self::generateIsoTime(); |
||||
| 339 | $authSignature = self::generateSign($uriSign, $oauth_token, $this->settings['secret_key'], $isoTime, null); |
||||
| 340 | |||||
| 341 | $headers = array(); |
||||
| 342 | $headers['Accept'] = 'application/json'; |
||||
| 343 | $headers['Content-Type'] = 'application/json'; |
||||
| 344 | $headers['Authorization'] = "Bearer $oauth_token"; |
||||
| 345 | $headers['X-BCA-Key'] = $this->settings['api_key']; |
||||
| 346 | $headers['X-BCA-Timestamp'] = $isoTime; |
||||
| 347 | $headers['X-BCA-Signature'] = $authSignature; |
||||
| 348 | |||||
| 349 | $request_path = "general/info-bca/atm?SearchBy=Distance&Latitude=$latitude&Longitude=$longitude&Count=$count&Radius=$radius"; |
||||
| 350 | $domain = $this->ddnDomain(); |
||||
| 351 | $full_url = $domain . $request_path; |
||||
| 352 | |||||
| 353 | $data = array('grant_type' => 'client_credentials'); |
||||
| 354 | $body = Body::form($data); |
||||
| 355 | $response = Request::get($full_url, $headers, $body); |
||||
| 356 | |||||
| 357 | return $response; |
||||
| 358 | } |
||||
| 359 | |||||
| 360 | /** |
||||
| 361 | * Ambil KURS mata uang. |
||||
| 362 | * |
||||
| 363 | * @param string $oauth_token nilai token yang telah didapatkan setelah login |
||||
| 364 | * @param string $rateType type rate |
||||
| 365 | * @param string $currency Mata uang |
||||
| 366 | * |
||||
| 367 | * @throws BcaHttpException error |
||||
| 368 | * @return \Unirest\Response |
||||
| 369 | */ |
||||
| 370 | public function getForexRate( |
||||
| 371 | $oauth_token, |
||||
| 372 | $rateType = 'eRate', |
||||
| 373 | $currency = 'USD' |
||||
| 374 | ) |
||||
| 375 | { |
||||
| 376 | $params = array(); |
||||
| 377 | $params['RateType'] = strtolower($rateType); |
||||
| 378 | $params['CurrencyCode'] = strtoupper($currency); |
||||
| 379 | ksort($params); |
||||
| 380 | |||||
| 381 | $auth_query_string = self::arrayImplode('=', '&', $params); |
||||
| 382 | |||||
| 383 | $uriSign = "GET:/general/rate/forex?$auth_query_string"; |
||||
| 384 | $isoTime = self::generateIsoTime(); |
||||
| 385 | $authSignature = self::generateSign($uriSign, $oauth_token, $this->settings['secret_key'], $isoTime, null); |
||||
| 386 | |||||
| 387 | $headers = array(); |
||||
| 388 | $headers['Accept'] = 'application/json'; |
||||
| 389 | $headers['Content-Type'] = 'application/json'; |
||||
| 390 | $headers['Authorization'] = "Bearer $oauth_token"; |
||||
| 391 | $headers['X-BCA-Key'] = $this->settings['api_key']; |
||||
| 392 | $headers['X-BCA-Timestamp'] = $isoTime; |
||||
| 393 | $headers['X-BCA-Signature'] = $authSignature; |
||||
| 394 | |||||
| 395 | $request_path = "general/rate/forex?$auth_query_string"; |
||||
| 396 | $domain = $this->ddnDomain(); |
||||
| 397 | $full_url = $domain . $request_path; |
||||
| 398 | |||||
| 399 | $data = array('grant_type' => 'client_credentials'); |
||||
| 400 | $body = Body::form($data); |
||||
| 401 | $response = Request::get($full_url, $headers, $body); |
||||
| 402 | |||||
| 403 | return $response; |
||||
| 404 | } |
||||
| 405 | |||||
| 406 | /** |
||||
| 407 | * Transfer dana kepada akun lain dengan jumlah nominal tertentu. |
||||
| 408 | * |
||||
| 409 | * @param string $oauth_token nilai token yang telah didapatkan setelah login |
||||
| 410 | * @param int $amount nilai dana dalam RUPIAH yang akan ditransfer, Format: 13.2 |
||||
| 411 | * @param string $beneficiaryAccountNumber BCA Account number to be credited (Destination) |
||||
| 412 | * @param string $referenceID Sender's transaction reference ID |
||||
| 413 | * @param string $remark1 Transfer remark for receiver |
||||
| 414 | * @param string $remark2 ransfer remark for receiver |
||||
| 415 | * @param string $sourceAccountNumber Source of Fund Account Number |
||||
| 416 | * @param string $transactionID Transcation ID unique per day (using UTC+07 Time Zone). Format: Number |
||||
| 417 | * @param string $corp_id nilai CorporateID yang telah diberikan oleh pihak BCA [Optional] |
||||
| 418 | * |
||||
| 419 | * @return \Unirest\Response |
||||
| 420 | */ |
||||
| 421 | public function fundTransfers( |
||||
| 422 | $oauth_token, |
||||
| 423 | $amount, |
||||
| 424 | $sourceAccountNumber, |
||||
| 425 | $beneficiaryAccountNumber, |
||||
| 426 | $referenceID, |
||||
| 427 | $remark1, |
||||
| 428 | $remark2, |
||||
| 429 | $transactionID |
||||
| 430 | ) |
||||
| 431 | { |
||||
| 432 | $uriSign = "POST:/banking/corporates/transfers"; |
||||
| 433 | |||||
| 434 | $isoTime = self::generateIsoTime(); |
||||
| 435 | |||||
| 436 | $headers = array(); |
||||
| 437 | $headers['Accept'] = 'application/json'; |
||||
| 438 | $headers['Content-Type'] = 'application/json'; |
||||
| 439 | $headers['Authorization'] = "Bearer $oauth_token"; |
||||
| 440 | $headers['X-BCA-Key'] = $this->settings['api_key']; |
||||
| 441 | $headers['X-BCA-Timestamp'] = $isoTime; |
||||
| 442 | |||||
| 443 | $request_path = "banking/corporates/transfers"; |
||||
| 444 | $domain = $this->ddnDomain(); |
||||
| 445 | $full_url = $domain . $request_path; |
||||
| 446 | |||||
| 447 | $bodyData = array(); |
||||
| 448 | $bodyData['Amount'] = $amount; |
||||
| 449 | $bodyData['BeneficiaryAccountNumber'] = strtolower(str_replace(' ', '', $beneficiaryAccountNumber)); |
||||
| 450 | $bodyData['CorporateID'] = strtolower(str_replace(' ', '', $this->settings['corp_id'])); |
||||
| 451 | $bodyData['CurrencyCode'] = 'idr'; |
||||
| 452 | $bodyData['ReferenceID'] = strtolower(str_replace(' ', '', $referenceID)); |
||||
| 453 | $bodyData['Remark1'] = strtolower(str_replace(' ', '', $remark1)); |
||||
| 454 | $bodyData['Remark2'] = strtolower(str_replace(' ', '', $remark2)); |
||||
| 455 | $bodyData['SourceAccountNumber'] = strtolower(str_replace(' ', '', $sourceAccountNumber)); |
||||
| 456 | $bodyData['TransactionDate'] = $isoTime; |
||||
| 457 | $bodyData['TransactionID'] = strtolower(str_replace(' ', '', $transactionID)); |
||||
| 458 | |||||
| 459 | // Harus disort agar mudah kalkulasi HMAC |
||||
| 460 | ksort($bodyData); |
||||
| 461 | |||||
| 462 | $authSignature = self::generateSign($uriSign, $oauth_token, $this->settings['secret_key'], $isoTime, $bodyData); |
||||
| 463 | |||||
| 464 | $headers['X-BCA-Signature'] = $authSignature; |
||||
| 465 | |||||
| 466 | // Supaya jgn strip "ReferenceID" "/" jadi "/\" karena HMAC akan menjadi tidak cocok |
||||
| 467 | $encoderData = json_encode($bodyData, JSON_UNESCAPED_SLASHES); |
||||
| 468 | |||||
| 469 | $body = Body::form($encoderData); |
||||
| 470 | $response = Request::post($full_url, $headers, $body); |
||||
| 471 | |||||
| 472 | return $response; |
||||
| 473 | } |
||||
| 474 | |||||
| 475 | /** |
||||
| 476 | * Realtime deposit untuk produk BCA. |
||||
| 477 | * |
||||
| 478 | * @param string $oauth_token nilai token yang telah didapatkan setelah login |
||||
| 479 | * |
||||
| 480 | * @return \Unirest\Response |
||||
| 481 | */ |
||||
| 482 | public function getDepositRate($oauth_token) |
||||
| 483 | { |
||||
| 484 | $uriSign = "GET:/general/rate/deposit"; |
||||
| 485 | $isoTime = self::generateIsoTime(); |
||||
| 486 | $authSignature = self::generateSign($uriSign, $oauth_token, $this->settings['secret_key'], $isoTime, null); |
||||
| 487 | |||||
| 488 | $headers = array(); |
||||
| 489 | $headers['Accept'] = 'application/json'; |
||||
| 490 | $headers['Content-Type'] = 'application/json'; |
||||
| 491 | $headers['Authorization'] = "Bearer $oauth_token"; |
||||
| 492 | $headers['X-BCA-Key'] = $this->settings['api_key']; |
||||
| 493 | $headers['X-BCA-Timestamp'] = $isoTime; |
||||
| 494 | $headers['X-BCA-Signature'] = $authSignature; |
||||
| 495 | |||||
| 496 | $request_path = "general/rate/deposit"; |
||||
| 497 | $domain = $this->ddnDomain(); |
||||
| 498 | $full_url = $domain . $request_path; |
||||
| 499 | |||||
| 500 | $data = array('grant_type' => 'client_credentials'); |
||||
| 501 | |||||
| 502 | $body = Body::form($data); |
||||
| 503 | $response = Request::get($full_url, $headers, $body); |
||||
| 504 | |||||
| 505 | return $response; |
||||
| 506 | } |
||||
| 507 | |||||
| 508 | /** |
||||
| 509 | * Generate Signature. |
||||
| 510 | * |
||||
| 511 | * @param string $url Url yang akan disign. |
||||
| 512 | * @param string $auth_token string nilai token dari login. |
||||
| 513 | * @param string $secret_key string secretkey yang telah diberikan oleh BCA. |
||||
| 514 | * @param string $isoTime string Waktu ISO8601. |
||||
| 515 | * @param array|mixed $bodyToHash array Body yang akan dikirimkan ke Server BCA. |
||||
| 516 | * |
||||
| 517 | * @return string |
||||
| 518 | */ |
||||
| 519 | public static function generateSign($url, $auth_token, $secret_key, $isoTime, $bodyToHash = []) |
||||
| 520 | { |
||||
| 521 | $hash = hash("sha256", ""); |
||||
| 522 | if (is_array($bodyToHash)) { |
||||
| 523 | ksort($bodyToHash); |
||||
| 524 | $encoderData = json_encode($bodyToHash, JSON_UNESCAPED_SLASHES); |
||||
| 525 | $hash = hash("sha256", $encoderData); |
||||
| 526 | } |
||||
| 527 | $stringToSign = $url . ":" . $auth_token . ":" . $hash . ":" . $isoTime; |
||||
| 528 | $auth_signature = hash_hmac('sha256', $stringToSign, $secret_key, false); |
||||
| 529 | |||||
| 530 | return $auth_signature; |
||||
| 531 | } |
||||
| 532 | |||||
| 533 | /** |
||||
| 534 | * Set TimeZone. |
||||
| 535 | * |
||||
| 536 | * @param string $timeZone Time yang akan dipergunakan. |
||||
| 537 | * |
||||
| 538 | * @return string |
||||
| 539 | */ |
||||
| 540 | public static function setTimeZone($timeZone) |
||||
| 541 | { |
||||
| 542 | self::$timezone = $timeZone; |
||||
| 543 | } |
||||
| 544 | |||||
| 545 | /** |
||||
| 546 | * Get TimeZone. |
||||
| 547 | * |
||||
| 548 | * @return string |
||||
| 549 | */ |
||||
| 550 | public static function getTimeZone() |
||||
| 551 | { |
||||
| 552 | return self::$timezone; |
||||
| 553 | } |
||||
| 554 | |||||
| 555 | /** |
||||
| 556 | * Set nama domain BCA yang akan dipergunakan. |
||||
| 557 | * |
||||
| 558 | * @param string $hostName nama domain BCA yang akan dipergunakan. |
||||
| 559 | * |
||||
| 560 | * @return string |
||||
| 561 | */ |
||||
| 562 | public static function setHostName($hostName) |
||||
| 563 | { |
||||
| 564 | self::$hostName = $hostName; |
||||
| 565 | } |
||||
| 566 | |||||
| 567 | /** |
||||
| 568 | * Ambil nama domain BCA yang akan dipergunakan. |
||||
| 569 | * |
||||
| 570 | * @return string |
||||
| 571 | */ |
||||
| 572 | public static function getHostName() |
||||
| 573 | { |
||||
| 574 | return self::$hostName; |
||||
| 575 | } |
||||
| 576 | |||||
| 577 | /** |
||||
| 578 | * Ambil maximum execution time. |
||||
| 579 | * |
||||
| 580 | * @return string |
||||
| 581 | */ |
||||
| 582 | public static function getTimeOut() |
||||
| 583 | { |
||||
| 584 | return self::$timeOut; |
||||
| 585 | } |
||||
| 586 | |||||
| 587 | /** |
||||
| 588 | * Ambil nama domain BCA yang akan dipergunakan. |
||||
| 589 | * |
||||
| 590 | * @return string |
||||
| 591 | */ |
||||
| 592 | public static function getCurlOptions() |
||||
| 593 | { |
||||
| 594 | return self::$curlOptions; |
||||
| 595 | } |
||||
| 596 | |||||
| 597 | /** |
||||
| 598 | * Setup curl options. |
||||
| 599 | * |
||||
| 600 | * @param array $curlOpts |
||||
| 601 | * @return array |
||||
| 602 | */ |
||||
| 603 | public static function setCurlOptions(array $curlOpts = []) |
||||
| 604 | { |
||||
| 605 | $data = self::mergeCurlOptions(self::$curlOptions, $curlOpts); |
||||
|
0 ignored issues
–
show
self::curlOptions of type integer is incompatible with the type array expected by parameter $existing_options of Bca\BcaHttp::mergeCurlOptions().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 606 | self::$curlOptions = $data; |
||||
|
0 ignored issues
–
show
It seems like
$data of type array is incompatible with the declared type integer of property $curlOptions.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||||
| 607 | } |
||||
| 608 | |||||
| 609 | /** |
||||
| 610 | * Set Ambil maximum execution time. |
||||
| 611 | * |
||||
| 612 | * @param int $timeOut timeout in milisecond. |
||||
| 613 | * |
||||
| 614 | * @return string |
||||
| 615 | */ |
||||
| 616 | public static function setTimeOut($timeOut) |
||||
| 617 | { |
||||
| 618 | self::$timeOut = $timeOut; |
||||
| 619 | return self::$timeOut; |
||||
| 620 | } |
||||
| 621 | |||||
| 622 | /** |
||||
| 623 | * Set BCA port |
||||
| 624 | * |
||||
| 625 | * @param int $port Port yang akan dipergunakan |
||||
| 626 | * |
||||
| 627 | * @return int |
||||
| 628 | */ |
||||
| 629 | public static function setPort($port) |
||||
| 630 | { |
||||
| 631 | self::$port = $port; |
||||
| 632 | } |
||||
| 633 | |||||
| 634 | /** |
||||
| 635 | * Get BCA port |
||||
| 636 | * |
||||
| 637 | * @return int |
||||
| 638 | */ |
||||
| 639 | public static function getPort() |
||||
| 640 | { |
||||
| 641 | return self::$port; |
||||
| 642 | } |
||||
| 643 | |||||
| 644 | /** |
||||
| 645 | * Set BCA Schema |
||||
| 646 | * |
||||
| 647 | * @param int $scheme Scheme yang akan dipergunakan |
||||
| 648 | * |
||||
| 649 | * @return string |
||||
| 650 | */ |
||||
| 651 | public static function setScheme($scheme) |
||||
| 652 | { |
||||
| 653 | self::$scheme = $scheme; |
||||
| 654 | } |
||||
| 655 | |||||
| 656 | /** |
||||
| 657 | * Get BCA Schema |
||||
| 658 | * |
||||
| 659 | * @return string |
||||
| 660 | */ |
||||
| 661 | public static function getScheme() |
||||
| 662 | { |
||||
| 663 | return self::$scheme; |
||||
| 664 | } |
||||
| 665 | |||||
| 666 | /** |
||||
| 667 | * Generate ISO8601 Time. |
||||
| 668 | * |
||||
| 669 | * @param string $timeZone Time yang akan dipergunakan |
||||
| 670 | * |
||||
| 671 | * @return string |
||||
| 672 | */ |
||||
| 673 | public static function generateIsoTime() |
||||
| 674 | { |
||||
| 675 | $date = Carbon::now(self::getTimeZone()); |
||||
| 676 | date_default_timezone_set(self::getTimeZone()); |
||||
| 677 | $fmt = $date->format('Y-m-d\TH:i:s'); |
||||
| 678 | $ISO8601 = sprintf("$fmt.%s%s", substr(microtime(), 2, 3), date('P')); |
||||
| 679 | |||||
| 680 | return $ISO8601; |
||||
| 681 | } |
||||
| 682 | |||||
| 683 | /** |
||||
| 684 | * Merge from existing array. |
||||
| 685 | * |
||||
| 686 | * @param array $existing_options |
||||
| 687 | * @param array $new_options |
||||
| 688 | * @return array |
||||
| 689 | */ |
||||
| 690 | private static function mergeCurlOptions(&$existing_options, $new_options) |
||||
| 691 | { |
||||
| 692 | $existing_options = $new_options + $existing_options; |
||||
| 693 | return $existing_options; |
||||
| 694 | } |
||||
| 695 | |||||
| 696 | /** |
||||
| 697 | * Validasi jika clientsecret telah di-definsikan. |
||||
| 698 | * |
||||
| 699 | * @param array $sourceAccountId |
||||
| 700 | * |
||||
| 701 | * @throws BcaHttpException Error jika array tidak memenuhi syarat |
||||
| 702 | * @return bool |
||||
| 703 | */ |
||||
| 704 | private function validateArray($sourceAccountId = []) |
||||
| 705 | { |
||||
| 706 | if (!is_array($sourceAccountId)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 707 | throw new BcaHttpException('Data harus array.'); |
||||
| 708 | } |
||||
| 709 | if (empty($sourceAccountId)) { |
||||
| 710 | throw new BcaHttpException('AccountNumber tidak boleh kosong.'); |
||||
| 711 | } else { |
||||
| 712 | $max = sizeof($sourceAccountId); |
||||
| 713 | if ($max > 20) { |
||||
| 714 | throw new BcaHttpException('Maksimal Account Number ' . 20); |
||||
| 715 | } |
||||
| 716 | } |
||||
| 717 | |||||
| 718 | return true; |
||||
| 719 | } |
||||
| 720 | |||||
| 721 | /** |
||||
| 722 | * Implode an array with the key and value pair giving |
||||
| 723 | * a glue, a separator between pairs and the array |
||||
| 724 | * to implode. |
||||
| 725 | * |
||||
| 726 | * @param string $glue The glue between key and value |
||||
| 727 | * @param string $separator Separator between pairs |
||||
| 728 | * @param array $array The array to implode |
||||
| 729 | * |
||||
| 730 | * @throws BcaHttpException error |
||||
| 731 | * @return string The imploded array |
||||
| 732 | */ |
||||
| 733 | public static function arrayImplode($glue, $separator, $array = []) |
||||
| 734 | { |
||||
| 735 | if (!is_array($array)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 736 | throw new BcaHttpException('Data harus array.'); |
||||
| 737 | } |
||||
| 738 | if (empty($array)) { |
||||
| 739 | throw new BcaHttpException('parameter array tidak boleh kosong.'); |
||||
| 740 | } |
||||
| 741 | foreach ($array as $key => $val) { |
||||
| 742 | if (is_array($val)) { |
||||
| 743 | $val = implode(',', $val); |
||||
| 744 | } |
||||
| 745 | $string[] = "{$key}{$glue}{$val}"; |
||||
| 746 | } |
||||
| 747 | |||||
| 748 | return implode($separator, $string); |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 749 | } |
||||
| 750 | } |
||||
| 751 |