Open
Conversation
Owner
|
/gemini review |
There was a problem hiding this comment.
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.
Comment on lines
+121
to
+149
| 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.
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;
}
}
};
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds B2B support to the library