|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -/** |
4 |
| - |
5 |
| - * Whatsapp: 081216089447 |
6 |
| - * Telegram: @decoderid |
7 |
| - */ |
| 3 | +require_once('Constant.php'); |
| 4 | + |
| 5 | +class Gojek { |
| 6 | + |
| 7 | + private $headers = [ |
| 8 | + 'Content-Type: application/json', |
| 9 | + 'User-Agent: '. USER_AGENT, |
| 10 | + 'X-Platform: ' . X_PLATFORM, |
| 11 | + 'X-Uniqueid: ' . X_UNIQUEID, |
| 12 | + 'X-Appversion: ' . X_APPVERSION, |
| 13 | + 'X-Appid: ' . X_APPID, |
| 14 | + 'X-User-Type: ' . X_USER_TYPE, |
| 15 | + 'X-Deviceos: ' . X_DEVICE_OS, |
| 16 | + 'X-Phonemake: ' . X_PHONEMAKE, |
| 17 | + 'X-Phonemodel: ' . X_PHONEMODEL, |
| 18 | + 'Gojek-Country-Code: ' . GOJEK_COUNTRY_CODE |
| 19 | + ]; |
| 20 | + |
| 21 | + public function __construct($token = '') { |
| 22 | + if ($token) { |
| 23 | + $this->headers = array_merge($this->headers, [ |
| 24 | + 'Authorization: Bearer ' . $token |
| 25 | + ]); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private function uuid() { |
| 30 | + return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
| 31 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
| 32 | + mt_rand(0, 0xffff), |
| 33 | + mt_rand(0, 0x0fff) | 0x4000, |
| 34 | + mt_rand(0, 0x3fff) | 0x8000, |
| 35 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + private function request($method, $url, $payload = [], $headers = []) { |
| 40 | + |
| 41 | + $method = strtoupper($method); |
| 42 | + |
| 43 | + $ch = curl_init(); |
| 44 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 45 | + curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); |
| 46 | + curl_setopt($ch, CURLOPT_TIMEOUT, 10000); |
| 47 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 48 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ? array_merge($this->headers, $headers) : $this->headers); |
| 49 | + |
| 50 | + if ($method === 'POST' || $method === 'PATCH' || $method === 'PUT') { |
| 51 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
| 52 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); |
| 53 | + } |
| 54 | + |
| 55 | + $exec = curl_exec($ch); |
| 56 | + curl_close($ch); |
| 57 | + |
| 58 | + return json_decode($exec); |
| 59 | + } |
| 60 | + |
| 61 | + public function login($phone) { |
| 62 | + return $this->request('POST', EP_LOGIN_REQUEST, [ |
| 63 | + 'client_id' => CLIENT_ID, |
| 64 | + 'client_secret' => CLIENT_SECRET, |
| 65 | + 'country_code' => COUNTRY_CODE_PREFIX, |
| 66 | + 'login_type' => '', |
| 67 | + 'magic_link_ref' => '', |
| 68 | + 'phone_number' => $phone |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + public function relogin($phone, $pin) { |
| 73 | + $challenge = $this->request('POST', EP_LOGIN_REQUEST, [ |
| 74 | + 'client_id' => CLIENT_ID, |
| 75 | + 'client_secret' => CLIENT_SECRET, |
| 76 | + 'country_code' => COUNTRY_CODE_PREFIX, |
| 77 | + 'login_type' => LOGIN_TYPE_PIN, |
| 78 | + 'phone_number' => $phone |
| 79 | + ]); |
| 80 | + |
| 81 | + if (!$challenge->success) { |
| 82 | + return 'Error Challenge'; |
| 83 | + } |
| 84 | + |
| 85 | + $challengeToken = $this->request('POST', EP_VERIFY_MFA, [ |
| 86 | + 'challenge_id' => $challenge->data->gopay_challenge_id, |
| 87 | + 'client_id' => CLIENT_ID_MFA, |
| 88 | + 'pin' => $pin |
| 89 | + ]); |
| 90 | + |
| 91 | + if (!$challengeToken->success) { |
| 92 | + return 'Error Challenge Token'; |
| 93 | + } |
| 94 | + |
| 95 | + $token = $this->request('POST', EP_VERIFY_OTP, [ |
| 96 | + 'client_id' => CLIENT_ID, |
| 97 | + 'client_secret' => CLIENT_SECRET, |
| 98 | + 'data' => [ |
| 99 | + 'gopay_challenge_id' => $challenge->data->gopay_challenge_id, |
| 100 | + 'gopay_jwt_value' => $challengeToken->data->token |
| 101 | + ], |
| 102 | + 'grant_type' => GRANT_TYPE_PIN, |
| 103 | + 'scopes' => [] |
| 104 | + ]); |
| 105 | + |
| 106 | + |
| 107 | + return $token; |
| 108 | + } |
| 109 | + |
| 110 | + public function verifyOtp($otp, $otp_token) { |
| 111 | + return $this->request('POST', EP_VERIFY_OTP, [ |
| 112 | + 'client_id' => CLIENT_ID, |
| 113 | + 'client_secret' => CLIENT_SECRET, |
| 114 | + 'data' => [ |
| 115 | + 'otp' => $otp, |
| 116 | + 'otp_token' => $otp_token |
| 117 | + ], |
| 118 | + 'grant_type' => GRANT_TYPE_OTP, |
| 119 | + 'scopes' => [] |
| 120 | + ]); |
| 121 | + } |
| 122 | + |
| 123 | + public function verifyMFA($challenge_id, $pin) { |
| 124 | + return $this->request('POST', EP_VERIFY_MFA, [ |
| 125 | + 'challenge_id' => $challenge_id, |
| 126 | + 'client_id' => CLIENT_ID_MFA, |
| 127 | + 'pin' => $pin |
| 128 | + ]); |
| 129 | + } |
| 130 | + |
| 131 | + public function verifyMFAToken($challenge_token, $token) { |
| 132 | + return $this->request('POST', EP_VERIFY_OTP, [ |
| 133 | + 'client_id' => CLIENT_ID, |
| 134 | + 'client_secret' => CLIENT_SECRET, |
| 135 | + 'data' => [ |
| 136 | + 'challenge_token' => $challenge_token, |
| 137 | + 'challenges' => [ |
| 138 | + [ |
| 139 | + 'name' => CHALLENGES_PIN_2FA, |
| 140 | + 'value' => $token |
| 141 | + ] |
| 142 | + ] |
| 143 | + ], |
| 144 | + 'grant_type' => 'challenge', |
| 145 | + 'scopes' => [] |
| 146 | + ]); |
| 147 | + } |
| 148 | + |
| 149 | + public function resendOtp($otp_token) { |
| 150 | + return $this->request('POST', EP_RESEND_OTP, [ |
| 151 | + 'channel_type' => CHANNEL_TYPE_SMS, |
| 152 | + 'otp_token' => $otp_token |
| 153 | + ]); |
| 154 | + } |
| 155 | + |
| 156 | + public function getProfile() { |
| 157 | + return $this->request('GET', EP_CUSTOMER); |
| 158 | + } |
| 159 | + |
| 160 | + public function getBalance() { |
| 161 | + return $this->request('GET', EP_PAYMENT_OPTIONS_BALANCES); |
| 162 | + } |
| 163 | + |
| 164 | + public function getTransactionList($page = 1, $limit = 10, $startDate = '', $endDate = '') { |
| 165 | + |
| 166 | + $query = http_build_query([ |
| 167 | + 'page' => $page, |
| 168 | + 'limit' => $limit, |
| 169 | + 'lower_bound' => $startDate ? $startDate . 'T00:00:00' : '', |
| 170 | + 'upper_bound' => $endDate ? $endDate . 'T00:00:00' : '', |
| 171 | + 'country_code' => COUNTRY_CODE_ID |
| 172 | + ]); |
| 173 | + |
| 174 | + return $this->request('GET', EP_USER_ORDER_HISTORY . '?' . $query); |
| 175 | + } |
| 176 | + |
| 177 | + public function getTransactionDetail($order_id) { |
| 178 | + $query = [ |
| 179 | + 'country_code' => COUNTRY_CODE_ID |
| 180 | + ]; |
| 181 | + |
| 182 | + return $this->request('GET', str_replace('{{ORDER_ID}}', $order_id, EP_USER_ORDER_HISTORY_DETAIL) . '?' . $query); |
| 183 | + } |
| 184 | + |
| 185 | + public function getBankList() { |
| 186 | + $query = http_build_query([ |
| 187 | + 'type' => 'transfer', |
| 188 | + 'show_withdrawal_block_status' => false |
| 189 | + ]); |
| 190 | + |
| 191 | + return $this->request('GET', EP_BANK_LIST . '?' . $query); |
| 192 | + } |
| 193 | + |
| 194 | + public function validateBank($bankCode, $accountNumber) { |
| 195 | + $query = http_build_query([ |
| 196 | + 'bank_code' => $bankCode, |
| 197 | + 'account_number' => $accountNumber |
| 198 | + ]); |
| 199 | + |
| 200 | + return $this->request('GET', EP_VALIDATE_BANK . '?' . $query); |
| 201 | + } |
| 202 | + |
| 203 | + public function validateP2P($phoneNumber) { |
| 204 | + |
| 205 | + $query = http_build_query([ |
| 206 | + 'phone_number' => $phoneNumber |
| 207 | + ]); |
| 208 | + |
| 209 | + return $this->request('GET', EP_VALIDATE_P2P . '?' . $query); |
| 210 | + } |
| 211 | + |
| 212 | + public function transferBank($bankCode, $accountNumber, $amount, $notes, $pin) { |
| 213 | + $validateBank = $this->validateBank($bankCode, $accountNumber); |
| 214 | + |
| 215 | + if (!$validateBank->success) { |
| 216 | + return 'Error ValidateBank'; |
| 217 | + } |
| 218 | + |
| 219 | + return $this->request('POST', EP_WITHDRAWALS, [ |
| 220 | + 'account_name' => $validateBank->data->account_name, |
| 221 | + 'account_number' => $accountNumber, |
| 222 | + 'amount' => $amount, |
| 223 | + 'bank_code' => $bankCode, |
| 224 | + 'currency' => 'IDR', |
| 225 | + 'notes' => $notes, |
| 226 | + 'pin' => $pin, |
| 227 | + 'type' => 'transfer' |
| 228 | + ], [ |
| 229 | + 'Idempotency-Key: ' . $this->uuid() |
| 230 | + ]); |
| 231 | + } |
| 232 | + |
| 233 | + public function transferP2P($phoneNumber, $amount, $pin) { |
| 234 | + $validateP2P = $this->validateP2P($phoneNumber); |
| 235 | + |
| 236 | + if (!$validateP2P->success) { |
| 237 | + return 'Error ValidateP2P'; |
| 238 | + } |
| 239 | + |
| 240 | + if ($validateP2P->data->is_blocked) { |
| 241 | + return 'Error ValidateP2P User Blocked'; |
| 242 | + } |
| 243 | + |
| 244 | + return $this->request('POST', EP_FUND_TRANSFER, [ |
| 245 | + 'amount' => [ |
| 246 | + 'currency' => 'IDR', |
| 247 | + 'value' => $amount |
| 248 | + ], |
| 249 | + 'description' => '', |
| 250 | + 'metadata' => [ |
| 251 | + 'post_visibility' => 'PRIVATE', |
| 252 | + 'theme_id' => 'THEME_CLASSIC' |
| 253 | + ], |
| 254 | + 'payee' => [ |
| 255 | + 'id' => $validateP2P->data->qr_id |
| 256 | + ] |
| 257 | + ], [ |
| 258 | + 'Pin: ' . $pin |
| 259 | + ]); |
| 260 | + } |
| 261 | + |
| 262 | + public function validateQRCode($data) { |
| 263 | + return $this->request('POST', EP_EXPLORE, [ |
| 264 | + 'data' => $data, |
| 265 | + 'type' => 'QR_CODE' |
| 266 | + ]); |
| 267 | + } |
| 268 | + |
| 269 | + public function payStaticQR($payee, $additionalData, $metaData, $orderSignature, $amount, $pin) { |
| 270 | + |
| 271 | + $inquiry = $this->request('POST', EP_PAYMENTS_V1, [ |
| 272 | + 'additional_data' => $additionalData, |
| 273 | + 'amount' => [ |
| 274 | + 'currency' => 'IDR', |
| 275 | + 'value' => $amount |
| 276 | + ], |
| 277 | + 'channel_type' => 'STATIC_QR', |
| 278 | + 'checksum' => json_decode($metaData->checksum), |
| 279 | + 'fetch_promotion_details' => false, |
| 280 | + 'metadata' => $metaData, |
| 281 | + 'order_signature' => $orderSignature, |
| 282 | + 'payee' => $payee, |
| 283 | + 'payment_intent' => $metaData->payment_widget_intent |
| 284 | + ], [ |
| 285 | + 'Idempotency-Key: ' . $this->uuid() |
| 286 | + ]); |
| 287 | + |
| 288 | + if (!$inquiry->success) { |
| 289 | + return 'Error Inquiry'; |
| 290 | + } |
| 291 | + |
| 292 | + $query = http_build_query([ |
| 293 | + 'intent' => $inquiry->data->intent, |
| 294 | + 'merchant_id' => $inquiry->data->merchant_information->merchant_id, |
| 295 | + ]); |
| 296 | + |
| 297 | + $paymentOptions = $this->request('GET', EP_PAYMENT_OPTIONS . '?' . $query); |
| 298 | + |
| 299 | + if (!$paymentOptions->success) { |
| 300 | + return 'Error Payment Options'; |
| 301 | + } |
| 302 | + |
| 303 | + $paymentOptionsToken = $paymentOptions->data->payment_options[0]->token; |
| 304 | + |
| 305 | + return $this->request('PATCH', str_replace('{{PAYMENT_ID}}', $inquiry->data->payment_id, EP_PAYMENTS_V3), [ |
| 306 | + 'additional_data' => $additionalData, |
| 307 | + 'applied_promo_code' => [ |
| 308 | + 'NO_PROMO_APPLIED' |
| 309 | + ], |
| 310 | + 'checksum' => json_decode($metaData->checksum), |
| 311 | + 'metadata' => $metaData, |
| 312 | + 'order_signature' => $orderSignature, |
| 313 | + 'payment_instructions' => [ |
| 314 | + [ |
| 315 | + 'amount' => [ |
| 316 | + 'currency' => 'IDR', |
| 317 | + 'display_value' => '', |
| 318 | + 'value' => $amount |
| 319 | + ], |
| 320 | + 'token' => $paymentOptionsToken |
| 321 | + ] |
| 322 | + ] |
| 323 | + ], [ |
| 324 | + 'Pin: ' . $pin, |
| 325 | + 'X-User-Locale: en_ID' |
| 326 | + ]); |
| 327 | + } |
| 328 | + |
| 329 | + public function payDynamicQR($paymentId, $additionalData, $metaData, $orderSignature, $amount, $pin) { |
| 330 | + |
| 331 | + $query = http_build_query([ |
| 332 | + 'fetch_promotion_details' => false |
| 333 | + ]); |
| 334 | + |
| 335 | + $inquiry = $this->request('GET', EP_PAYMENTS_V1 . '/' . $paymentId . '?' . $query); |
| 336 | + |
| 337 | + if (!$inquiry->success) { |
| 338 | + return 'Error Inquiry'; |
| 339 | + } |
| 340 | + |
| 341 | + $query = http_build_query([ |
| 342 | + 'intent' => $inquiry->data->intent, |
| 343 | + 'merchant_id' => $inquiry->data->merchant_information->merchant_id, |
| 344 | + ]); |
| 345 | + |
| 346 | + $paymentOptions = $this->request('GET', EP_PAYMENT_OPTIONS . '?' . $query); |
| 347 | + |
| 348 | + if (!$paymentOptions->success) { |
| 349 | + return 'Error Payment Options'; |
| 350 | + } |
| 351 | + |
| 352 | + $paymentOptionsToken = $paymentOptions->data->payment_options[0]->token; |
| 353 | + |
| 354 | + return $this->request('PATCH', str_replace('{{PAYMENT_ID}}', $inquiry->data->payment_id, EP_PAYMENTS_V3), [ |
| 355 | + 'additional_data' => $additionalData, |
| 356 | + 'applied_promo_code' => [ |
| 357 | + 'NO_PROMO_APPLIED' |
| 358 | + ], |
| 359 | + 'channel_type' => 'DYNAMIC_QR', |
| 360 | + 'checksum' => json_decode($metaData->checksum), |
| 361 | + 'metadata' => $metaData, |
| 362 | + 'order_signature' => $orderSignature, |
| 363 | + 'payment_instructions' => [ |
| 364 | + [ |
| 365 | + 'amount' => [ |
| 366 | + 'currency' => 'IDR', |
| 367 | + 'display_value' => '', |
| 368 | + 'value' => $inquiry->data->amount->value |
| 369 | + ], |
| 370 | + 'token' => $paymentOptionsToken |
| 371 | + ] |
| 372 | + ] |
| 373 | + ], [ |
| 374 | + 'Pin: ' . $pin, |
| 375 | + 'X-User-Locale: en_ID' |
| 376 | + ]); |
| 377 | + } |
| 378 | + |
| 379 | + public function updatePIN($oldPin, $newPin) { |
| 380 | + return $this->request('PUT', EP_PIN_UPDATE, [ |
| 381 | + 'new_pin' => $newPin |
| 382 | + ], [ |
| 383 | + 'Pin: ' . $oldPin |
| 384 | + ]); |
| 385 | + } |
| 386 | + |
| 387 | + public function logout() { |
| 388 | + return $this->request('DELETE', EP_VERIFY_OTP); |
| 389 | + } |
| 390 | +} |
0 commit comments