1.0.6 β€’ Published 7 months ago

momo-payment-lib v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

🌐 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

  1. Fork this repository.
  2. Implement your changes.
  3. 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.
1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago