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

@zenetralabs/mpesa

v0.3.0

Published

A functional simple mpesa node sdk, with typescript support

Downloads

245

Readme

mpesa

A functional simple mpesa node sdk, with typescript support Star me on Github

Installation

npm

npm install @zenetralabs/mpesa

yarn

yarn add @zenetralabs/mpesa

pnpm

pnpm add @zenetralabs/mpesa

Usage

mpesa Config Setup

To use the mpesa sdk, you need to initialize it with your credentials. Consider storing all these credentials in a .env file and using dotenv to load them into your application.

import { Mpesa } from '@zenetralabs/mpesa'

export const mpesa = new Mpesa({
    env: 'sandbox', // "sandbox" or "live"
    type: 4, // 2 or 4
    shortcode: 4657849,
    store: 4657849,
    key: 'MPESA_B2C_CONSUMER_KEY',
    secret: 'MPESA_B2C_CONSUMER_SECRET',
    username: 'USER_NAME',
    password: 'PASSWORD',
    certFolderPath: 'CERT_FOLDER_ABSOLUTE_PATH',
    passkey: 'MPESA_PASSKEY'
})

// STK Push code
// B2C code
// Check Transaction Status code
// etc

STK Push

STK Push is a service that allows you to receive payments on the go from your customers. It is a simple and secure way to receive payments from customers using M-Pesa. The service is available on the Paybill and Buy Goods and Services platforms.

import { Mpesa } from '@zenetralabs/mpesa'

// mpesa Config setup here

const { error, data } = await mpesa.stkPush({
    phone: '254712345678',
    product: `Shown in stk push message`,
    amount: 400,
    CallBackURL: `https://todo.com/api/v1/pay/stk/callback`, // Must be https secure
    description: `Description of this payment`
})

B2C

B2C is a service that allows you to send money to any mpesa mobile number in Kenya.

import { Mpesa } from '@zenetralabs/mpesa'

// mpesa Config setup here

const { data, error } = await mpesa.sendB2C({
    PartyB: '254712345678',
    Amount: 5,
    CommandID: 'BusinessPayment',
    Occasion: 'Some message here',
    Remarks: `Some remarks here`,
    ResultURL: `https://todo.com/api/v1/pay/b2c/callback`,
    QueueTimeOutURL: `https://todo.com/api/v1/pay/b2c/timeout`
})

Check Transaction Status

Check the status of a transaction using the TransactionID returned from the B2C or STK Push request.

import { Mpesa } from '@zenetralabs/mpesa'

// mpesa Config setup here

const { data, error } = await mpesa.checkStatus({
    code: 'SJDK98K8H',
    QueueTimeOutURL: `https://todo.com/api/v1/pay/status/timeout`,
    ResultURL: `https://todo.com/api/v1/pay/status/callback`
})

Check mpesa business accounts Balance

Check the balance of your business account

import { Mpesa } from '@zenetralabs/mpesa'

// mpesa Config setup here

const { data, error } = await mpesa.accountBalance({
    QueueTimeOutURL: `https://todo.com/api/v1/pay/balance/timeout`,
    ResultURL: `https://todo.com/api/v1/pay/balance/callback`
})

Note (VERY IMPORTANT):

Please note that most of this code is basic. If you need something more detailed check out the zenetra labs oss docs for more details.

If you need original mpesa docs please see mpesa api documentation for more details.

License

MIT

Powered by Zenetra Labs