Files
portal-api/app/Http/Services/VNPayService.php
T
2025-07-24 16:18:27 +07:00

121 lines
3.7 KiB
PHP

<?php
namespace App\Http\Services;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
class VNPayService
{
/**
* Create a payment URL
*
* @param array $data
* @return string
*/
public function createPaymentUrl(array $data)
{
$vnp_TxnRef = rand(1, 10000); // Transaction reference
$vnp_Amount = $data['amount'] ?? 0;
$vnp_Locale = $data['language'] ?? 'vn';
$vnp_BankCode = $data['bankCode'] ?? '';
$vnp_IpAddr = request()->ip();
// Lấy thông tin người dùng đang đăng nhập
$user = customer();
$userName = $user ? $user->firstname . ' ' . $user->lastname : 'Khách';
// Lấy thông tin đơn hàng nếu có
$orderId = $data['order_id'] ?? $vnp_TxnRef;
// Tạo nội dung thanh toán với thông tin người dùng và mã đơn hàng
$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" => setting('setting.vnpay_tmn_code'),
"vnp_Amount" => $vnp_Amount * 100,
"vnp_Command" => "billpayment",
"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" => $orderId,
// "vnp_ExpireDate" => $expire
];
// 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') . "?" . $query;
$vnpSecureHash = hash_hmac('sha512', $hashdata, setting('setting.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;
}
}