-
Notifications
You must be signed in to change notification settings - Fork 21
Add B2B #5
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
base: master
Are you sure you want to change the base?
Add B2B #5
Conversation
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds B2B payment support by introducing a new intiate_b2b function. The implementation is straightforward, but I've provided a suggestion to improve the new function. The feedback focuses on fixing a typo in the function name, adhering to common JavaScript naming conventions for parameters, improving code clarity with modern syntax like template literals, and ensuring consistency with the existing codebase.
| module.exports.intiate_b2b = async function (input_Amount, input_TransactionReference, input_ThirdPartyReference, input_PrimaryPartyCode, input_ReceiverPartyCode) { | ||
| initialize_api_from_dotenv(); | ||
| try { | ||
| let response; | ||
| response = await axios({ | ||
| method: 'post', | ||
| url: 'https://' + mpesaConfig.baseUrl + ':18349/ipg/v1x/b2bPayment/', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': 'Bearer ' + _getBearerToken(mpesaConfig.publicKey, mpesaConfig.apiKey), | ||
| 'Origin': mpesaConfig.origin | ||
| }, | ||
| data: { | ||
| "input_TransactionReference": input_TransactionReference, | ||
| "input_Amount": input_Amount + "", | ||
| "input_ThirdPartyReference": input_ThirdPartyReference, | ||
| "input_PrimaryPartyCode": input_PrimaryPartyCode, | ||
| "input_ReceiverPartyCode": input_ReceiverPartyCode | ||
| } | ||
| }); | ||
| return response.data; | ||
| } catch(e) { | ||
| if (e.response && e.response.data) { | ||
| throw e.response.data; | ||
| } else { | ||
| throw e; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new function has a few areas for improvement regarding consistency, conventions, and clarity.
- Typo: There's a typo in the function name (
intiate_b2b). It should beinitiate_b2bto match other functions. - Parameter Naming: The parameter names (e.g.,
input_Amount) are inconsistent with JavaScript'scamelCaseconvention. Theinput_prefix is also redundant. - Code Clarity:
- The
responsevariable can be declared withconstas it's not reassigned. - Using template literals for URL and Authorization header strings is cleaner.
- Explicitly converting
input_Amountto a string withString()is more readable than using+ "".
- The
- Consistency: The function definition is missing a semicolon at the end, which is present on other similar functions in this file.
Here is a refactored version that addresses all these points:
module.exports.initiate_b2b = async function (amount, transactionReference, thirdPartyReference, primaryPartyCode, receiverPartyCode) {
initialize_api_from_dotenv();
try {
const response = await axios({
method: 'post',
url: `https://${mpesaConfig.baseUrl}:18349/ipg/v1x/b2bPayment/`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${_getBearerToken(mpesaConfig.publicKey, mpesaConfig.apiKey)}`,
'Origin': mpesaConfig.origin
},
data: {
"input_TransactionReference": transactionReference,
"input_Amount": String(amount),
"input_ThirdPartyReference": thirdPartyReference,
"input_PrimaryPartyCode": primaryPartyCode,
"input_ReceiverPartyCode": receiverPartyCode
}
});
return response.data;
} catch (e) {
if (e.response && e.response.data) {
throw e.response.data;
} else {
throw e;
}
}
};
Summary
This PR adds B2B support to the library