UNPKG

easypay-nestjs

Version:
200 lines 7.52 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ZarinpalStrategy = void 0; const axios_1 = __importStar(require("axios")); class ZarinpalStrategy { constructor() { this.API_URLS = { sandbox: "https://sandbox.zarinpal.com/pg/v4/payment", production: "https://api.zarinpal.com/pg/v4/payment", }; this.GATEWAY_URLS = { sandbox: "https://sandbox.zarinpal.com/pg/StartPay", production: "https://payment.zarinpal.com/pg/StartPay", }; } async request(data) { const options = data.options; try { const url = options.sandbox ? `${this.API_URLS.sandbox}/request.json` : `${this.API_URLS.production}/request.json`; const gatewayUrl = options.sandbox ? this.GATEWAY_URLS.sandbox : this.GATEWAY_URLS.production; const request = await axios_1.default.post(url, { merchant_id: options.merchantId, amount: options.amount, callback_url: options.callbackUrl, description: options.description, metadata: options.metadata, }); return { success: true, code: request.data.data.code, message: request.data.data.message, raw: request.data, data: { authority: request.data.data.authority, fee: request.data.data.fee, fee_type: request.data.data.fee_type, url: `${gatewayUrl}/${request.data.data.authority}`, }, }; } catch (error) { if (error instanceof axios_1.AxiosError) { return this.handleError(error, "request"); } else { return { success: false, code: 500, message: "خطایی نامشخص رخ داده است", data: null, raw: JSON.stringify(error.response), }; } } } async verify(data) { const options = data.options; try { const url = options.sandbox ? `${this.API_URLS.sandbox}/verify.json` : `${this.API_URLS.production}/verify.json`; const request = await axios_1.default.post(url, { merchant_id: options.merchantId, authority: options.authority, amount: options.amount, }); return { success: true, code: request.data.data.code, message: request.data.data.message, raw: request.data, data: { fee: request.data.data.fee, fee_type: request.data.data.fee_type, card_hash: request.data.data.card_hash, card_pan: request.data.data.card_pan, ref_id: request.data.data.ref_id, }, }; } catch (error) { if (error instanceof axios_1.AxiosError) { return this.handleError(error, "verify"); } else { return { success: false, code: 500, message: "خطایی نامشخص رخ داده است", data: null, raw: JSON.stringify(error.response), }; } } } async inquiry(data) { const options = data.options; try { const url = options.sandbox ? `${this.API_URLS.sandbox}/inquiry.json` : `${this.API_URLS.production}/inquiry.json`; const request = await axios_1.default.post(url, { merchant_id: options.merchantId, authority: options.authority, }); return { success: true, code: request.data.data.code, message: request.data.data.message, raw: request.data, data: { status: request.data.data.status, }, }; } catch (error) { if (error instanceof axios_1.AxiosError) { return this.handleError(error, "inquiry"); } else { return { success: false, code: 500, message: "خطایی نامشخص رخ داده است", data: null, raw: JSON.stringify(error.response), }; } } } handleError(error, type) { switch (type) { case "request": const requestResponse = error?.response?.data; return { success: false, data: null, code: requestResponse?.errors?.code, message: requestResponse?.errors?.message, raw: requestResponse, }; case "verify": const verifyResponse = error?.response?.data; return { success: false, data: null, code: verifyResponse?.errors?.code, message: verifyResponse?.errors?.message, raw: verifyResponse, }; case "inquiry": const inquiryResponse = error?.response?.data; let code; if (inquiryResponse.errors.authority) { code = -54; } else { code = -10; } return { success: false, data: null, code: code, message: inquiryResponse?.message, raw: inquiryResponse, }; } } } exports.ZarinpalStrategy = ZarinpalStrategy; //# sourceMappingURL=zarinpal.strategy.js.map