@php class BniCheck { const TIME_DIFF_LIMIT = 480; public static function encrypt(array $json_data, $cid, $secret) { return self::doubleEncrypt(strrev(time()) . '.' . json_encode($json_data), $cid, $secret); } public static function decrypt($hased_string, $cid, $secret) { $parsed_string = self::doubleDecrypt($hased_string, $cid, $secret); [$timestamp, $data] = array_pad(explode('.', $parsed_string, 2), 2, null); if (self::tsDiff(strrev($timestamp)) === true) { return json_decode($data, true); } return null; } private static function tsDiff($ts) { return abs($ts - time()) <= self::TIME_DIFF_LIMIT; } private static function doubleEncrypt($string, $cid, $secret) { $result = ''; $result = self::enc($string, $cid); $result = self::enc($result, $secret); return strtr(rtrim(base64_encode($result), '='), '+/', '-_'); } private static function enc($string, $key) { $result = ''; $strls = strlen($string); $strlk = strlen($key); for ($i = 0; $i < $strls; $i++) { $char = substr($string, $i, 1); $keychar = substr($key, ($i % $strlk) - 1, 1); $char = chr((ord($char) + ord($keychar)) % 128); $result .= $char; } return $result; } private static function doubleDecrypt($string, $cid, $secret) { $result = base64_decode( strtr(str_pad($string, ceil(strlen($string) / 4) * 4, '=', STR_PAD_RIGHT), '-_', '+/'), ); $result = self::dec($result, $cid); $result = self::dec($result, $secret); return $result; } private static function dec($string, $key) { $result = ''; $strls = strlen($string); $strlk = strlen($key); for ($i = 0; $i < $strls; $i++) { $char = substr($string, $i, 1); $keychar = substr($key, ($i % $strlk) - 1, 1); $char = chr((ord($char) - ord($keychar) + 256) % 128); $result .= $char; } return $result; } } @endphp @php // $client_id = env('BNI_CLIENT_ID_PUSAT'); // $secret_key = env('BNI_SECRET_KEY_PUSAT'); $client_id = env('BNI_CLIENT_ID_RESELLER'); $secret_key = env('BNI_SECRET_KEY_RESELLER'); $data_aktivasi_va = [ 'customer_name' => 'Eko Hendro W', 'datetime_expired' => date('c', time() + 720 * 3600), 'trx_id' => '10812', 'virtual_account' => 9883111300011019, 'type' => 'createbilling', 'billing_type' => 'o', 'client_id' => $client_id, ]; $hashed_string = BniCheck::encrypt($data_aktivasi_va, $client_id, $secret_key); $data = [ 'client_id' => $client_id, 'data' => $hashed_string, ]; $response = bni_get_content(env('BNI_API_URL'), json_encode($data)); $response_json = json_decode($response, true); if ($response_json['status'] !== '000') { } else { $data_response = BniCheck::decrypt($response_json['data'], $client_id, $secret_key); dd($data_response); } @endphp