paympesa

2.0.3 • Public • Published

PayMpesa Logo

Introduction

payMpesa is a Node.js library that simplifies the integration of Safaricom's M-Pesa API for seamless mobile payments in Kenya. It provides easy-to-use functions for STK Push transactions, payment confirmation, QR code generation, and more.

Features

  • Initiate STK Push transactions
  • Confirm payment status
  • Handle STK Push callbacks
  • Generate QR codes for M-Pesa payments
  • Easy configuration and extensibility

Installation

To install PayMpesa, run the following command:

npm install paympesa

or using pnpm:

pnpm add paympesa

or using Yarn:

yarn add paympesa

Configuration

Before using the package, you need to set up the necessary configurations.

STK Push Configuration

const { StkpushConfig } = require("paympesa");

const stkConfig = new StkpushConfig({
  consumerKey: "your_consumer_key",
  consumerSecret: "your_consumer_secret",
  shortCode: "your_shortcode",
  passkey: "your_passkey",
  transactionType: "CustomerPayBillOnline", // or "CustomerBuyGoodsOnline"
  accountReference: "TestTransaction",
  transactionDesc: "Payment for services",
  authUrl: "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",
  stkPushUrl: "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest",
  queryUrl: "https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query",
  callbackUrl: "https://your-callback-url.com/callback",
});

QR Code Configuration

const { QrCodeConfig } = require("paympesa");

const qrConfig = new QrCodeConfig({
  MerchantName: "TEST SUPERMARKET",
  merchantRequestID: "REQ002",
  RefNo: "Invoice Test",
  Amount: 1,
  TrxCode: "PB",
  CPI: "373132",
  Size: "300",
  qrCodeUrl: "https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate",
  consumerKey: "your_consumer_key",
  consumerSecret: "your_consumer_secret",
  saveLocally: true,
  amount: 500,
  refNumber: "REF500",
  shortCode: "654321",
  transactionType: "CustomerBuyGoodsOnline",
  authUrl: "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",
});

Usage

Initiate STK Push

const { initiateSTKPush } = require("paympesa");

async function sendSTKPush() {
  try {
    const response = await initiateSTKPush(stkConfig, {
      phoneNumber: "2547XXXXXXXX",
      amount: 100,
    });
    console.log("STK Push Response:", response);
  } catch (error) {
    console.error("Error initiating STK Push:", error.message || error);
  }
}

sendSTKPush();

Confirm Payment

const { confirmPayment } = require("paympesa");

async function checkPaymentStatus(CheckoutRequestID) {
  try {
    const response = await confirmPayment(stkConfig, CheckoutRequestID);
    console.log("Payment Confirmation Response:", response);
  } catch (error) {
    console.error("Error confirming payment:", error.message || error);
  }
}

checkPaymentStatus("your_checkout_request_id");

Handle STK Push Callback

const { stkPushCallback } = require("paympesa");

async function processCallback(callbackData) {
  try {
    const result = await stkPushCallback(callbackData);
    console.log("Callback Response:", result);
  } catch (error) {
    console.error("Error processing callback:", error.message || error);
  }
}

// Example callback data
const sampleCallbackData = {
  Body: {
    stkCallback: {
      MerchantRequestID: "123456",
      CheckoutRequestID: "789012",
      ResultCode: 0,
      ResultDesc: "Success",
    },
  },
};

processCallback(sampleCallbackData);

Generate QR Code

const { generateQRCode } = require("paympesa");

async function createQRCode() {
  try {
    const qrCodeResponse = await generateQRCode(qrConfig);
    console.log("QR Code Response:", qrCodeResponse);
  } catch (error) {
    console.error("Error generating QR Code:", error.message || error);
  }
}

createQRCode();

Developer

Paul
🚀 Web Developer & Open-Source Contributor
📧 Email: marcochollapaul01@gmail.com
🐦 Twitter: @MarcochollaP
💼 LinkedIn: marcocholla
📖 GitHub: @Marcocholla01

License

This project is licensed under the MIT License.


Note:

  • generateQRCode() is not fully Completed hence might or not work properly.
  • Replace the sandbox with api if going live

Package Sidebar

Install

npm i paympesa

Weekly Downloads

3

Version

2.0.3

License

ISC

Unpacked Size

142 kB

Total Files

17

Last publish

Collaborators

  • marcocholla