@khoimd/nestjs-vnpay
Version:
NestJS module wrapper for VNPay payment gateway integration based on the vnpay package
137 lines (136 loc) • 6.54 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VnpayService = void 0;
const common_1 = require("@nestjs/common");
const vnpay_1 = require("vnpay");
const vnpay_constant_1 = require("./vnpay.constant");
let VnpayService = class VnpayService {
constructor(options) {
this.options = options;
this.vnpay = new vnpay_1.VNPay(this.options);
}
/**
* Sử dụng trực tiếp vnpay instance
*
* @en Get raw vnpay instance
* @returns {VNPay} The raw vnpay instance
*
* @example
* const bankList = await this.vnpayService.instance.getBankList();
* console.log(bankList);
*/
get instance() {
return this.vnpay;
}
/**
* Lấy danh sách ngân hàng
* @en Get list of banks
* @returns {Promise<Bank[]>} List of banks
*/
getBankList() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.vnpay.getBankList();
});
}
/**
* Phương thức xây dựng, tạo thành url thanh toán của VNPay
* @en Build the payment url
*
* @param {BuildPaymentUrl} data - Payload that contains the information to build the payment url
* @param {BuildPaymentUrlOptions} options - Options
* @returns {string} The payment url string
* @see https://sandbox.vnpayment.vn/apis/docs/thanh-toan-pay/pay.html#tao-url-thanh-toan
*/
buildPaymentUrl(data, options) {
return this.vnpay.buildPaymentUrl(data, options);
}
/**
* Phương thức xác thực tính đúng đắn của các tham số trả về từ VNPay
* @en Method to verify the return url from VNPay
*
* @param {ReturnQueryFromVNPay} query - The object of data return from VNPay
* @param {VerifyReturnUrlOptions} options - Options
* @returns {VerifyReturnUrl} The return object
* @see https://sandbox.vnpayment.vn/apis/docs/thanh-toan-pay/pay.html#code-returnurl
*/
verifyReturnUrl(data, options) {
return this.vnpay.verifyReturnUrl(data, options);
}
/**
* Phương thức xác thực tính đúng đắn của lời gọi ipn từ VNPay
*
* Sau khi nhận được lời gọi, hệ thống merchant cần xác thực dữ liệu nhận được từ VNPay, kiểm tra đơn hàng có hợp lệ không, kiểm tra số tiền thanh toán có đúng không.
*
* Sau đó phản hồi lại VNPay kết quả xác thực thông qua các `IpnResponse`
*
* @en Method to verify the ipn url from VNPay
*
* After receiving the call, the merchant system needs to verify the data received from VNPay, check if the order is valid, check if the payment amount is correct.
*
* Then respond to VNPay the verification result through the `IpnResponse`
*
* @param {ReturnQueryFromVNPay} query The object of data return from VNPay
* @param {VerifyIpnCallOptions} options - Options
* @returns {VerifyIpnCall} The return object
* @see https://sandbox.vnpayment.vn/apis/docs/thanh-toan-pay/pay.html#code-ipn-url
*/
verifyIpnCall(data, options) {
return this.vnpay.verifyIpnCall(data, options);
}
/**
* Đây là API để hệ thống merchant truy vấn kết quả thanh toán của giao dịch tại hệ thống VNPAY.
*
* @en This is the API for the merchant system to query the payment result of the transaction at the VNPAY system.
*
* @param {QueryDr} query - The data to query payment result
* @param {QueryDrOptions} options - Options
* @returns {Promise<QueryDrResponse>} The data return from VNPay and after verified
* @see https://sandbox.vnpayment.vn/apis/docs/truy-van-hoan-tien/querydr&refund.html#truy-van-ket-qua-thanh-toan-PAY
*/
queryDr(data, options) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.vnpay.queryDr(data, options);
});
}
/**
* Đây là API để hệ thống merchant gửi yêu cầu hoàn tiền cho giao dịch qua hệ thống Cổng thanh toán VNPAY.
*
* @en This is the API for the merchant system to refund the transaction at the VNPAY system.
* @param {Refund} data - The data to request refund
* @param {RefundOptions} options - Options
* @returns The data return from VNPay
* @see https://sandbox.vnpayment.vn/apis/docs/truy-van-hoan-tien/querydr&refund.html#hoan-tien-thanh-toan-PAY
*/
refund(data, options) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.vnpay.refund(data, options);
});
}
};
exports.VnpayService = VnpayService;
exports.VnpayService = VnpayService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(vnpay_constant_1.VNPAY_MODULE_OPTIONS)),
__metadata("design:paramtypes", [Object])
], VnpayService);