nestjs-khalti
Version:
This is simple wrapper for Khalti Payment extended from @dallotech/nestjs-khalti. It supports ePayment Gateway(NEW) and transaction verification, but later more will be added. Just ping us or open pull request and contribute :)
109 lines • 5.7 kB
JavaScript
"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.KhaltiService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("@nestjs/axios");
const rxjs_1 = require("rxjs");
const khalti_interface_1 = require("./khalti.interface");
let KhaltiService = class KhaltiService {
constructor(options, httpService) {
this.options = options;
this.httpService = httpService;
this.paymentMode = null;
this.secretKey = null;
this.initiateUrl = null;
this.initiateUrlForTest = null;
this.lookupUrl = null;
this.lookupUrlForTest = null;
this.verifyUrl = null;
if (!options.secretKey) {
throw new common_1.InternalServerErrorException("Secret Key for khalti payment is missing");
}
this.paymentMode = options.paymentMode || khalti_interface_1.PaymentMode.TEST;
this.secretKey = options.secretKey;
this.initiateUrl = options.initiateUrl || khalti_interface_1.KHALTI_PAYMENT_URL;
this.initiateUrlForTest = options.initiateUrlForTest || khalti_interface_1.KHALTI_PAYMENT_TEST_URL;
this.lookupUrl = options.lookupUrl || khalti_interface_1.KHALTI_LOOKUP_URL;
this.lookupUrlForTest = options.lookupUrl || khalti_interface_1.KHALTI_LOOKUP_TEST_URL;
this.verifyUrl = khalti_interface_1.KHALTI_VERIFY_URL;
}
async init(khaltiRequestDto) {
const { returnUrl, websiteUrl, amount, purchaseOrderId, purchaseOrderName, customerInfo, amountBreakdown, productDetails } = khaltiRequestDto;
if (!returnUrl || !websiteUrl || !amount || !purchaseOrderId || !purchaseOrderName) {
throw new common_1.BadRequestException("Data missing for initiating Khalti payment");
}
const khaltiDto = {
return_url: returnUrl,
website_url: websiteUrl,
amount: amount,
purchase_order_id: purchaseOrderId,
purchase_order_name: purchaseOrderName,
customer_info: customerInfo,
amount_breakdown: amountBreakdown,
product_details: productDetails
};
const response = await (0, rxjs_1.firstValueFrom)(this.httpService
.post(this.paymentMode.localeCompare(khalti_interface_1.PaymentMode.TEST) == 0 ? this.initiateUrlForTest : this.initiateUrl, khaltiDto, {
headers: {
Authorization: this.secretKey,
},
}));
if (response && (response === null || response === void 0 ? void 0 : response.status) == 200 && (response === null || response === void 0 ? void 0 : response.data)) {
const { pidx, payment_url, expires_in, expires_at } = response === null || response === void 0 ? void 0 : response.data;
return {
pidx: pidx,
paymentUrl: payment_url,
expiresIn: expires_in,
expiresAt: expires_at,
};
}
}
async lookUp(data) {
try {
const { pidx } = data;
if (!pidx) {
throw new common_1.BadRequestException("pidx missing for validating khalti payment");
}
const response = await (0, rxjs_1.firstValueFrom)(this.httpService
.post(this.paymentMode.localeCompare(khalti_interface_1.PaymentMode.TEST) == 0 ? this.lookupUrlForTest : this.lookupUrl, { pidx }, {
headers: {
Authorization: this.secretKey,
},
}));
if (response && (response === null || response === void 0 ? void 0 : response.status) == 200 && (response === null || response === void 0 ? void 0 : response.data)) {
const { pidx, total_amount, status, transaction_id, fee, refunded } = response === null || response === void 0 ? void 0 : response.data;
return {
pidx,
totalAmount: total_amount,
status,
transactionId: transaction_id,
fee,
refunded
};
}
throw new common_1.InternalServerErrorException('Unexpected response from Khalti lookup endpoint');
}
catch (error) {
throw new common_1.InternalServerErrorException(`Error in payment verification \n ${error === null || error === void 0 ? void 0 : error.message}`);
}
}
};
exports.KhaltiService = KhaltiService;
exports.KhaltiService = KhaltiService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(khalti_interface_1.KHALTI_CONFIG_OPTIONS)),
__metadata("design:paramtypes", [Object, axios_1.HttpService])
], KhaltiService);
//# sourceMappingURL=khalti.service.js.map