Node.js

The Node.js package

Installation

Install FusionPay using npm or yarn:

npm install fusionpay

Usage

Initializing 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");

Setting Payment Data

fusionPay
  .totalPrice(200)
  .addArticle("Sac", 100)
  .addArticle("Veste", 100)
  .addInfo({
    orderId: "12345",
    customerEmail: "customer@example.com",
  })
  .clientName("M. Yaya")
  .clientNumber("01010101")
  .returnUrl("https://yourlink.com")
  .webhookUrl("https://yourlink.com/webhook");

Making a Payment

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);
}

Payment Response Structure

{
  statut: boolean; // Payment initiation status
  token: string; // Token for payment verification
  message: string; // Status message
  url: string; // Payment gateway URL for user redirection
}

Handling Payment Callback

When the payment is completed, the user will be redirected to your return URL with a token parameter:

Checking Payment Status

//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);
}

Payment Verification Response Structure

{
  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
  }
}

Error Handling

The library throws errors for failed API calls and invalid parameters. Always wrap API calls in try-catch blocks for proper error handling.