Skip to content

Conversation

@IvanBila
Copy link

Summary

This PR adds B2B support to the library

@thatfiredev
Copy link
Owner

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

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;
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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 be initiate_b2b to match other functions.
  • Parameter Naming: The parameter names (e.g., input_Amount) are inconsistent with JavaScript's camelCase convention. The input_ prefix is also redundant.
  • Code Clarity:
    • The response variable can be declared with const as it's not reassigned.
    • Using template literals for URL and Authorization header strings is cleaner.
    • Explicitly converting input_Amount to a string with String() is more readable than using + "".
  • 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;
        }
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants