UNPKG

nestjs-vnpay

Version:

NestJS module wrapper for VNPay payment gateway integration based on the vnpay package

133 lines (132 loc) 6.14 kB
"use strict"; 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); } }; 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 */ async getBankList() { return 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 tạo QR thanh toán VNPay (QR Pay). * @en Build VNPay payment QR (QR Pay). * * @param {BuildPaymentUrl} data - Dữ liệu thanh toán (cùng payload cơ bản với buildPaymentUrl) * @param {GenerateQrOptions} options - Tùy chọn (logger, ...) * @returns {Promise<GenerateQrResponse>} Mã phản hồi, thông báo và nội dung chuỗi QR */ async generateQr(data, options) { return this.vnpay.generateQr(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 */ async 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 */ async 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 */ async queryDr(data, options) { return 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 */ async refund(data, options) { return 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);