FusionPay is a TypeScript/JavaScript library for handling payment operations with the MoneyFusion payment gateway, providing a simple and intuitive API to facilitate online payments.
Install FusionPay using npm or yarn:
npm install fusionpay
# or
yarn add fusionpay
import { FusionPay } from "fusionpay";
// Basic initialization
const fusionPay = new FusionPay("https://your-api-url.com");
// With custom data type
interface OrderData {
orderId: string;
customerEmail: string;
}
const typedFusionPay = new FusionPay<OrderData>("https://your-api-url.com");
fusionPay
.totalPrice(200)
.addArticle("Sac", 100)
.addArticle("Veste", 100)
.addInfo({
orderId: "12345",
customerEmail: "customer@example.com",
})
.clientName("M. Yaya")
.clientNumber("01010101")
.returnUrl("https://my_callback_url.com");
try {
const response = await fusionPay.makePayment();
console.log("Payment initiated:", response);
// Redirect user to payment URL or send url to client
} catch (error) {
console.error("Payment initiation failed:", error);
}
{
statut: boolean; // Payment initiation status
token: string; // Token for payment verification
message: string; // Status message
url: string; // Payment gateway URL for user redirection
}
When the payment is completed, the user will be redirected to your return URL with a token parameter:
https://my_callback_url.com?token=payment_token_here
//extract token in your url
//eg: Nodejs -> const {token} = req.query
try {
// Verify payment status
const status = await fusionPay.checkPaymentStatus(token);
if (status.statut && status.data.statut === "paid") {
// Payment successful
const customData = status.data.personal_Info[0];
// Handle success...
}
} catch (error) {
console.error("Status check failed:", error);
}
{
statut: boolean; // Verification request status
message: string; // Status message
data: {
_id: string; // Payment record ID
tokenPay: string; // Payment token
numeroSend: string; // Customer phone number
nomclient: string; // Customer name
personal_Info: T[]; // Your custom data array
numeroTransaction: string; // Transaction reference
Montant: number; // Payment amount
frais: number; // Transaction fees
statut: "pending" | "paid" | "failed"; // Payment status
moyen: string; // Payment method used
return_url: string; // Callback URL
createdAt: string; // Transaction timestamp
}
}
Here are some examples of custom data you might want to store:
// E-commerce order
interface OrderData {
orderId: string;
customerEmail: string;
}
// Subscription
interface SubscriptionData {
planId: string;
subscriberId: string;
period: "monthly" | "yearly";
}
// Event ticket
interface TicketData {
eventId: string;
ticketType: string;
quantity: number;
}
// Usage
const payment = new FusionPay<OrderData>(apiUrl);
payment.addInfo({
orderId: "ORD-123",
customerEmail: "customer@example.com",
});
new FusionPay<T = CustomPaymentData>(apiUrl: string)
All methods (except makePayment
and checkPaymentStatus
) support method chaining.
totalPrice(amount: number): this
addArticle(name: string, value: number): this
addInfo(data: T): this
clientName(name: string): this
clientNumber(number: string): this
returnUrl(url: string): this
makePayment(): Promise<PaymentResponse>
checkPaymentStatus(token: string): Promise<PaymentVerificationResponse<T>>
The library throws errors for failed API calls and invalid parameters. Always wrap API calls in try-catch blocks for proper error handling.
This project is licensed under the MIT License - see the LICENSE file for details.