@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_RESELLER'); $secret_key = env('BNI_SECRET_KEY_RESELLER'); $data = [ 'trx_id' => 10812, 'type' => 'inquirybilling', 'virtual_account' => 9883111300011019, 'client_id' => $client_id, ]; $hashed_string = BniCheck::encrypt($data, $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') { //dd($response_json); $trx_id = 'Not Found'; $client_id = 'Not Found'; $virtual_account = 'Not Found'; $trx_amount = 'Not Found'; $customer_name = 'Not Found'; $datetime_created = 'Not Found'; $datetime_expired = 'Not Found'; $datetime_payment = 'Not Found'; $datetime_last_updated = 'Not Found'; $payment_ntb = 'Not Found'; $payment_amount = 'Not Found'; $va_status = 'Not Found'; $billing_type = 'Not Found'; } else { $data_response = BniCheck::decrypt($response_json['data'], $client_id, $secret_key); dd($data_response); $trx_id = $data_response['trx_id']; $client_id = $data_response['client_id']; $virtual_account = $data_response['virtual_account']; $trx_amount = $data_response['trx_amount']; $customer_name = $data_response['customer_name']; $datetime_created = $data_response['datetime_created']; $datetime_expired = $data_response['datetime_expired']; $datetime_payment = $data_response['datetime_payment']; $datetime_last_updated = $data_response['datetime_last_updated']; $payment_ntb = $data_response['payment_ntb']; $payment_amount = $data_response['payment_amount']; $va_status = $data_response['va_status']; $billing_type = $data_response['billing_type']; } @endphp {{ $datetime_expired }}