Skip to content
Open
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
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,33 @@ module.exports.initiate_b2c = async function (amount, msisdn, transaction_ref, t
}
}
};

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;
}
}
}
Comment on lines +121 to +149

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