feat: implement deposit request validation and transaction API, refactor client routes, and update transaction schema.

This commit is contained in:
pbao9
2026-02-18 21:17:13 +07:00
parent 31cbdf323f
commit 9a2cab4521
12 changed files with 371 additions and 175 deletions
+21 -16
View File
@@ -15,21 +15,27 @@ class VNPayService
*/
public function createPaymentUrl(array $data)
{
$vnp_TxnRef = rand(1, 10000); // Transaction reference
$vnp_TxnRef = $data['transaction_ref'] ?? rand(1, 10000);
if (!is_numeric($vnp_TxnRef)) {
$vnp_TxnRef = preg_replace('/[^0-9]/', '', $vnp_TxnRef);
}
$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';
$user = Auth::guard('sanctum')->user() ?? Auth::guard('users')->user();
$userName = $user ? $user->fullname : '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;
$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');
@@ -38,9 +44,9 @@ class VNPayService
$inputData = [
"vnp_Version" => "2.1.0",
"vnp_TmnCode" => setting('setting.vnpay_tmn_code'),
"vnp_TmnCode" => config('vnpay.tmn_code'),
"vnp_Amount" => $vnp_Amount * 100,
"vnp_Command" => "billpayment",
"vnp_Command" => "pay",
"vnp_CreateDate" => $startTime,
"vnp_CurrCode" => "VND",
"vnp_IpAddr" => $vnp_IpAddr,
@@ -48,8 +54,7 @@ class VNPayService
"vnp_OrderInfo" => $orderInfo,
"vnp_OrderType" => "other",
"vnp_ReturnUrl" => config('vnpay.return_url'),
"vnp_TxnRef" => $orderId,
// "vnp_ExpireDate" => $expire
"vnp_TxnRef" => $vnp_TxnRef,
];
// Bỏ cmt khi nếu muốn chọn thanh toán chọn thẻ thôi
@@ -71,9 +76,9 @@ class VNPayService
$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;
$vnp_Url = config('vnpay.url') . "?" . rtrim($query, '&');
$vnpSecureHash = hash_hmac('sha512', $hashdata, config('vnpay.hash_secret'));
$vnp_Url .= '&vnp_SecureHash=' . $vnpSecureHash;
return $vnp_Url;
}