AnhLedger's picture
add log ipn
2865a3f
raw
history blame
6.32 kB
// payment.service.ts
import { HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as querystring from 'qs';
import * as crypto from 'crypto';
@Injectable()
export class PaymentService {
constructor(private readonly configService: ConfigService) {}
async createPaymentUrl(amount: number, bankCode: string, orderDescription: string, orderType: string, language: string, ipAddr: string) {
const tmnCode = this.configService.get<string>('vnp_TmnCode');
const secretKey = this.configService.get<string>('vnp_HashSecret');
const vnpUrl = this.configService.get<string>('vnp_Url');
const returnUrl = this.configService.get<string>('vnp_ReturnUrl');
const date = new Date();
const createDate = this.formatDate(date, 'yyyymmddHHmmss');
const orderId = Date.now().toString();
const locale = language || 'vn';
const currCode = 'VND';
const vnp_Params: Record<string, string> = {
vnp_Version: '2.1.0',
vnp_Command: 'pay',
vnp_TmnCode: tmnCode,
vnp_Locale: locale,
vnp_CurrCode: currCode,
vnp_TxnRef: orderId,
vnp_OrderInfo: orderDescription,
vnp_OrderType: orderType,
vnp_Amount: (amount * 100).toString(),
vnp_ReturnUrl: returnUrl,
vnp_IpAddr: ipAddr,
vnp_CreateDate: createDate,
};
console.log("3")
if (bankCode) {
vnp_Params['vnp_BankCode'] = bankCode;
}
const sortedParams = this.sortObject(vnp_Params);
// Sign the data
const signData = querystring.stringify(sortedParams, { encode: false });
const hmac = crypto.createHmac('sha512', secretKey);
const signed = hmac.update(Buffer.from(signData, 'utf-8')).digest('hex');
sortedParams['vnp_SecureHash'] = signed;
// Create the URL
const res = `${vnpUrl}?${querystring.stringify(sortedParams, { encode: false })}`;
return res;
}
vnpayIpn(reqQuery){
console.log("helloooo")
let vnp_Params = reqQuery;
let secureHash = vnp_Params['vnp_SecureHash'];
let orderId = vnp_Params['vnp_TxnRef'];
let rspCode = vnp_Params['vnp_ResponseCode'];
delete vnp_Params['vnp_SecureHash'];
delete vnp_Params['vnp_SecureHashType'];
vnp_Params = this.sortObject(vnp_Params);
let secretKey = this.configService.get('vnp_HashSecret');
let signData = querystring.stringify(vnp_Params, { encode: false });
let hmac = crypto.createHmac("sha512", secretKey);
let signed = hmac.update(Buffer.from(signData, 'utf-8')).digest("hex");
let paymentStatus = '0'; // Giả sử '0' là trạng thái khởi tạo giao dịch, chưa có IPN. Trạng thái này được lưu khi yêu cầu thanh toán chuyển hướng sang Cổng thanh toán VNPAY tại đầu khởi tạo đơn hàng.
//let paymentStatus = '1'; // Giả sử '1' là trạng thái thành công bạn cập nhật sau IPN được gọi và trả kết quả về nó
//let paymentStatus = '2'; // Giả sử '2' là trạng thái thất bại bạn cập nhật sau IPN được gọi và trả kết quả về nó
let checkOrderId = true; // Mã đơn hàng "giá trị của vnp_TxnRef" VNPAY phản hồi tồn tại trong CSDL của bạn
let checkAmount = true; // Kiểm tra số tiền "giá trị của vnp_Amout/100" trùng khớp với số tiền của đơn hàng trong CSDL của bạn
if(secureHash === signed){ //kiểm tra checksum
if(checkOrderId){
if(checkAmount){
if(paymentStatus=="0"){ //kiểm tra tình trạng giao dịch trước khi cập nhật tình trạng thanh toán
if(rspCode=="00"){
//thanh cong
//paymentStatus = '1'
// Ở đây cập nhật trạng thái giao dịch thanh toán thành công vào CSDL của bạn
return {
statusCode: HttpStatus.OK,
message: 'Thành công!',
data: { /* dữ liệu trả về nếu có */ },
};
}
else {
//that bai
//paymentStatus = '2'
// Ở đây cập nhật trạng thái giao dịch thanh toán thất bại vào CSDL của bạn
return {
statusCode: HttpStatus.OK,
message: 'Thất bại',
data: { /* dữ liệu trả về nếu có */ },
};
}
}
else{
return {
statusCode: HttpStatus.OK,
message: 'This order has been updated to the payment status',
data: { /* dữ liệu trả về nếu có */ },
};
}
}
else{
return {
statusCode: HttpStatus.OK,
message: 'Amount invalid',
data: { /* dữ liệu trả về nếu có */ },
};
}
}
else {
return {
statusCode: HttpStatus.OK,
message: 'Order not found',
data: { /* dữ liệu trả về nếu có */ },
};
}
}
else {
return {
statusCode: HttpStatus.OK,
message: 'Checksum failed!',
data: { /* dữ liệu trả về nếu có */ },
};
}
}
// Format date helper function
formatDate(date: Date, format: string): string {
const yyyymmdd = date.toISOString().slice(0, 10).replace(/-/g, ''); // YYYYMMDD
const hhmmss = date.toTimeString().slice(0, 8).replace(/:/g, ''); // HHMMSS
return format === 'yyyymmddHHmmss' ? yyyymmdd + hhmmss : hhmmss;
}
sortObject(obj) {
let sorted = {};
let str = [];
let key;
for (key in obj){
if (obj.hasOwnProperty(key)) {
str.push(encodeURIComponent(key));
}
}
str.sort();
for (key = 0; key < str.length; key++) {
sorted[str[key]] = encodeURIComponent(obj[str[key]]).replace(/%20/g, "+");
}
return sorted;
}
}