New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

momo-payment-lib

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

momo-payment-lib

A simple TypeScript library to integrate the MTN MoMo API for payment requests and transaction status retrieval.

latest
Source
npmnpm
Version
1.0.6
Version published
Weekly downloads
1
Maintainers
0
Weekly downloads
 
Created
Source

🌐 MoMo Payments Library

A TypeScript library for integrating MTN MoMo API to manage various payment-related operations such as Request to Pay, Remittances, Transfers, and more. This library provides a simple interface to interact with the MoMo API, supporting both sandbox and production environments.

📦 Installation

Install the package via npm:

npm install momo-payment-lib

🌟 Features

  • 📥 Request to Pay: Collect payments from customers.
  • 🔍 Check Transaction Status: Retrieve the status of payment transactions.
  • 💳 Remittances: Send money to other users.
  • 🔄 Account Balances: Fetch balance details for your accounts.
  • 🌐 Environment Support: Easily switch between sandbox and production.

🛠️ Setup

1️⃣ Environment Variables

Add the following to your .env file:

COLLECTION_API_USER_ID=your_collection_api_user_id
COLLECTION_API_KEY=your_collection_api_key
COLLECTION_PRIMARY_KEY=your_collection_primary_key

REMITTANCE_API_USER_ID=your_remittance_api_user_id
REMITTANCE_API_KEY=your_remittance_api_key
REMITTANCE_PRIMARY_KEY=your_remittance_primary_key

Replace placeholders with your MTN MoMo API credentials for each service.

🚀 Usage

Import and Initialize

Create an instance of MoMoClient with separate configurations for each product:

import { MoMoClient } from 'momo-payment-lib';

const momoClient = new MoMoClient({
  collection: {
    apiUserId: process.env.COLLECTION_API_USER_ID || '',
    apiKey: process.env.COLLECTION_API_KEY || '',
    primaryKey: process.env.COLLECTION_PRIMARY_KEY || '',
  },
  remittance: {
    apiUserId: process.env.REMITTANCE_API_USER_ID || '',
    apiKey: process.env.REMITTANCE_API_KEY || '',
    primaryKey: process.env.REMITTANCE_PRIMARY_KEY || '',
  },
  environment: 'sandbox', // Use 'sandbox' for testing or 'production' for live
});

Request to Pay

To request a payment:

const requestPayment = async () => {
  try {
    const response = await momoClient.requestPayment({
      amount: 100,
      currency: 'EUR',
      phoneNumber: '256700123456',
      reference: 'Invoice123',
    });
    console.log('Payment requested:', response);
  } catch (error) {
    console.error('Error requesting payment:', error.message);
  }
};

requestPayment();

📥 Request Parameters

ParameterTypeDescription
amountnumberAmount to charge.
currencystringCurrency code (e.g., "UGX", "EUR").
phoneNumberstringPhone number (MSISDN format).
referencestringYour reference number for tracking.

Response

{
  "referenceId": "unique-identifier"
}

Check Payment Status

To check the status of a payment:

const checkPaymentStatus = async () => {
  try {
    const status = await momoClient.getPaymentStatus('reference-id');
    console.log('Payment status:', status);
  } catch (error) {
    console.error('Error fetching status:', error.message);
  }
};

checkPaymentStatus();

📊 Response

{
  "amount": "100.00",
  "currency": "EUR",
  "status": "SUCCESSFUL",
  "payer": {
    "partyIdType": "MSISDN",
    "partyId": "256700123456"
  },
  "reason": null
}

Remittances

To send money via the remittance API:

const sendMoney = async () => {
  try {
    const response = await momoClient.sendRemittance({
      amount: 50,
      currency: 'USD',
      phoneNumber: '256700123456',
      externalId: 'Remittance123',
    });
    console.log('Remittance sent:', response);
  } catch (error) {
    console.error('Error sending remittance:', error.message);
  }
};

sendMoney();

🔗 Full Example

Here’s an end-to-end integration example:

import { MoMoClient } from 'momo-payments-lib';
import dotenv from 'dotenv';

dotenv.config();

const momoClient = new MoMoClient({
  collection: {
    apiUserId: process.env.COLLECTION_API_USER_ID || '',
    apiKey: process.env.COLLECTION_API_KEY || '',
    primaryKey: process.env.COLLECTION_PRIMARY_KEY || '',
  },
  remittance: {
    apiUserId: process.env.REMITTANCE_API_USER_ID || '',
    apiKey: process.env.REMITTANCE_API_KEY || '',
    primaryKey: process.env.REMITTANCE_PRIMARY_KEY || '',
  },
  environment: 'sandbox',
});

const processTransaction = async () => {
  try {
    const paymentResponse = await momoClient.requestPayment({
      amount: 100,
      currency: 'UGX',
      phoneNumber: '256700123456',
      reference: 'Invoice001',
    });
    console.log('Payment Requested:', paymentResponse);

    const paymentStatus = await momoClient.getPaymentStatus(paymentResponse.referenceId);
    console.log('Payment Status:', paymentStatus);
  } catch (error) {
    console.error('Error:', error.message);
  }
};

processTransaction();

🤝 Contributing

  • Fork this repository.
  • Implement your changes.
  • Open a pull request with a detailed explanation of your feature or fix.

📜 License

This library is released under the MIT License.

📝 Notes

  • Ensure you have registered and obtained credentials for each MTN MoMo product from the MTN Developer Portal.
  • Replace placeholders with actual MSISDN numbers in production environments.

Keywords

npm

FAQs

Package last updated on 17 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts