Files
web/server/api/vnpay/create-payment.post.ts
2026-06-23 14:06:29 +07:00

36 lines
1.1 KiB
TypeScript

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 };
});