Skip to content

FusionPay is a JavaScript library for handling payment operations, providing a simple and intuitive API to facilitate online payments for moneyfusion.net

Notifications You must be signed in to change notification settings

Yaya12085/fusionpay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FusionPay

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.

Installation

Install FusionPay using npm or yarn:

npm install fusionpay
# or
yarn add 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://my_callback_url.com");

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:

https://my_callback_url.com?token=payment_token_here

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

Custom Data Examples

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

API Reference

Constructor

  • new FusionPay<T = CustomPaymentData>(apiUrl: string)

Methods

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

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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

FusionPay is a JavaScript library for handling payment operations, providing a simple and intuitive API to facilitate online payments for moneyfusion.net

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published