@saverious/daraja
module was designed to simplify integration of daraja api endpoints by reducing not only the
syntax complexity of the raw api, but also the number of required arguments
As of the current date, the module supports only the M-Pesa Express API (stk-push)
- M-Pesa Express
npm i @saverious/daraja
require('dotenv').config();
const Daraja = require('@saverious/daraja');
const { CONSUMER_KEY, CONSUMER_SECRET, PHONE, CALLBACK } = process.env;
async function payViaMpesa() {
try{
const daraja = new Daraja({
consumer_key : CONSUMER_KEY,
consumer_secret : CONSUMER_SECRET,
environment : 'development'
});
const response = await daraja.stkPush({
sender_phone : PHONE,
payBillOrTillNumber : '174379',
amount : '1',
callback_url : CALLBACK
});
console.log('safaricom response : ', response);
}catch(e){
console.log('payment error : ', e);
}
}
payViaMpesa();
-
options
<object> Set of configurable options to set on the class. Can have the following fields:-
consumer_key
<string> Your consumer key from the safaricom portal -
consumer_secret
<string> Your consumer secret from the safaricom portal -
environment
<string> The current project environment. One of eitherdevelopment
orproduction
-
All the three key/value pairs in options
object are required
-
options
<object> Options containing payment details. Can have the following fields:-
sender_phone
<string> The sender's phone number.required : true
-
payBillOrTillNumber
<string> The account number (Paybill number or Till number) to which funds should be sent.required : true
-
amount
<string> The amount to be sent.required : true
-
callback_url
<string> The URL to which the response from safaricom should be sent.required : true
-
passkey
<string> The passkey from safaricom.required : true
only ifenvironment === production
, otherwiserequired : false
-
account_reference
<string> An Identifier of the transaction for the CustomerPayBillOnline transaction type.required : false
-
transaction_desc
<string> Any additional information/comment that can be sent along with the request from your system.required : false
-
-
Returns
<Promise>