Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format code according to prettier config #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
4 changes: 4 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
printWidth: 100,
singleQuote: true,
};
34 changes: 21 additions & 13 deletions src/endpoints/account-balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
* @param {String} [commandId='AccountBalance'] Takes only 'AccountBalance' CommandID
* @return {Promise} This returns a promise that resolves to the account balance
*/
module.exports = async function accountBalance (shortCode, idType, queueUrl, resultUrl, remarks = 'Checking account balance', initiator = null, commandId = 'AccountBalance') {
const securityCredential = this.security()
const req = await this.request()
module.exports = async function accountBalance(
shortCode,
idType,
queueUrl,
resultUrl,
remarks = 'Checking account balance',
initiator = null,
commandId = 'AccountBalance'
) {
const securityCredential = this.security();
const req = await this.request();
return req.post('/mpesa/accountbalance/v1/query', {
'Initiator': initiator || this.configs.initiatorName,
'SecurityCredential': securityCredential,
'CommandID': commandId,
'PartyA': shortCode,
'IdentifierType': idType,
'Remarks': remarks,
'QueueTimeOutURL': queueUrl,
'ResultURL': resultUrl
})
}
Initiator: initiator || this.configs.initiatorName,
SecurityCredential: securityCredential,
CommandID: commandId,
PartyA: shortCode,
IdentifierType: idType,
Remarks: remarks,
QueueTimeOutURL: queueUrl,
ResultURL: resultUrl,
});
};
46 changes: 29 additions & 17 deletions src/endpoints/b2b.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,33 @@
* @param {String} [remarks='B2B Request'] Comments that are sent along with the transaction.
* @return {Promise}
*/
module.exports = async function (senderParty, receiverParty, amount, queueUrl, resultUrl, senderType = 4, receiverType = 4, initiator = null, commandId = 'BusinessToBusinessTransfer', accountRef = null, remarks = 'B2B Request') {
const req = await this.request()
const securityCredential = this.security()
module.exports = async function (
senderParty,
receiverParty,
amount,
queueUrl,
resultUrl,
senderType = 4,
receiverType = 4,
initiator = null,
commandId = 'BusinessToBusinessTransfer',
accountRef = null,
remarks = 'B2B Request'
) {
const req = await this.request();
const securityCredential = this.security();
return req.post('/mpesa/b2b/v1/paymentrequest', {
'Initiator': initiator || this.configs.initiatorName,
'SecurityCredential': securityCredential,
'CommandID': commandId,
'SenderIdentifierType': senderType,
'RecieverIdentifierType': receiverType,
'Amount': amount,
'PartyA': senderParty,
'PartyB': receiverParty,
'AccountReference': accountRef,
'Remarks': remarks,
'QueueTimeOutURL': queueUrl,
'ResultURL': resultUrl
})
}
Initiator: initiator || this.configs.initiatorName,
SecurityCredential: securityCredential,
CommandID: commandId,
SenderIdentifierType: senderType,
RecieverIdentifierType: receiverType,
Amount: amount,
PartyA: senderParty,
PartyB: receiverParty,
AccountReference: accountRef,
Remarks: remarks,
QueueTimeOutURL: queueUrl,
ResultURL: resultUrl,
});
};
40 changes: 25 additions & 15 deletions src/endpoints/b2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@
* @param {string} occasion
* @return {Promise}
*/
module.exports = async function (senderParty, receiverParty, amount, queueUrl, resultUrl, commandId = 'BusinessPayment', initiatorName = null, remarks = 'B2C Payment', occasion) {
const securityCredential = this.security()
const req = await this.request()
module.exports = async function (
senderParty,
receiverParty,
amount,
queueUrl,
resultUrl,
commandId = 'BusinessPayment',
initiatorName = null,
remarks = 'B2C Payment',
occasion
) {
const securityCredential = this.security();
const req = await this.request();
return req.post('/mpesa/b2c/v1/paymentrequest', {
'InitiatorName': initiatorName || this.configs.initiatorName,
'SecurityCredential': securityCredential,
'CommandID': commandId,
'Amount': amount,
'PartyA': senderParty,
'PartyB': receiverParty,
'Remarks': remarks,
'QueueTimeOutURL': queueUrl,
'ResultURL': resultUrl,
'Occasion': occasion
})
}
InitiatorName: initiatorName || this.configs.initiatorName,
SecurityCredential: securityCredential,
CommandID: commandId,
Amount: amount,
PartyA: senderParty,
PartyB: receiverParty,
Remarks: remarks,
QueueTimeOutURL: queueUrl,
ResultURL: resultUrl,
Occasion: occasion,
});
};
21 changes: 13 additions & 8 deletions src/endpoints/c2b-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
* @param {string} [responseType='Completed'] Default response type for timeout. Incase a tranaction times out, Mpesa will by default Complete or Cancel the transaction
* @return {Promise}
*/
module.exports = async function (confirmationUrl, validationUrl, shortCode = null, responseType = 'Completed') {
const req = await this.request()
module.exports = async function (
confirmationUrl,
validationUrl,
shortCode = null,
responseType = 'Completed'
) {
const req = await this.request();
return req.post('/mpesa/c2b/v1/registerurl', {
'ShortCode': shortCode || this.configs.shortCode,
'ResponseType': responseType,
'ConfirmationURL': confirmationUrl,
'ValidationURL': validationUrl
})
}
ShortCode: shortCode || this.configs.shortCode,
ResponseType: responseType,
ConfirmationURL: confirmationUrl,
ValidationURL: validationUrl,
});
};
24 changes: 15 additions & 9 deletions src/endpoints/c2b-simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
* @param {number} [shortCode=null] Short Code receiving the amount being transacted
* @return {Promise}
*/
module.exports = async function (msisdn, amount, billRefNumber, commandId = 'CustomerPayBillOnline', shortCode = null) {
const req = await this.request()
module.exports = async function (
msisdn,
amount,
billRefNumber,
commandId = 'CustomerPayBillOnline',
shortCode = null
) {
const req = await this.request();
return req.post('/mpesa/c2b/v1/simulate', {
'ShortCode': shortCode || this.configs.shortCode,
'CommandID': commandId,
'Amount': amount,
'Msisdn': msisdn,
'BillRefNumber': billRefNumber
})
}
ShortCode: shortCode || this.configs.shortCode,
CommandID: commandId,
Amount: amount,
Msisdn: msisdn,
BillRefNumber: billRefNumber,
});
};
25 changes: 13 additions & 12 deletions src/endpoints/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const accountBalance = require('./account-balance')
const b2b = require('./b2b')
const b2c = require('./b2c')
const c2bRegister = require('./c2b-register')
const c2bSimulate = require('./c2b-simulate')
const lipaNaMpesaOnline = require('./lipa-na-mpesa-online')
const lipaNaMpesaQuery = require('./lipa-na-mpesa-query')
const oAuth = require('./oauth')
const reversal = require('./reversal')
const transactionStatus = require('./transaction-status')
/* eslint-disable @typescript-eslint/no-var-requires */
const accountBalance = require('./account-balance');
const b2b = require('./b2b');
const b2c = require('./b2c');
const c2bRegister = require('./c2b-register');
const c2bSimulate = require('./c2b-simulate');
const lipaNaMpesaOnline = require('./lipa-na-mpesa-online');
const lipaNaMpesaQuery = require('./lipa-na-mpesa-query');
const oAuth = require('./oauth');
const reversal = require('./reversal');
const transactionStatus = require('./transaction-status');

module.exports = {
accountBalance,
Expand All @@ -19,5 +20,5 @@ module.exports = {
lipaNaMpesaQuery,
reversal,
transactionStatus,
oAuth
}
oAuth,
};
50 changes: 31 additions & 19 deletions src/endpoints/lipa-na-mpesa-online.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,35 @@
* @param {string} [passKey=null] Lipa na mpesa passKey
* @return {Promise}
*/
module.exports = async function (senderMsisdn, amount, callbackUrl, accountRef, transactionDesc = 'Lipa na mpesa online', transactionType = 'CustomerPayBillOnline', shortCode = null, passKey = null) {
const _shortCode = shortCode || this.configs.lipaNaMpesaShortCode
const _passKey = passKey || this.configs.lipaNaMpesaShortPass
const timeStamp = (new Date()).toISOString().replace(/[^0-9]/g, '').slice(0, -3)
const password = Buffer.from(`${_shortCode}${_passKey}${timeStamp}`).toString('base64')
const req = await this.request()
module.exports = async function (
senderMsisdn,
amount,
callbackUrl,
accountRef,
transactionDesc = 'Lipa na mpesa online',
transactionType = 'CustomerPayBillOnline',
shortCode = null,
passKey = null
) {
const _shortCode = shortCode || this.configs.lipaNaMpesaShortCode;
const _passKey = passKey || this.configs.lipaNaMpesaShortPass;
const timeStamp = new Date()
.toISOString()
.replace(/[^0-9]/g, '')
.slice(0, -3);
const password = Buffer.from(`${_shortCode}${_passKey}${timeStamp}`).toString('base64');
const req = await this.request();
return req.post('/mpesa/stkpush/v1/processrequest', {
'BusinessShortCode': _shortCode,
'Password': password,
'Timestamp': timeStamp,
'TransactionType': transactionType,
'Amount': amount,
'PartyA': senderMsisdn,
'PartyB': _shortCode,
'PhoneNumber': senderMsisdn,
'CallBackURL': callbackUrl,
'AccountReference': accountRef,
'TransactionDesc': transactionDesc
})
}
BusinessShortCode: _shortCode,
Password: password,
Timestamp: timeStamp,
TransactionType: transactionType,
Amount: amount,
PartyA: senderMsisdn,
PartyB: _shortCode,
PhoneNumber: senderMsisdn,
CallBackURL: callbackUrl,
AccountReference: accountRef,
TransactionDesc: transactionDesc,
});
};
25 changes: 14 additions & 11 deletions src/endpoints/lipa-na-mpesa-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
* @return {Promise}
*/
module.exports = async function (checkoutRequestId, shortCode = null, passKey = null) {
const _shortCode = shortCode || this.configs.lipaNaMpesaShortCode
const _passKey = passKey || this.configs.lipaNaMpesaShortPass
const timeStamp = (new Date()).toISOString().replace(/[^0-9]/g, '').slice(0, -3)
const password = Buffer.from(`${_shortCode}${_passKey}${timeStamp}`).toString('base64')
const req = await this.request()
const _shortCode = shortCode || this.configs.lipaNaMpesaShortCode;
const _passKey = passKey || this.configs.lipaNaMpesaShortPass;
const timeStamp = new Date()
.toISOString()
.replace(/[^0-9]/g, '')
.slice(0, -3);
const password = Buffer.from(`${_shortCode}${_passKey}${timeStamp}`).toString('base64');
const req = await this.request();
return req.post('/mpesa/stkpushquery/v1/query', {
'BusinessShortCode': _shortCode,
'Password': password,
'Timestamp': timeStamp,
'CheckoutRequestID': checkoutRequestId
})
}
BusinessShortCode: _shortCode,
Password: password,
Timestamp: timeStamp,
CheckoutRequestID: checkoutRequestId,
});
};
15 changes: 8 additions & 7 deletions src/endpoints/oauth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const axios = require('axios')
const axios = require('axios');

module.exports = function (consumerKey, consumerSecret, baseURL = null) {
const auth = Buffer.from(consumerKey + ':' + consumerSecret).toString('base64')
const auth = Buffer.from(consumerKey + ':' + consumerSecret).toString('base64');
return axios.get((baseURL || this.baseURL) + '/oauth/v1/generate?grant_type=client_credentials', {
headers: {
'Authorization': 'Basic ' + auth,
'content-type': 'application/json'
}
})
}
Authorization: 'Basic ' + auth,
'content-type': 'application/json',
},
});
};
43 changes: 27 additions & 16 deletions src/endpoints/reversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@
* @param {String} [commandId='TransactionReversal'] Takes only 'TransactionReversal' Command id
* @return {Promise}
*/
module.exports = async function (transactionId, amount, queueUrl, resultUrl, shortCode = null, remarks = 'Reversal', occasion = 'Reversal', initiator = null, receiverIdType = '11', commandId = 'TransactionReversal') {
const securityCredential = this.security()
const req = await this.request()
module.exports = async function (
transactionId,
amount,
queueUrl,
resultUrl,
shortCode = null,
remarks = 'Reversal',
occasion = 'Reversal',
initiator = null,
receiverIdType = '11',
commandId = 'TransactionReversal'
) {
const securityCredential = this.security();
const req = await this.request();
return req.post('/mpesa/reversal/v1/request', {
'Initiator': initiator || this.configs.initiatorName,
'SecurityCredential': securityCredential,
'CommandID': commandId,
'TransactionID': transactionId,
'Amount': amount,
'ReceiverParty': shortCode || this.configs.shortCode,
'RecieverIdentifierType': receiverIdType,
'ResultURL': resultUrl,
'QueueTimeOutURL': queueUrl,
'Remarks': remarks,
'Occasion': occasion
})
}
Initiator: initiator || this.configs.initiatorName,
SecurityCredential: securityCredential,
CommandID: commandId,
TransactionID: transactionId,
Amount: amount,
ReceiverParty: shortCode || this.configs.shortCode,
RecieverIdentifierType: receiverIdType,
ResultURL: resultUrl,
QueueTimeOutURL: queueUrl,
Remarks: remarks,
Occasion: occasion,
});
};
Loading