newtonmunene99 / mpesa-api

A NodeJs Module to help you with M-Pesa Daraja API calls.
http://npmjs.com/package/mpesa-api
MIT License
108 stars 54 forks source link

Getting "The initiator information is invalid." when Live #12

Closed cngeru closed 4 years ago

cngeru commented 4 years ago

I'm getting the error above in production when using the Account Balance API and the B2C. Could it be an error when generating the security credential coz everything else works. Here's an example of the error (Account Balance)

{"ResultDesc":"The initiator information is invalid.",
"ConversationID":"AG_20200625_0000650ea6a99ae1c0bb",
"TransactionID":"OFP0000000",
"ResultParameters":{"ResultParameter":{"Key":"BOCompletedTime","Value":20200625232427}},
"ResultCode":2001,"ReferenceData":{"ReferenceItem":{"Value":"https://internalapi.safaricom.co.ke/mpesa/abresults/v1/submit","Key":"QueueTimeoutURL"}},
"OriginatorConversationID":"13443-10746354-1",
"ResultType":0} 
newtonmunene99 commented 4 years ago

Hi there, does this occur in the sandbox? It doesn't seem to suggest a problem with the library, you might be passing the wrong initiator name. When you say everything else works, does that include other methods which require initiator name?

cngeru commented 4 years ago

The withdrawal works in postman but fails when i use the code with the same credentials

cngeru commented 4 years ago

Any solution to this problem ?

newtonmunene99 commented 4 years ago

Hi @cngeru i'm still unable to reproduce this. You mentioned it worked for you on Postman. Can you share your request, with dummy values for sensitive fields.

cngeru commented 4 years ago

The request body in postman

{
    "InitiatorName": "CODE",
    "SecurityCredential": "CODE",
    "CommandID": "BusinessPayment",
    "Amount": "100",
    "PartyA": "CODE",
    "PartyB": "CODE",
    "Remarks": "CODEWithdrawal",
    "QueueTimeOutURL": "https://us-central1-CODE.cloudfunctions.net/mpesaWithdrawalTimeout?userId=xx&wAuth=xx",
    "ResultURL": "https://us-central1-CODE.cloudfunctions.net/mpesaWithdrawalResult?userId=xx&wAuth=xx",
    "Occasion": "CODE Withdrawal"
}

The code (JS)

const B2C_CREDS ={
  client_key: CODE,
  client_secret: 'CODE',
  initiator_password: "CODE",
  certificatepath: null
}

const B2C_DETAILS= {
  SHORTCODE : CODE,
  INITIATOR_NAME:'CODE',
};

const environment = "production";
const B2C = new Mpesa(B2C_CREDS, environment);

try {
    await B2C.b2c({
      Initiator:B2C_DETAILS.INITIATOR_NAME,
      Amount: amount,
      PartyA: B2C_DETAILS.SHORTCODE,
      PartyB: phoneNumber,
      QueueTimeOutURL:timeout,
      ResultURL: result,
      CommandID: "BusinessPayment",
      Occasion: `Withdrawal By ${CODE}`,
      Remarks: "CODE Withdrawal" 
    })
    .then((mpesaResponse)=>{
      console.log(JSON.stringify(mpesaResponse));
      return {result: 'Mpesa Transaction was successful'};
    })
newtonmunene99 commented 4 years ago

@cngeru I'm still looking into this, in the meantime, I've added the option to pass security credentials when initiating. This means you can generate the security credential in your M-Pesa web portal and pass it statically as a string. The library will generate it automatically if you exclude this field or pass it as null.

cngeru commented 4 years ago

Alrighty