easypay-nestjs
Version:
easypay for iranian payment gateways
154 lines • 5.63 kB
JavaScript
;
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.NovinpalStrategy = void 0;
const axios_1 = __importStar(require("axios"));
class NovinpalStrategy {
constructor() {
this.API_URLS = {
sandbox: "https://api.novinpal.ir/invoice",
production: "https://api.novinpal.ir/invoice",
};
this.GATEWAY_URLS = {
sandbox: "https://api.novinpal.ir/invoice/start",
production: "https://api.novinpal.ir/invoice/start",
};
}
async request(data) {
const options = data.options;
const url = options.sandbox ? `${this.API_URLS.sandbox}/request` : `${this.API_URLS.production}/request`;
const gatewayUrl = options.sandbox ? this.GATEWAY_URLS.sandbox : this.GATEWAY_URLS.production;
try {
const response = await axios_1.default.post(url, {
api_key: options.api_key,
amount: options.amount,
return_url: options.callbackUrl,
order_id: options.order_id,
card_number: options.card_number,
mobile: options.mobile,
description: options.description,
});
const isSuccess = response.data.status === 1;
return {
success: isSuccess,
code: response.data.status,
message: isSuccess ? "success" : response.data.errorDescription,
data: {
refId: +response.data.refId,
url: `${gatewayUrl}/${response.data.refId}`,
},
raw: response.data,
};
}
catch (error) {
if (error instanceof axios_1.AxiosError) {
return this.handleAxiosError(error);
}
else {
return this.handleUnknownError(error);
}
}
}
async verify(data) {
const options = data.options;
const url = options.sandbox ? `${this.API_URLS.sandbox}/verify` : `${this.API_URLS.production}/verify`;
try {
const response = await axios_1.default.post(url, {
api_key: options.apiKey,
ref_id: options.refId,
});
const isSuccess = response.data.status === 1;
return {
success: isSuccess,
code: response.data.status,
message: isSuccess ? "success" : "failed",
data: {
refId: +response.data.refId,
amount: response.data.amount,
cardNumber: response.data.cardNumber,
description: response.data.description,
orderId: response.data.orderId,
paidAt: response.data.paidAt,
refNumber: response.data.refNumber,
verifiedBefore: response.data.verifiedBefore,
},
raw: response.data,
};
}
catch (error) {
if (error instanceof axios_1.AxiosError) {
return this.handleAxiosError(error);
}
else {
return this.handleUnknownError(error);
}
}
}
inquiry(options) {
throw new Error("Novinpal does not support inquiry payment.");
}
handleAxiosError(error) {
const errorData = error.response?.data;
if ("status" in errorData) {
return {
success: false,
code: +errorData.errorCode,
message: errorData.errorDescription,
data: null,
raw: errorData,
};
}
else {
return {
success: false,
code: +errorData.code,
message: errorData.message,
data: null,
raw: errorData,
};
}
}
handleUnknownError(error) {
return {
success: false,
code: 500,
message: "unknown error",
data: null,
raw: error,
};
}
}
exports.NovinpalStrategy = NovinpalStrategy;
//# sourceMappingURL=novinpal.strategy.js.map