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

nestjs-mpesa

v1.0.0

Published

A module for Lipa na M-PESA (LNM) payments built for the Nestjs framework

Downloads

6

Readme

Lipa na M-PESA Payment Integration

A module for Lipa na M-PESA (LNM) payments built for the Nestjs framework.

Installation

npm install --save nestjs-mpesa

Usage

Module Initialization

You can initialize the M-PESA module either synchronously or asynchronously.

Synchronous Initialization

import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { MpesaModule } from "nestjs-mpesa";

@Module({
    imports: [
        ConfigModule.forRoot({
            isGlobal: true,
        }),
        MpesaModule.forRoot({
            consumerKey: process.env.MPESA_CONSUMER_KEY,
            consumerSecret: process.env.MPESA_CONSUMER_SECRET,
            businessShortCode: process.env.MPESA_BUSINESS_SHORTCODE,
            passkey: process.env.MPESA_PASSKEY,
            accountReference: process.env.MPESA_ACCOUNT_REFERENCE,
            transactionDesc: process.env.MPESA_TRANSACTION_DESC,
            partyA: process.env.MPESA_PARTYA,
            b2cSecurityCredential: process.env.MPESA_B2C_SECURITY_CREDENTIAL,
            initiatorName: process.env.MPESA_INITIATOR_NAME,
            environment: process.env.MPESA_ENVIRONMENT as "sandbox" | "live",
            transactionType: process.env.MPESA_TRANSACTION_TYPE as "paybill" | "till",
        }),
    ],
})
export class AppModule {}

Asynchronous Initialization

import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { MpesaModule } from "nestjs-mpesa";

@Module({
    imports: [
        ConfigModule.forRoot({
            isGlobal: true,
        }),
        MpesaModule.forRootAsync({
            inject: [ConfigService],
            useFactory: (configService: ConfigService) => ({
                consumerKey: configService.get("MPESA_CONSUMER_KEY"),
                consumerSecret: configService.get("MPESA_CONSUMER_SECRET"),
                businessShortCode: configService.get("MPESA_BUSINESS_SHORTCODE"),
                passkey: configService.get("MPESA_PASSKEY"),
                accountReference: configService.get("MPESA_ACCOUNT_REFERENCE"),
                transactionDesc: configService.get("MPESA_TRANSACTION_DESC"),
                partyA: configService.get("MPESA_PARTYA"),
                b2cSecurityCredential: configService.get("MPESA_B2C_SECURITY_CREDENTIAL"),
                initiatorName: configService.get("MPESA_INITIATOR_NAME"),
                environment: configService.get("MPESA_ENVIRONMENT") as "sandbox" | "live",
                transactionType: configService.get("MPESA_TRANSACTION_TYPE") as "paybill" | "till",
            }),
        }),
    ],
})
export class AppModule {}

Dependency Injection

To use the M-PESA client in your service, you can inject it as follows:

import { Injectable } from "@nestjs/common";
import { InjectMpesa } from "nestjs-mpesa";
import MpesaPay from "MpesaPay";

@Injectable()
export class AppService {
    public constructor(@InjectMpesa() private readonly mpesaClient: MpesaPay) {}

    // To initiate a payment using Mpesa Pay, you can call the stkPush method:
    async initiatePayment(amount: string, phoneNumber: string, callbackUrl: string) {
        try {
            const response = await this.mpesaClient.stkPush({ amount, phoneNumber, callbackUrl });
            console.log(response);
            // Handle the response data
        } catch (error) {
            console.error(error);
            // Handle errors
        }
    }
}

Examples

Acknowledgements

Contributing

Thanks to everyone who has contributed to this project so far.

License

MIT