npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dadson-momo-stk

v1.0.2

Published

Collection STK for MTN momo Ghana

Downloads

5

Readme

dadson-momo-stk

A simple Mobile money toolkit with reusable middleware and utilities for Express applications.

Table of Contents

Installation

To install this toolkit, you can use npm:

npm install @papakowdadson/dadson-momo-stk

USAGE

Here’s an example of how to use the toolkit in an Express application-MVC:

  • Create a .env file with the PORT value //Sample .env
PORT=5000
MTN_BASE_URL_SANDBOX=https://sandbox.momodeveloper.mtn.com // Textbed URL
MTN_BASIC_AUTH=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
MTN_BASE_URL=https://proxy.momoapi.mtn.com // Production url
MTN_OCP_COLLECTION_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  • Create index.js file
//Sample index.js
const express = require("express");
const cors = require("cors");
const app = express();
const { urlencoded } = require("express");
const dotenv = require("dotenv");
dotenv.config();

const port = process.env.PORT;

const paymentRoute = require("./routes/paymentroute");

app.use(cors());
app.options("*", cors());
app.use(express.json());
app.use(urlencoded({ extended: true }));

app.use("/@your url/payment", paymentRoute);//sample route, change to any route of your choice

app.listen(port, () => {
  console.log(`server is running on ${port}`);
});
  • Create Route file
  • Create Controller file

Middleware

createAccessToken Generates access token for each request from MTN_BASIC_AUTH. Call this method to generate access token before making any request.It accepts a callback function.

Usage Example

const mtnMomo = require('papakowdadson/dadson-momo-stk');
const _Momo = mtnMomo(process.env.MTN_BASIC_AUTH,process.env.MTN_OCP_COLLECTION_KEY);
const { createAccessToken } = _Momo;

const GenerateAccessToken = (req, res, next) => {
  console.log("====creating access token====");
  createAccessToken((error, body) => {
    if (body) {
      const _body = JSON.parse(body);
      if (_body.access_token) {
        req.body.access_token = _body.access_token;
        next();
      } else {
        res.status(400).json({ error: "No token in body" });
      }
    } else {
      res.status(500).json({ error: "couldn't create access token" });
    }
  });
};
module.exports = { GenerateAccessToken };

Middleware initializePayment Initiates a request to pay to clients MSISDN.It accepts a form and a callback

Usage

const mtnMomo = = require('papakowdadson/dadson-momo-stk');
const _Momo = mtnMomo(process.env.MTN_BASIC_AUTH,process.env.MTN_OCP_COLLECTION_KEY);
const {initializePayment}=_Momo;

Example

const MakePayment = (req, res) => {
  const form = {
    amount: req.body.amount,
    currency: "GHS",
    externalId: req.body.XReferenceId,
    payer: {
      partyIdType: "MSISDN",
      partyId: req.body.payer.partyId,
    },
    payerMessage:req.body.payerMessage,
    payeeNote: req.body.payeeNote,
    access_token: req.body.access_token,
  }; 

  initializePayment(form, (error, body) => {
    if (error) {
      res.status(500).json({ error: error });
    } else {
      res.status(200).json(body);
    }
  });
};

Middleware verifyPayment Checks the status of transaction. It accepts form an a callback

Usage

const mtnMomo = = require('papakowdadson/dadson-momo-stk');
const _Momo = mtnMomo(process.env.MTN_BASIC_AUTH,process.env.MTN_OCP_COLLECTION_KEY);
const {verifyPayment}=_Momo;

Example

const VerifyPayment = (req, res) => {
  const form = {
    ref: req.body.externalId,
    access_token: req.body.access_token,
  };
  verifyPayment(form, (error, body) => {
    if (error) {
    //handle errors appropriately
       res.status(500).json(error);
    } else {
      const _body = JSON.parse(body);
      if (_body.status == "SUCCESSFUL") {
        const data = {
          amount: _body.amount,
          MSISDN: _body.payer.partyId,
          message: _body.payeeNote,
          paymentRef: _body.externalId,
          paymentStatus: _body.status,
        };
        res.status(200).json(data);
      } else {
        // Req to Pay failed
        res.status(400).json(_body);
      }
      // response = JSON.parse(body);
    }
  });
};

Utilities

// No utilities used yet

Routes

Predefined routes, to be documented here.

Example Route

Usage

License

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

Contributing

If you would like to contribute to this project, please fork the repository and submit a pull request. We appreciate your contributions!

Issues

If you encounter any issues or have suggestions for improvements, please open an issue in the GitHub Repository