mds-daraja-sdk
v1.1.11
Published
A simple SDK for integrating with Safaricom's Daraja API for M-Pesa payments.
Downloads
45
Maintainers
Readme
MDS DARAJA SDK
The mds-daraja-sdk
is a simple, easy-to-use Node.js SDK for integrating with the Safaricom M-Pesa API. It supports key M-Pesa functionalities such as STK Push, B2C payments, and transaction reversals. This SDK works with both JavaScript and TypeScript environments.
Table of Contents
Installation
To install the SDK, simply run:
npm install mds-daraja-sdk
Or with Yarn:
yarn add mds-daraja-sdk
Usage
Importing the SDK
The SDK can be imported in both JavaScript and TypeScript projects.
JavaScript:
const { MdsDarajaSdk } = require('mds-daraja-sdk');
TypeScript:
import { MdsDarajaSdk } from 'mds-daraja-sdk';
Initialization
To start using the SDK, you need to create an instance of the MdsDarajaSdk
class. The constructor takes three arguments:
- Consumer Key (from Safaricom Developer Portal)
- Consumer Secret (from Safaricom Developer Portal)
- Sandbox Mode (optional, default is
true
)
Example:
const sdk = new MdsDarajaSdk('your-consumer-key', 'your-consumer-secret', true); // Sandbox mode
For production use:
const sdk = new MdsDarajaSdk('your-consumer-key', 'your-consumer-secret', false); // Production mode
STK Push
The STK Push feature allows you to initiate a payment request to a customer's phone. It requires the following parameters:
businessShortCode
: The short code of your M-Pesa Paybill or Till Number.password
: Base64-encoded string derived from your short code, passkey, and timestamp.timestamp
: The current timestamp in the formatyyyyMMddHHmmss
.phoneNumber
: The customer's phone number (in the format 2547XXXXXXXX).amount
: The amount to be paid.callbackUrl
: The URL Safaricom will call to confirm payment status.
Example:
const businessShortCode = '174379'; // Your shortcode
const passkey = 'your-lipa-na-mpesa-passkey'; // Safaricom passkey
const timestamp = MdsDarajaSdk.generateTimeStamp();
const password = MdsDarajaSdk.generatePassword(businessShortCode, passkey);
const phoneNumber = '254712345678'; // Customer's phone number
const amount = 100;
const callbackUrl = 'https://example.com/callback';
sdk.initiateStkPush(businessShortCode, password, timestamp, phoneNumber, amount, callbackUrl)
.then(response => console.log('STK Push Response:', response))
.catch(error => console.error('STK Push Error:', error));
Transaction Reversal
The transaction reversal feature allows you to reverse a mistaken transaction.
Parameters:
shortCode
: The short code initiating the reversal.password
: Security credential.transactionID
: The ID of the transaction to be reversed.
Example:
const shortCode = '600000'; // Your shortcode
const password = 'your-security-credential'; // Safaricom security credential
const transactionID = 'LKXXXX1234'; // The ID of the transaction to reverse
sdk.reverseTransaction(shortCode, password, transactionID)
.then(response => console.log('Reversal Response:', response))
.catch(error => console.error('Reversal Error:', error));
B2C Payments
B2C (Business to Customer) payments allow you to send money from your business to a customer.
Parameters:
shortCode
: The short code initiating the payment.password
: Security credential.timestamp
: The current timestamp.phoneNumber
: The customer's phone number.amount
: The amount to be paid.
Example:
const shortCode = '600000'; // Your shortcode
const password = 'your-security-credential'; // Safaricom security credential
const timestamp = MdsDarajaSdk.generateTimeStamp();
const phoneNumber = '254712345678'; // Recipient's phone number
const amount = 1000; // Amount to send
sdk.initiateB2cPayment(shortCode, password, timestamp, phoneNumber, amount)
.then(response => console.log('B2C Response:', response))
.catch(error => console.error('B2C Error:', error));
Utilities
Generate Password
The password for STK Push is generated using your Business Short Code, Passkey, and a timestamp.
const password = MdsDarajaSdk.generatePassword('shortCode', 'passkey');
Generate Timestamp
Generate a Safaricom-compliant timestamp in the format yyyyMMddHHmmss
:
const timestamp = MdsDarajaSdk.generateTimeStamp();
Error Handling
All methods return a Promise
. Use .then()
and .catch()
to handle the responses and errors.
Example:
sdk.initiateStkPush(businessShortCode, password, timestamp, phoneNumber, amount, callbackUrl)
.then(response => {
console.log('STK Push successful:', response);
})
.catch(error => {
console.error('Error initiating STK Push:', error);
});
Common error scenarios include:
- Invalid consumer key or secret.
- Incorrect API credentials.
- Network-related issues.
For detailed error codes, consult the M-Pesa API Documentation.
Contributing
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a new feature branch (
git checkout -b feature-branch
). - Commit your changes (
git commit -m 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Open a pull request.
For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License. See the LICENSE
file for more details.
Notes
- Ensure your callback URLs are publicly accessible when testing M-Pesa API features like STK Push and B2C payments.
- For security, make sure you do not expose sensitive credentials (consumer key, secret, passkey, etc.) in your production code or version control.