ip(); $user = Auth::guard('client')->user() ?? Auth::guard('users')->user(); $userName = $user ? $user->fullname : 'Khách'; $isDeposit = isset($data['transaction_ref']); if ($isDeposit) { $orderInfo = "Nạp tiền vào tài khoản - Khách hàng: " . $userName; } else { $orderId = $data['order_id'] ?? $vnp_TxnRef; $orderInfo = "Thông tin đơn hàng: " . $userName . " - Mã đơn hàng: " . $orderId; } // Set timezone Carbon::setLocale('vi'); $startTime = Carbon::now()->format('YmdHis'); $expire = Carbon::now()->addMinutes(15)->format('YmdHis'); $inputData = [ "vnp_Version" => "2.1.0", "vnp_TmnCode" => config('vnpay.tmn_code'), "vnp_Amount" => $vnp_Amount * 100, "vnp_Command" => "pay", "vnp_CreateDate" => $startTime, "vnp_CurrCode" => "VND", "vnp_IpAddr" => $vnp_IpAddr, "vnp_Locale" => 'vn', "vnp_OrderInfo" => $orderInfo, "vnp_OrderType" => "other", "vnp_ReturnUrl" => config('vnpay.return_url'), "vnp_TxnRef" => $vnp_TxnRef, ]; // Bỏ cmt khi nếu muốn chọn thanh toán chọn thẻ thôi // if (isset($vnp_BankCode) && $vnp_BankCode != "") { // $inputData['vnp_BankCode'] = $vnp_BankCode; // } ksort($inputData); $query = ""; $i = 0; $hashdata = ""; foreach ($inputData as $key => $value) { if ($i == 1) { $hashdata .= '&' . urlencode($key) . "=" . urlencode($value); } else { $hashdata .= urlencode($key) . "=" . urlencode($value); $i = 1; } $query .= urlencode($key) . "=" . urlencode($value) . '&'; } $vnp_Url = config('vnpay.url') . "?" . rtrim($query, '&'); $vnpSecureHash = hash_hmac('sha512', $hashdata, config('vnpay.hash_secret')); $vnp_Url .= '&vnp_SecureHash=' . $vnpSecureHash; return $vnp_Url; } /** * Verify return data from VNPAY * * @param array $data * @return array */ public function verifyPayment(array $data) { $vnp_SecureHash = $data['vnp_SecureHash'] ?? ''; $inputData = []; foreach ($data as $key => $value) { if (substr($key, 0, 4) == "vnp_") { $inputData[$key] = $value; } } unset($inputData['vnp_SecureHash']); ksort($inputData); $i = 0; $hashData = ""; foreach ($inputData as $key => $value) { if ($i == 1) { $hashData = $hashData . '&' . urlencode($key) . "=" . urlencode($value); } else { $hashData = $hashData . urlencode($key) . "=" . urlencode($value); $i = 1; } } $secureHash = hash_hmac('sha512', $hashData, config('vnpay.hash_secret')); $result = [ 'isValid' => ($secureHash == $vnp_SecureHash), 'data' => $data, 'isSuccess' => (($secureHash == $vnp_SecureHash) && ($data['vnp_ResponseCode'] == '00')) ]; return $result; } }