zenopay
Version:
ZenoPay is a simple and seamless payment designed for developers looking to integrate secure and reliable payment functionality into their mobile, frontend, or backend applications. With minimal setup, you can initiate payments and track their statuses ef
126 lines (125 loc) • 6.65 kB
JavaScript
"use strict";
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const qs_1 = __importDefault(require("qs"));
const axios_1 = __importDefault(require("axios"));
const fast_web_kit_1 = require("fast-web-kit");
class ZenoPay {
/**
* Initializes a new instance of the ZenoPay class.
* @param zenoPayOptions - Configuration options including API key, secret key, and account ID.
*/
constructor(zenoPayOptions) {
/**
* Handles making POST requests to the ZenoPay API.
* @param route - API endpoint route.
* @param requestData - Data to send with the request.
* @returns A promise resolving to the API response.
*/
this.postRequest = (route, requestData) => __awaiter(this, void 0, void 0, function* () {
try {
// Convert request data to query string format.
requestData = qs_1.default.stringify(requestData);
const response = yield axios_1.default.post(`${this.baseURL}/${route}`, requestData, this.headers);
const { status } = response.data;
// Return success or failure based on the API response status.
if (status === "success")
return { success: true, message: Object.assign({}, response.data) };
return { success: false, message: response.data.message };
}
catch (error) {
// Handle errors and return a failure response with the error message.
return { success: false, message: error.message };
}
});
/**
* Initiates a payment request.
* @param paymentOptions - Details of the payment including customer info and amount.
* @returns A promise resolving to the payment API response.
*/
this.Pay = (paymentOptions) => __awaiter(this, void 0, void 0, function* () {
try {
// Validate required payment details.
if (fast_web_kit_1.string.isEmpty(paymentOptions.customerName))
return { success: false, message: "Customer name is required" };
if (fast_web_kit_1.string.isEmpty(paymentOptions.customerEmail))
return { success: false, message: "Customer email is required" };
if (!fast_web_kit_1.email.isValid(paymentOptions.customerEmail))
return { success: false, message: "Invalid customer email" };
if (fast_web_kit_1.string.isEmpty(paymentOptions.customerPhoneNumber))
return { success: false, message: "Customer phone number is required" };
const phoneNumberLength = fast_web_kit_1.string.getLength(paymentOptions.customerPhoneNumber);
if (phoneNumberLength !== 10 && phoneNumberLength !== 12)
return { success: false, message: "Customer phone number must have 10 or 12 characters" };
if (!fast_web_kit_1.number.isValid(paymentOptions.amountToCharge))
return { success: false, message: "Invalid amount" };
if (paymentOptions.amountToCharge <= 0)
return { success: false, message: "Amount cannot be less than or equal to 0" };
if (fast_web_kit_1.string.isEmpty(paymentOptions.callbackURL))
return { success: false, message: "callback URL must be provided" };
// Construct the request payload for the API.
const requestData = {
create_order: 1,
api_key: this.apiKey,
account_id: this.accountID,
secret_key: this.secretKey,
amount: paymentOptions.amountToCharge,
buyer_name: paymentOptions.customerName,
webhook_url: paymentOptions.callbackURL,
buyer_email: paymentOptions.customerEmail,
buyer_phone: paymentOptions.customerPhoneNumber,
};
// Make the API call and return the response.
return this.postRequest("", requestData);
}
catch (error) {
// Handle errors and return a failure response.
return { success: false, message: error.message };
}
});
/**
* Checks the status of a payment using its order ID.
* @param orderID - The unique identifier of the order.
* @returns A promise resolving to the payment status API response.
*/
this.CheckPaymentStatus = (orderID) => __awaiter(this, void 0, void 0, function* () {
try {
// Validate the order ID.
if (fast_web_kit_1.string.isEmpty(orderID))
return { success: false, message: "Order ID is required" };
// Construct the request payload for the API.
const requestData = {
check_status: 1,
order_id: orderID,
};
// Make the API call and return the response.
return this.postRequest("order-status", requestData);
}
catch (error) {
// Handle errors and return a failure response.
return { success: false, message: error.message };
}
});
this.apiKey = zenoPayOptions.apiKey;
this.secretKey = zenoPayOptions.secretKey;
this.accountID = zenoPayOptions.accountID;
this.baseURL = "https://api.zeno.africa";
this.headers = {
headers: {
"Content-Type": "application/x-www-form-urlencoded" // API requires form-encoded data.
}
};
}
}
exports.default = ZenoPay;