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.
- Initiate STK Push transactions
- Confirm payment status
- Handle STK Push callbacks
- Generate QR codes for M-Pesa payments
- Easy configuration and extensibility
To install PayMpesa, run the following command:
npm install paympesa
or using pnpm:
pnpm add paympesa
or using Yarn:
yarn add paympesa
Before using the package, you need to set up the necessary configurations.
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",
});
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",
});
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();
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");
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);
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();
Paul
🚀 Web Developer & Open-Source Contributor
📧 Email: marcochollapaul01@gmail.com
🐦 Twitter: @MarcochollaP
💼 LinkedIn: marcocholla
📖 GitHub: @Marcocholla01
This project is licensed under the MIT License.
Note:
generateQRCode()
is not fully Completed hence might or not work properly.- Replace the
sandbox
withapi
if going live