· 6 years ago · Oct 18, 2018, 02:14 AM
1<?php
2
3namespace Bca;
4
5class BcaHttp
6{
7 public static $VERSION = '2.2.0';
8 private static $timezone = 'Asia/Jakarta';
9 private static $port = 443;
10 private static $hostName = 'sandbox.bca.co.id';
11
12 protected $settings = array(
13 'corp_id' => '',
14 'client_id' => '',
15 'client_secret' => '',
16 'api_key' => '',
17 'secret_key' => '',
18 'scheme' => 'https',
19 'port' => 443,
20 'timezone' => 'Asia/Jakarta',
21 'timeout' => null,
22 'development' => true,
23 );
24
25 public function __construct($corp_id, $client_id, $client_secret, $api_key, $secret_key, $options = array())
26 {
27 if (!isset($options['port'])) {
28 $options['port'] = self::getPort();
29 }
30
31 if (!isset($options['timezone'])) {
32 $options['timezone'] = self::getTimeZone();
33 }
34
35 foreach ($options as $key => $value) {
36 if (isset($this->settings[$key])) {
37 $this->settings[$key] = $value;
38 }
39 }
40
41 if (!array_key_exists('host', $this->settings)) {
42 if (array_key_exists('host', $options)) {
43 $this->settings['host'] = $options['host'];
44 } else {
45 $this->settings['host'] = self::getHostName();
46 }
47 }
48
49 $this->settings['corp_id'] = $corp_id;
50 $this->settings['client_id'] = $client_id;
51 $this->settings['client_secret'] = $client_secret;
52 $this->settings['api_key'] = $api_key;
53 $this->settings['secret_key'] = $secret_key;
54
55 $this->settings['date_now'] = "";
56
57 $this->settings['host'] =
58 preg_replace('/http[s]?\:\/\//', '', $this->settings['host'], 1);
59 }
60
61 public function getSettings()
62 {
63 return $this->settings;
64 }
65
66 private function ddnDomain()
67 {
68 return $this->settings['scheme'] . '://' . $this->settings['host'] . ':' . $this->settings['port'] . '/';
69 }
70
71 public function httpAuth()
72 {
73 $client_id = $this->settings['client_id'];
74 $client_secret = $this->settings['client_secret'];
75
76 $headerToken = base64_encode("$client_id:$client_secret");
77
78 $headers = array('Accept' => 'application/json', 'Authorization' => "Basic $headerToken");
79
80 $request_path = "api/oauth/token";
81 $domain = $this->ddnDomain();
82 $full_url = $domain . $request_path;
83
84 \Unirest\Request::curlOpts(array(
85 CURLOPT_SSL_VERIFYHOST => 0,
86 CURLOPT_SSLVERSION => 6,
87 CURLOPT_SSL_VERIFYPEER => false,
88 CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30
89 ));
90
91 $data = array('grant_type' => 'client_credentials');
92 $body = \Unirest\Request\Body::form($data);
93 $response = \Unirest\Request::post($full_url, $headers, $body);
94
95 return $response;
96 }
97
98
99public function fundTransfersDomestic(
100 $oauth_token,
101 $transactionID,
102 $referenceID,
103 $sourceAccountNumber,
104 $beneficiaryAccountNumber,
105 $beneficiaryBankCode,
106 $beneficiaryName,
107 $amount,
108 $transferType,
109 $beneficiaryCustType,
110 $beneficiaryCustResidence,
111 $remark1,
112 $remark2
113 ) {
114 $corp_id = $this->settings['corp_id'];
115 $apikey = $this->settings['api_key'];
116 $secret = $this->settings['secret_key'];
117
118 $uriSign = "POST:/banking/corporates/transfers/domestic";
119
120 $isoTime = self::generateIsoTime();
121
122 $headers = array();
123 $headers['Accept'] = 'application/json';
124 $headers['Content-Type'] = 'application/json';
125 $headers['Authorization'] = "Bearer $oauth_token";
126 $headers['X-BCA-Key'] = $apikey;
127 $headers['X-BCA-Timestamp'] = $isoTime;
128
129 $request_path = "banking/corporates/transfers/domestic";
130 $domain = $this->ddnDomain();
131 $full_url = $domain . $request_path;
132
133 $bodyData = array();
134 $bodyData['TransactionID'] = $transactionID;
135 $bodyData['TransactionDate'] = date("Y-m-d");
136 $bodyData['ReferenceID'] = $referenceID;
137 $bodyData['SourceAccountNumber'] = $sourceAccountNumber;
138 $bodyData['BeneficiaryAccountNumber'] = $beneficiaryAccountNumber;
139 $bodyData['BeneficiaryBankCode'] = $beneficiaryBankCode;
140 $bodyData['BeneficiaryName'] = $beneficiaryName;
141 $bodyData['Amount'] = $amount;
142 $bodyData['TransferType'] = $transferType;
143 $bodyData['BeneficiaryCustType'] = $beneficiaryCustType;
144 $bodyData['BeneficiaryCustResidence'] = $beneficiaryCustResidence;
145 $bodyData['CurrencyCode'] = 'IDR';
146 $bodyData['Remark1'] = strtolower(str_replace(' ', '', $remark1));
147 $bodyData['Remark2'] = strtolower(str_replace(' ', '', $remark2));
148
149 // Harus disort agar mudah kalkulasi HMAC
150 ksort($bodyData);
151
152 // Supaya jgn strip "ReferenceID" "/" jadi "/\" karena HMAC akan menjadi tidak cocok
153 $encoderData = json_encode($bodyData, JSON_UNESCAPED_SLASHES);
154
155 //echo $encoderData;
156
157 $authSignature = self::generateSign($uriSign, $oauth_token, $secret, $isoTime, $bodyData);
158
159 $headers['X-BCA-Signature'] = $authSignature;
160 $headers['ChannelID'] = '95051';
161 $headers['CredentialID'] = 'BCAAPI';
162
163 \Unirest\Request::curlOpts(array(
164 CURLOPT_SSL_VERIFYHOST => 0,
165 CURLOPT_SSLVERSION => 6,
166 CURLOPT_SSL_VERIFYPEER => false,
167 CURLOPT_TIMEOUT => $this->settings['timeout'] !== 30 ? $this->settings['timeout'] : 30
168 ));
169 $body = \Unirest\Request\Body::form($encoderData);
170 $response = \Unirest\Request::post($full_url, $headers, $body);
171
172 return $response;
173 }
174
175 public static function generateSign($url, $auth_token, $secret_key, $isoTime, $bodyToHash)
176 {
177 $hash = null;
178 if (is_array($bodyToHash)) {
179 ksort($bodyToHash);
180 $encoderData = json_encode($bodyToHash, JSON_UNESCAPED_SLASHES);
181 $hash = hash("sha256", $encoderData);
182 } else {
183 $hash = hash("sha256", "");
184 }
185 $stringToSign = $url . ":" . $auth_token . ":" . $hash . ":" . $isoTime;
186 $auth_signature = hash_hmac('sha256', $stringToSign, $secret_key, false);
187
188 return $auth_signature;
189 }
190
191
192 public static function setTimeZone($timeZone)
193 {
194 self::$timezone = $timeZone;
195 }
196
197
198 public static function getTimeZone()
199 {
200 return self::$timezone;
201 }
202
203 public static function setHostName($hostName)
204 {
205 self::$hostName = $hostName;
206 }
207
208 public static function getHostName()
209 {
210 return self::$hostName;
211 }
212
213 public static function setPort($port)
214 {
215 self::$port = $port;
216 }
217
218 public static function getPort()
219 {
220 return self::$port;
221 }
222
223 public static function generateIsoTime()
224 {
225 date_default_timezone_set(self::getTimeZone());
226
227 /* $date = \Carbon\Carbon::now(self::getTimeZone());
228 $fmt = $date->format('Y-m-d\TH:i:s');
229 $ISO8601 = sprintf("$fmt.%s%s", substr(microtime(), 2, 3), date('P')); */
230 $ISO8601 = date("Y-m-d\TH:i:s").".000+07:00";
231
232 return $ISO8601;
233 }
234
235}