M-Pesa Library for Node.js using REST API
This package is intended to assist Node.js developers to use the M-Pesa API.
Install the node modules by running npm install
on the project folder in command prompt to install dependencies.
Before using the transaction API set the consumer key and consumer secret from
My Apps > Select App > copy Consumer Key and Consumer Secret and paste within quotes:
const consumer_key = 'INSERT CONSUMER KEY HERE';
const consumer_secret = 'INSERT CONSUMER SECRET HERE';
On the security function set your security credential value as specified within quotes:
let bufferToEncrypt = new Buffer("ENTER SECURITY CREDENTIAL TEXT HERE");
Additional Notes
- If using on production comment sandbox URL and uncomment production URL, set to sandbox initially.
- ES5 compatible version of library generated with babel located in lib folder of project.
- Click links for more information on possible values and use of each API
Usage
This initiates a business to customer transactions from a company (shortcode) to end users (mobile numbers) of their services.
B2C(initiatorName, commandId, amount, partyA, partyB, remarks, queueUrl, resultUrl, occasion)
Example: mpesa.B2C('testapi', 'BusinessPayment', '100', '600133', '254708374149', 'test', 'http://randomurl.com', 'http://randomurl2.com');
This initiates a business to business transaction between one company to another.
B2B(initiator, commandId, senderId, receiverId, amount, partyA, partyB, accountRef, remarks, queueUrl, resultUrl, occasion)
Example: mpesa.B2B('testapi', 'BusinessPayBill', '4', '4', '1000', '600133', '600000', 'BusinessA', 'test', 'http://randomurl.com', 'http://randomurl2.com', 'test');
This initiates a C2B confirmation and validation registration for a company's URLs
C2B_Register(shortCode, responseType, confirmationUrl, validationUrl)
Example: mpesa.C2B_Register('600133', 'Completed', 'http://randomurl.com', 'http://randomurl2.com');
This initiates a C2B transaction between an end-user and a company (paybill or till number)
C2B_Simulate(shortCode, commandId, amount, msisdn, billRefNumber)
Example: mpesa.C2B_Simulate('600133', 'CustomerPayBillOnline', '300', '254708374149', '0000');
M-Pesa Express Request - Lipa Na M-Pesa Online Payment API
This initiates a Lipa Na M-Pesa Online Payment transaction using STK Push.
Lipa_Na_Mpesa_Online(shortCode, timeStamp, passKey, transactionType, amount, partyA, partyB, phoneNumber, callbackUrl, accountRef, transactionDesc)
Example: mpesa.Lipa_Na_Mpesa_Online('174379', '20180215123520', 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919', 'CustomerPayBillOnline', '1', '254708374149', '174379', '254708374149', 'http://randomurl.com', 'test', 'test');
M-Pesa Express Query Request - Lipa Na M-Pesa Query Request API
This API checks the status of a Lipa Na M-Pesa Online Payment transaction
Lipa_Na_Mpesa_Query(shortCode, timeStamp, passKey, checkoutRequestId)
Example: mpesa.Lipa_Na_Mpesa_Query(174379, '20180215123520', 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919', 'ws_co_123456789');
This initiates an M-Pesa transaction reversal on B2B, B2C or C2B API
Reversal(initiator, commandId, transactionId, amount, receiverParty, receiverIdType, resultUrl, queueUrl, remarks, occasion)
Example: mpesa.Reversal('testapi', 'TransactionReversal', 'LKXXXX1234', '100', '600133', '11', 'http://randomurl.com', 'http://randomurl2.com', 'test', 'test');
This API is used to check the status of B2B, B2C and C2B transactions
Transaction_Status(initiator, commandId, transactionId, partyA, idType, resultUrl, queueUrl, remarks, occasion, originalConversationId)
Example: mpesa.Transaction_Status('testapi', 'TransactionStatusQuery', 'LKXXXX1234', '600133', '4', 'http://randomurl.com', 'http://randomurl2.com', 'test', '4455-6589979');
This initiates a request for the account balance of a shortcode
Account_Balance(initiator, commandId, partyA, idType, remarks, queueUrl, resultUrl)
Example: mpesa.Account_Balance('testapi', 'AccountBalance', '600133', '4', 'test', 'http://randomurl.com', 'http://randomurl2.com');