changes
This commit is contained in:
35
server/api/vnpay/create-payment.post.ts
Normal file
35
server/api/vnpay/create-payment.post.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { VNPay } from "vnpay";
|
||||
import { secureSecret, tmnCode, vnpayHost } from "~~/server/utils/vnpaySettings";
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
|
||||
const vnpay = new VNPay({
|
||||
// ⚡ Cấu hình bắt buộc
|
||||
tmnCode,
|
||||
secureSecret,
|
||||
vnpayHost,
|
||||
|
||||
// 🔧 Cấu hình tùy chọn
|
||||
testMode: true, // Chế độ test
|
||||
// hashAlgorithm: 'SHA512', // Thuật toán mã hóa
|
||||
enableLog: true, // Bật/tắt log
|
||||
// loggerFn: ignoreLogger, // Custom logger
|
||||
|
||||
// 🔧 Custom endpoints
|
||||
endpoints: {
|
||||
paymentEndpoint: "paymentv2/vpcpay.html",
|
||||
queryDrRefundEndpoint: "merchant_webapi/api/transaction",
|
||||
getBankListEndpoint: "qrpayauth/api/merchant/get_bank_list",
|
||||
},
|
||||
});
|
||||
|
||||
const paymentUrl = vnpay.buildPaymentUrl({
|
||||
vnp_Amount: body.amount,
|
||||
vnp_IpAddr: "192.168.1.1",
|
||||
vnp_ReturnUrl: "http://localhost:3000/vnpay-return",
|
||||
vnp_TxnRef: `Test-${body.invoiceCode}-${new Date().toISOString()}`,
|
||||
vnp_OrderInfo: `Thanh toan don hang ${body.invoiceCode}`,
|
||||
});
|
||||
|
||||
return { paymentUrl };
|
||||
});
|
||||
36
server/api/vnpay/return.get.ts
Normal file
36
server/api/vnpay/return.get.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { VNPay } from "vnpay";
|
||||
|
||||
export default defineEventHandler<{
|
||||
query: {
|
||||
vnp_Amount: string;
|
||||
vnp_BankCode: string;
|
||||
vnp_BankTranNo: string;
|
||||
vnp_CardType: string;
|
||||
vnp_OrderInfo: string;
|
||||
vnp_PayDate: string;
|
||||
vnp_ResponseCode: string;
|
||||
vnp_TmnCode: string;
|
||||
vnp_TransactionNo: string;
|
||||
vnp_TransactionStatus: string;
|
||||
vnp_TxnRef: string;
|
||||
vnp_SecureHash: string;
|
||||
};
|
||||
}>(async (event) => {
|
||||
const query = getQuery(event);
|
||||
const vnpay = new VNPay({ tmnCode, secureSecret, vnpayHost });
|
||||
|
||||
try {
|
||||
const verify = vnpay.verifyReturnUrl(query);
|
||||
/*
|
||||
!isVerified -> Xác thực tính toàn vẹn dữ liệu thất bại
|
||||
!isSuccess -> Đơn hàng thanh toán thất bại
|
||||
*/
|
||||
return {
|
||||
isSuccess: verify.isSuccess,
|
||||
isVerified: verify.isVerified,
|
||||
message: verify.message,
|
||||
};
|
||||
} catch (error) {
|
||||
return { error: "Dữ liệu không hợp lệ" };
|
||||
}
|
||||
});
|
||||
3
server/utils/vnpaySettings.ts
Normal file
3
server/utils/vnpaySettings.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const tmnCode = "SCG4UUS6";
|
||||
export const secureSecret = "G0O1GV6Z5OS0YDG38O76QSYRR81D8WWY";
|
||||
export const vnpayHost = "https://sandbox.vnpayment.vn/paymentv2/vpcpay.html";
|
||||
Reference in New Issue
Block a user