| Total Complexity | 43 | 
| Total Lines | 674 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like BcaHttp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BcaHttp, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 12 | class BcaHttp | ||
| 13 | { | ||
| 14 | public static $VERSION = '2.1.0'; | ||
| 15 | |||
| 16 | private static $timezone = 'Asia/Jakarta'; | ||
| 17 | |||
| 18 | private static $port = 443; | ||
| 19 | |||
| 20 | private static $hostName = 'sandbox.bca.co.id'; | ||
| 21 | |||
| 22 | protected $settings = array( | ||
| 23 | 'corp_id' => '', | ||
| 24 | 'client_id' => '', | ||
| 25 | 'client_secret' => '', | ||
| 26 | 'api_key' => '', | ||
| 27 | 'secret_key' => '', | ||
| 28 | 'scheme' => 'https', | ||
| 29 | 'port' => 443, | ||
| 30 | 'timezone' => 'Asia/Jakarta', | ||
| 31 | 'timeout' => null, | ||
| 32 | 'development' => true, | ||
| 33 | ); | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Default Constructor. | ||
| 37 | * | ||
| 38 | * @param string $corp_id nilai corp id | ||
| 39 | * @param string $client_id nilai client key | ||
| 40 | * @param string $client_secret nilai client secret | ||
| 41 | * @param string $api_key niali oauth key | ||
| 42 | * @param string $secret_key nilai oauth secret | ||
| 43 | * @param array $options opsi ke server bca | ||
| 44 | */ | ||
| 45 | public function __construct($corp_id, $client_id, $client_secret, $api_key, $secret_key, $options = array()) | ||
| 46 |     { | ||
| 47 |         if (!isset($options['port'])) { | ||
| 48 | $options['port'] = self::getPort(); | ||
| 49 | } | ||
| 50 | |||
| 51 |         if (!isset($options['timezone'])) { | ||
| 52 | $options['timezone'] = self::getTimeZone(); | ||
| 53 | } | ||
| 54 | |||
| 55 |         foreach ($options as $key => $value) { | ||
| 56 |             if (isset($this->settings[$key])) { | ||
| 57 | $this->settings[$key] = $value; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 |         if (!array_key_exists('host', $this->settings)) { | ||
| 62 |             if (array_key_exists('host', $options)) { | ||
| 63 | $this->settings['host'] = $options['host']; | ||
| 64 |             } else { | ||
| 65 | $this->settings['host'] = self::getHostName(); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | $this->settings['corp_id'] = $corp_id; | ||
| 70 | $this->settings['client_id'] = $client_id; | ||
| 71 | $this->settings['client_secret'] = $client_secret; | ||
| 72 | $this->settings['api_key'] = $api_key; | ||
| 73 | $this->settings['secret_key'] = $secret_key; | ||
| 74 | |||
| 75 | $this->settings['host'] = | ||
| 76 |             preg_replace('/http[s]?\:\/\//', '', $this->settings['host'], 1); | ||
| 77 | } | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Ambil Nilai settings. | ||
| 81 | * | ||
| 82 | * @return array | ||
| 83 | */ | ||
| 84 | public function getSettings() | ||
| 85 |     { | ||
| 86 | return $this->settings; | ||
| 87 | } | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Build the ddn domain. | ||
| 91 | * output = 'https://sandbox.bca.co.id:443' | ||
| 92 | * scheme = http(s) | ||
| 93 | * host = sandbox.bca.co.id | ||
| 94 | * port = 80 ? 443 | ||
| 95 | * | ||
| 96 | * @return string | ||
| 97 | */ | ||
| 98 | private function ddnDomain() | ||
| 99 |     { | ||
| 100 | return $this->settings['scheme'] . '://' . $this->settings['host'] . ':' . $this->settings['port'] . '/'; | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Generate authentifikasi ke server berupa OAUTH. | ||
| 105 | * | ||
| 106 | * @return \Unirest\Response | ||
| 107 | */ | ||
| 108 | public function httpAuth() | ||
| 109 |     { | ||
| 110 | $client_id = $this->settings['client_id']; | ||
| 111 | $client_secret = $this->settings['client_secret']; | ||
| 112 | |||
| 113 | $this->validateBcaKey($client_id); | ||
| 114 | $this->validateBcaKey($client_secret); | ||
| 115 | |||
| 116 |         $headerToken = base64_encode("$client_id:$client_secret"); | ||
| 117 | |||
| 118 |         $headers = array('Accept' => 'application/json', 'Authorization' => "Basic $headerToken"); | ||
| 119 | |||
| 120 | $request_path = "api/oauth/token"; | ||
| 121 | $domain = $this->ddnDomain(); | ||
| 122 | $full_url = $domain . $request_path; | ||
| 123 | |||
| 124 | \Unirest\Request::curlOpts(array( | ||
| 125 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 126 | CURLOPT_SSLVERSION => 6, | ||
| 127 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 128 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 129 | )); | ||
| 130 | |||
| 131 |         $data = array('grant_type' => 'client_credentials'); | ||
| 132 | $body = \Unirest\Request\Body::form($data); | ||
| 133 | $response = \Unirest\Request::post($full_url, $headers, $body); | ||
| 134 | |||
| 135 | return $response; | ||
| 136 | } | ||
| 137 | |||
| 138 | /** | ||
| 139 | * Ambil informasi saldo berdasarkan nomor akun BCA. | ||
| 140 | * | ||
| 141 | * @param string $oauth_token nilai token yang telah didapatkan setelah login | ||
| 142 | * @param array $sourceAccountId nomor akun yang akan dicek | ||
| 143 | * | ||
| 144 | * @return \Unirest\Response | ||
| 145 | */ | ||
| 146 | public function getBalanceInfo($oauth_token, $sourceAccountId = []) | ||
| 147 |     { | ||
| 148 | $corp_id = $this->settings['corp_id']; | ||
| 149 | $apikey = $this->settings['api_key']; | ||
| 150 | $secret = $this->settings['secret_key']; | ||
| 151 | |||
| 152 | $this->validateCorpId($corp_id); | ||
| 153 | $this->validateBcaKey($apikey); | ||
| 154 | $this->validateBcaKey($secret); | ||
| 155 | $this->validateArray($sourceAccountId); | ||
| 156 | |||
| 157 | ksort($sourceAccountId); | ||
| 158 |         $arraySplit = implode(",", $sourceAccountId); | ||
| 159 | $arraySplit = urlencode($arraySplit); | ||
| 160 | |||
| 161 | $uriSign = "GET:/banking/v3/corporates/$corp_id/accounts/$arraySplit"; | ||
| 162 | $isoTime = self::generateIsoTime(); | ||
| 163 | $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, null); | ||
| 164 | |||
| 165 | $headers = array(); | ||
| 166 | $headers['Accept'] = 'application/json'; | ||
| 167 | $headers['Content-Type'] = 'application/json'; | ||
| 168 | $headers['Authorization'] = "Bearer $oauth_token"; | ||
| 169 | $headers['X-BCA-Key'] = $apikey; | ||
| 170 | $headers['X-BCA-Timestamp'] = $isoTime; | ||
| 171 | $headers['X-BCA-Signature'] = $authSignature; | ||
| 172 | |||
| 173 | $request_path = "banking/v3/corporates/$corp_id/accounts/$arraySplit"; | ||
| 174 | $domain = $this->ddnDomain(); | ||
| 175 | $full_url = $domain . $request_path; | ||
| 176 | |||
| 177 |         $data     = array('grant_type' => 'client_credentials'); | ||
| 178 | |||
| 179 | \Unirest\Request::curlOpts(array( | ||
| 180 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 181 | CURLOPT_SSLVERSION => 6, | ||
| 182 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 183 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 184 | )); | ||
| 185 | |||
| 186 | $body = \Unirest\Request\Body::form($data); | ||
| 187 | $response = \Unirest\Request::get($full_url, $headers, $body); | ||
| 188 | |||
| 189 | return $response; | ||
| 190 | } | ||
| 191 | |||
| 192 | /** | ||
| 193 | * Ambil Daftar transaksi pertanggal. | ||
| 194 | * | ||
| 195 | * @param string $oauth_token nilai token yang telah didapatkan setelah login | ||
| 196 | * @param array $sourceAccount nomor akun yang akan dicek | ||
| 197 | * @param string $startDate tanggal awal | ||
| 198 | * @param string $endDate tanggal akhir | ||
| 199 | * @param string $corp_id nilai CorporateID yang telah diberikan oleh pihak BCA | ||
| 200 | * | ||
| 201 | * @return \Unirest\Response | ||
| 202 | */ | ||
| 203 | public function getAccountStatement($oauth_token, $sourceAccount, $startDate, $endDate) | ||
| 204 |     { | ||
| 205 | $corp_id = $this->settings['corp_id']; | ||
| 206 | |||
| 207 | $apikey = $this->settings['api_key']; | ||
| 208 | |||
| 209 | $secret = $this->settings['secret_key']; | ||
| 210 | |||
| 211 | $this->validateCorpId($corp_id); | ||
| 212 | $this->validateBcaKey($apikey); | ||
| 213 | $this->validateBcaKey($secret); | ||
| 214 | |||
| 215 | $uriSign = "GET:/banking/v3/corporates/$corp_id/accounts/$sourceAccount/statements?EndDate=$endDate&StartDate=$startDate"; | ||
| 216 | $isoTime = self::generateIsoTime(); | ||
| 217 | $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, null); | ||
| 218 | |||
| 219 | $headers = array(); | ||
| 220 | $headers['Accept'] = 'application/json'; | ||
| 221 | $headers['Content-Type'] = 'application/json'; | ||
| 222 | $headers['Authorization'] = "Bearer $oauth_token"; | ||
| 223 | $headers['X-BCA-Key'] = $apikey; | ||
| 224 | $headers['X-BCA-Timestamp'] = $isoTime; | ||
| 225 | $headers['X-BCA-Signature'] = $authSignature; | ||
| 226 | |||
| 227 | $request_path = "banking/v3/corporates/$corp_id/accounts/$sourceAccount/statements?EndDate=$endDate&StartDate=$startDate"; | ||
| 228 | $domain = $this->ddnDomain(); | ||
| 229 | $full_url = $domain . $request_path; | ||
| 230 | |||
| 231 | \Unirest\Request::curlOpts(array( | ||
| 232 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 233 | CURLOPT_SSLVERSION => 6, | ||
| 234 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 235 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 236 | )); | ||
| 237 | |||
| 238 |         $data     = array('grant_type' => 'client_credentials'); | ||
| 239 | $body = \Unirest\Request\Body::form($data); | ||
| 240 | $response = \Unirest\Request::get($full_url, $headers, $body); | ||
| 241 | |||
| 242 | return $response; | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * Ambil informasi ATM berdasarkan lokasi GEO. | ||
| 247 | * | ||
| 248 | * @param string $oauth_token nilai token yang telah didapatkan setelah login | ||
| 249 | * @param string $latitude Langitude GPS | ||
| 250 | * @param string $longitude Longitude GPS | ||
| 251 | * @param string $count Jumlah ATM BCA yang akan ditampilkan | ||
| 252 | * @param string $radius Nilai radius dari lokasi GEO | ||
| 253 | * | ||
| 254 | * @return \Unirest\Response | ||
| 255 | */ | ||
| 256 | public function getAtmLocation( | ||
| 257 | $oauth_token, | ||
| 258 | $latitude, | ||
| 259 | $longitude, | ||
| 260 | $count = '10', | ||
| 261 | $radius = '20' | ||
| 262 |     ) { | ||
| 263 | $apikey = $this->settings['api_key']; | ||
| 264 | |||
| 265 | $secret = $this->settings['secret_key']; | ||
| 266 | |||
| 267 | $this->validateBcaKey($apikey); | ||
| 268 | $this->validateBcaKey($secret); | ||
| 269 | |||
| 270 | $params = array(); | ||
| 271 | $params['SearchBy'] = 'Distance'; | ||
| 272 | $params['Latitude'] = $latitude; | ||
| 273 | $params['Longitude'] = $longitude; | ||
| 274 | $params['Count'] = $count; | ||
| 275 | $params['Radius'] = $radius; | ||
| 276 | ksort($params); | ||
| 277 | |||
| 278 |         $auth_query_string = self::arrayImplode('=', '&', $params); | ||
| 279 | |||
| 280 | $uriSign = "GET:/general/info-bca/atm?$auth_query_string"; | ||
| 281 | $isoTime = self::generateIsoTime(); | ||
| 282 | $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, null); | ||
| 283 | |||
| 284 | $headers = array(); | ||
| 285 | $headers['Accept'] = 'application/json'; | ||
| 286 | $headers['Content-Type'] = 'application/json'; | ||
| 287 | $headers['Authorization'] = "Bearer $oauth_token"; | ||
| 288 | $headers['X-BCA-Key'] = $apikey; | ||
| 289 | $headers['X-BCA-Timestamp'] = $isoTime; | ||
| 290 | $headers['X-BCA-Signature'] = $authSignature; | ||
| 291 | |||
| 292 | $request_path = "general/info-bca/atm?SearchBy=Distance&Latitude=$latitude&Longitude=$longitude&Count=$count&Radius=$radius"; | ||
| 293 | $domain = $this->ddnDomain(); | ||
| 294 | $full_url = $domain . $request_path; | ||
| 295 | |||
| 296 | \Unirest\Request::curlOpts(array( | ||
| 297 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 298 | CURLOPT_SSLVERSION => 6, | ||
| 299 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 300 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 301 | )); | ||
| 302 | |||
| 303 |         $data     = array('grant_type' => 'client_credentials'); | ||
| 304 | $body = \Unirest\Request\Body::form($data); | ||
| 305 | $response = \Unirest\Request::get($full_url, $headers, $body); | ||
| 306 | |||
| 307 | return $response; | ||
| 308 | } | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Ambil KURS mata uang. | ||
| 312 | * | ||
| 313 | * @param string $oauth_token nilai token yang telah didapatkan setelah login | ||
| 314 | * @param string $rateType type rate | ||
| 315 | * @param string $currency Mata uang | ||
| 316 | * | ||
| 317 | * @return \Unirest\Response | ||
| 318 | */ | ||
| 319 | public function getForexRate( | ||
| 320 | $oauth_token, | ||
| 321 | $rateType = 'e-rate', | ||
| 322 | $currency = 'USD' | ||
| 323 |     ) { | ||
| 324 | $apikey = $this->settings['api_key']; | ||
| 325 | |||
| 326 | $secret = $this->settings['secret_key']; | ||
| 327 | |||
| 328 | $this->validateBcaKey($apikey); | ||
| 329 | $this->validateBcaKey($secret); | ||
| 330 | |||
| 331 | $params = array(); | ||
| 332 | $params['RateType'] = strtolower($rateType); | ||
| 333 | $params['Currency'] = strtoupper($currency); | ||
| 334 | ksort($params); | ||
| 335 | |||
| 336 |         $auth_query_string = self::arrayImplode('=', '&', $params); | ||
| 337 | |||
| 338 | $uriSign = "GET:/general/rate/forex?$auth_query_string"; | ||
| 339 | $isoTime = self::generateIsoTime(); | ||
| 340 | $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, null); | ||
| 341 | |||
| 342 | $headers = array(); | ||
| 343 | $headers['Accept'] = 'application/json'; | ||
| 344 | $headers['Content-Type'] = 'application/json'; | ||
| 345 | $headers['Authorization'] = "Bearer $oauth_token"; | ||
| 346 | $headers['X-BCA-Key'] = $apikey; | ||
| 347 | $headers['X-BCA-Timestamp'] = $isoTime; | ||
| 348 | $headers['X-BCA-Signature'] = $authSignature; | ||
| 349 | |||
| 350 | $request_path = "general/rate/forex?$auth_query_string"; | ||
| 351 | $domain = $this->ddnDomain(); | ||
| 352 | $full_url = $domain . $request_path; | ||
| 353 | |||
| 354 | \Unirest\Request::curlOpts(array( | ||
| 355 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 356 | CURLOPT_SSLVERSION => 6, | ||
| 357 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 358 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 359 | )); | ||
| 360 | |||
| 361 |         $data     = array('grant_type' => 'client_credentials'); | ||
| 362 | $body = \Unirest\Request\Body::form($data); | ||
| 363 | $response = \Unirest\Request::get($full_url, $headers, $body); | ||
| 364 | |||
| 365 | return $response; | ||
| 366 | } | ||
| 367 | |||
| 368 | /** | ||
| 369 | * Transfer dana kepada akun lain dengan jumlah nominal tertentu. | ||
| 370 | * | ||
| 371 | * @param string $oauth_token nilai token yang telah didapatkan setelah login | ||
| 372 | * @param int $amount nilai dana dalam RUPIAH yang akan ditransfer, Format: 13.2 | ||
| 373 | * @param string $beneficiaryAccountNumber BCA Account number to be credited (Destination) | ||
| 374 | * @param string $referenceID Sender's transaction reference ID | ||
| 375 | * @param string $remark1 Transfer remark for receiver | ||
| 376 | * @param string $remark2 ransfer remark for receiver | ||
| 377 | * @param string $sourceAccountNumber Source of Fund Account Number | ||
| 378 | * @param string $transactionID Transcation ID unique per day (using UTC+07 Time Zone). Format: Number | ||
| 379 | * @param string $corp_id nilai CorporateID yang telah diberikan oleh pihak BCA [Optional] | ||
| 380 | * | ||
| 381 | * @return \Unirest\Response | ||
| 382 | */ | ||
| 383 | public function fundTransfers( | ||
| 384 | $oauth_token, | ||
| 385 | $amount, | ||
| 386 | $sourceAccountNumber, | ||
| 387 | $beneficiaryAccountNumber, | ||
| 388 | $referenceID, | ||
| 389 | $remark1, | ||
| 390 | $remark2, | ||
| 391 | $transactionID | ||
| 392 |     ) { | ||
| 393 | $corp_id = $this->settings['corp_id']; | ||
| 394 | $apikey = $this->settings['api_key']; | ||
| 395 | $secret = $this->settings['secret_key']; | ||
| 396 | |||
| 397 | $this->validateCorpId($corp_id); | ||
| 398 | $this->validateBcaKey($apikey); | ||
| 399 | $this->validateBcaKey($secret); | ||
| 400 | |||
| 401 | $uriSign = "POST:/banking/corporates/transfers"; | ||
| 402 | |||
| 403 | $isoTime = self::generateIsoTime(); | ||
| 404 | |||
| 405 | $headers = array(); | ||
| 406 | $headers['Accept'] = 'application/json'; | ||
| 407 | $headers['Content-Type'] = 'application/json'; | ||
| 408 | $headers['Authorization'] = "Bearer $oauth_token"; | ||
| 409 | $headers['X-BCA-Key'] = $apikey; | ||
| 410 | $headers['X-BCA-Timestamp'] = $isoTime; | ||
| 411 | |||
| 412 | $request_path = "banking/corporates/transfers"; | ||
| 413 | $domain = $this->ddnDomain(); | ||
| 414 | $full_url = $domain . $request_path; | ||
| 415 | |||
| 416 | $bodyData = array(); | ||
| 417 | $bodyData['Amount'] = $amount; | ||
| 418 | $bodyData['BeneficiaryAccountNumber'] = $beneficiaryAccountNumber; | ||
| 419 | $bodyData['CorporateID'] = $corp_id; | ||
| 420 | $bodyData['CurrencyCode'] = 'IDR'; | ||
| 421 | $bodyData['ReferenceID'] = $referenceID; | ||
| 422 |         $bodyData['Remark1']                  = strtolower(str_replace(' ', '', $remark1)); | ||
| 423 |         $bodyData['Remark2']                  = strtolower(str_replace(' ', '', $remark2)); | ||
| 424 | $bodyData['SourceAccountNumber'] = $sourceAccountNumber; | ||
| 425 | $bodyData['TransactionDate'] = $isoTime; | ||
| 426 | $bodyData['TransactionID'] = $transactionID; | ||
| 427 | |||
| 428 | // Harus disort agar mudah kalkulasi HMAC | ||
| 429 | ksort($bodyData); | ||
| 430 | |||
| 431 | // Supaya jgn strip "ReferenceID" "/" jadi "/\" karena HMAC akan menjadi tidak cocok | ||
| 432 | $encoderData = json_encode($bodyData, JSON_UNESCAPED_SLASHES); | ||
| 433 | |||
| 434 | $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, $bodyData); | ||
| 435 | |||
| 436 | $headers['X-BCA-Signature'] = $authSignature; | ||
| 437 | |||
| 438 | \Unirest\Request::curlOpts(array( | ||
| 439 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 440 | CURLOPT_SSLVERSION => 6, | ||
| 441 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 442 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 443 | )); | ||
| 444 | $body = \Unirest\Request\Body::form($encoderData); | ||
| 445 | $response = \Unirest\Request::post($full_url, $headers, $body); | ||
| 446 | |||
| 447 | return $response; | ||
| 448 | } | ||
| 449 | |||
| 450 | /** | ||
| 451 | * Realtime deposit untuk produk BCA. | ||
| 452 | * | ||
| 453 | * @param string $oauth_token nilai token yang telah didapatkan setelah login | ||
| 454 | * | ||
| 455 | * @return \Unirest\Response | ||
| 456 | */ | ||
| 457 | public function getDepositRate($oauth_token) | ||
| 458 |     { | ||
| 459 | $corp_id = $this->settings['corp_id']; | ||
| 460 | $apikey = $this->settings['api_key']; | ||
| 461 | $secret = $this->settings['secret_key']; | ||
| 462 | |||
| 463 | $this->validateCorpId($corp_id); | ||
| 464 | $this->validateBcaKey($apikey); | ||
| 465 | $this->validateBcaKey($secret); | ||
| 466 | |||
| 467 | $uriSign = "GET:/general/rate/deposit"; | ||
| 468 | $isoTime = self::generateIsoTime(); | ||
| 469 | $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, null); | ||
| 470 | |||
| 471 | $headers = array(); | ||
| 472 | $headers['Accept'] = 'application/json'; | ||
| 473 | $headers['Content-Type'] = 'application/json'; | ||
| 474 | $headers['Authorization'] = "Bearer $oauth_token"; | ||
| 475 | $headers['X-BCA-Key'] = $apikey; | ||
| 476 | $headers['X-BCA-Timestamp'] = $isoTime; | ||
| 477 | $headers['X-BCA-Signature'] = $authSignature; | ||
| 478 | |||
| 479 | $request_path = "general/rate/deposit"; | ||
| 480 | $domain = $this->ddnDomain(); | ||
| 481 | $full_url = $domain . $request_path; | ||
| 482 | |||
| 483 |         $data     = array('grant_type' => 'client_credentials'); | ||
| 484 | |||
| 485 | \Unirest\Request::curlOpts(array( | ||
| 486 | CURLOPT_SSL_VERIFYHOST => 0, | ||
| 487 | CURLOPT_SSLVERSION => 6, | ||
| 488 | CURLOPT_SSL_VERIFYPEER => false, | ||
| 489 | CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30 | ||
| 490 | )); | ||
| 491 | |||
| 492 | $body = \Unirest\Request\Body::form($data); | ||
| 493 | $response = \Unirest\Request::get($full_url, $headers, $body); | ||
| 494 | |||
| 495 | return $response; | ||
| 496 | } | ||
| 497 | |||
| 498 | /** | ||
| 499 | * Generate Signature. | ||
| 500 | * | ||
| 501 | * @param string $url Url yang akan disign | ||
| 502 | * @param string $auth_token string nilai token dari login | ||
| 503 | * @param string $secret_key string secretkey yang telah diberikan oleh BCA | ||
| 504 | * @param string $isoTime string Waktu ISO8601 | ||
| 505 | * @param array $bodyToHash array Body yang akan dikirimkan ke Server BCA | ||
| 506 | * | ||
| 507 | * @return string | ||
| 508 | */ | ||
| 509 | public static function generateSign($url, $auth_token, $secret_key, $isoTime, $bodyToHash) | ||
| 510 |     { | ||
| 511 | $hash = null; | ||
| 512 |         if (is_array($bodyToHash)) { | ||
|  | |||
| 513 | ksort($bodyToHash); | ||
| 514 | $encoderData = json_encode($bodyToHash, JSON_UNESCAPED_SLASHES); | ||
| 515 |             $hash        = hash("sha256", $encoderData); | ||
| 516 |         } else { | ||
| 517 |             $hash = hash("sha256", ""); | ||
| 518 | } | ||
| 519 | $stringToSign = $url . ":" . $auth_token . ":" . $hash . ":" . $isoTime; | ||
| 520 |         $auth_signature = hash_hmac('sha256', $stringToSign, $secret_key, false); | ||
| 521 | |||
| 522 | return $auth_signature; | ||
| 523 | } | ||
| 524 | |||
| 525 | /** | ||
| 526 | * Set TimeZone. | ||
| 527 | * | ||
| 528 | * @param string $timeZone Time yang akan dipergunakan. | ||
| 529 | * | ||
| 530 | * @return string | ||
| 531 | */ | ||
| 532 | public static function setTimeZone($timeZone) | ||
| 533 |     { | ||
| 534 | self::$timezone = $timeZone; | ||
| 535 | } | ||
| 536 | |||
| 537 | /** | ||
| 538 | * Get TimeZone. | ||
| 539 | * | ||
| 540 | * @return string | ||
| 541 | */ | ||
| 542 | public static function getTimeZone() | ||
| 545 | } | ||
| 546 | |||
| 547 | /** | ||
| 548 | * Set nama domain BCA yang akan dipergunakan. | ||
| 549 | * | ||
| 550 | * @param string $hostName nama domain BCA yang akan dipergunakan. | ||
| 551 | * | ||
| 552 | * @return string | ||
| 553 | */ | ||
| 554 | public static function setHostName($hostName) | ||
| 555 |     { | ||
| 556 | self::$hostName = $hostName; | ||
| 557 | } | ||
| 558 | |||
| 559 | /** | ||
| 560 | * Ambil nama domain BCA yang akan dipergunakan. | ||
| 561 | * | ||
| 562 | * @return string | ||
| 563 | */ | ||
| 564 | public static function getHostName() | ||
| 565 |     { | ||
| 566 | return self::$hostName; | ||
| 567 | } | ||
| 568 | |||
| 569 | /** | ||
| 570 | * Set BCA port | ||
| 571 | * | ||
| 572 | * @param int $port Port yang akan dipergunakan | ||
| 573 | * | ||
| 574 | * @return int | ||
| 575 | */ | ||
| 576 | public static function setPort($port) | ||
| 579 | } | ||
| 580 | |||
| 581 | /** | ||
| 582 | * Get BCA port | ||
| 583 | * | ||
| 584 | * @return int | ||
| 585 | */ | ||
| 586 | public static function getPort() | ||
| 587 |     { | ||
| 588 | return self::$port; | ||
| 589 | } | ||
| 590 | |||
| 591 | /** | ||
| 592 | * Generate ISO8601 Time. | ||
| 593 | * | ||
| 594 | * @param string $timeZone Time yang akan dipergunakan | ||
| 595 | * | ||
| 596 | * @return string | ||
| 597 | */ | ||
| 598 | public static function generateIsoTime() | ||
| 599 |     { | ||
| 600 | $date = \Carbon\Carbon::now(self::getTimeZone()); | ||
| 601 | date_default_timezone_set(self::getTimeZone()); | ||
| 602 |         $fmt     = $date->format('Y-m-d\TH:i:s'); | ||
| 603 |         $ISO8601 = sprintf("$fmt.%s%s", substr(microtime(), 2, 3), date('P')); | ||
| 604 | |||
| 605 | return $ISO8601; | ||
| 606 | } | ||
| 607 | |||
| 608 | /** | ||
| 609 | * Validasi CORP_ID yang telah diberikan pihahk BCA. | ||
| 610 | * | ||
| 611 | * @param string $corpId | ||
| 612 | * | ||
| 613 | * @return bool | ||
| 614 | */ | ||
| 615 | private function validateCorpId($corpId) | ||
| 616 |     { | ||
| 617 |         if (!preg_match('/\A[-a-zA-Z0-9_=@,.;]+\z/', $corpId)) { | ||
| 618 |             throw new BcaHttpException('Invalid CorpId' . $corpId); | ||
| 619 | } | ||
| 620 | |||
| 621 | return true; | ||
| 622 | } | ||
| 623 | |||
| 624 | /** | ||
| 625 | * Validasi Key yang telah BCA tentukan. | ||
| 626 | * Format 1234567-1234-1234-1345-123456789123 | ||
| 627 | * | ||
| 628 | * @param string $key | ||
| 629 | * | ||
| 630 | * @return bool | ||
| 631 | */ | ||
| 632 | private function validateBcaKey($key) | ||
| 639 | } | ||
| 640 | |||
| 641 | /** | ||
| 642 | * Validasi jika clientsecret telah di-definsikan. | ||
| 643 | * | ||
| 644 | * @param array $sourceAccountId | ||
| 645 | * | ||
| 646 | * @return bool | ||
| 647 | */ | ||
| 648 | private function validateArray($sourceAccountId = []) | ||
| 659 | } | ||
| 660 | |||
| 661 | /** | ||
| 662 | * Implode an array with the key and value pair giving | ||
| 663 | * a glue, a separator between pairs and the array | ||
| 664 | * to implode. | ||
| 665 | * | ||
| 666 | * @param string $glue The glue between key and value | ||
| 667 | * @param string $separator Separator between pairs | ||
| 668 | * @param array $array The array to implode | ||
| 669 | * | ||
| 670 | * @return string The imploded array | ||
| 671 | */ | ||
| 672 | public static function arrayImplode($glue, $separator, $array) | ||
| 686 | } | ||
| 687 | } | ||
| 688 |