safaricom / mpesa-php-sdk

A PHP sdk for the new Mpesa RESTful APIs
188 stars 211 forks source link

AccountBalance API returns "The initiator information is invalid." #41

Closed qlint closed 4 years ago

qlint commented 4 years ago

I've followed the documentation for the AccountBalance but keep ending up with the same message, that the Initiator information is invalid. I'm assuming this could be one of two possibilites, either the $Initiator value is wrong or the $SecurityCredential.

This is how I'm computing the $securityCredential

    $plainTextPwd = 'somePassword';
    $pubKeyFile =  __DIR__ . "/cert/cert.cer";
    $publicKey = '';
    if(\is_file($pubKeyFile)){
        $publicKey = file_get_contents($pubKeyFile);
        openssl_public_encrypt($plainTextPwd, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING);
        $securityCredential = base64_encode($encrypted);
        echo $securityCredential; // This is my $SecurityCredential value
    }else{
        throw new \Exception("Please provide a valid public key file");
    }

Which gives me a $securityCredential which is 344 characters long.

As for the $Initiator This is what the documentation says at https://developer.safaricom.co.ke/account-balance/apis/post/query

Initiator This is the credential/username used to authenticate the transaction request

My confusion is - what is credential/username given that there is an initiator username and there is an initiator credential (assuming credential means password)?

I've tried using either but still get the same message though - "Initiator information is invalid.". What could be happening?

kelvinthiongo commented 3 years ago

Why does Safaricom ignore all these issues, very sad? I am facing the same problem too but no help.

kievo23 commented 3 years ago

I am stuck too. For two weeks. Safaricom's monopoly is hurting the market.

mossey commented 3 years ago

Hi All, There exists an issue in the documentation and I intend to make a fix to it.

To clarify, the check account balance method takes these parameters: $CommandID, $Initiator, $SecurityCredential, $PartyA, $IdentifierType, $Remarks, $QueueTimeOutURL, $ResultURL

The issue of initiator information is invalid is caused by you using a wrong combination of $Initiator and $SecurityCredential. Looking at my test credentials below,

Screenshot 2021-01-04 at 14 03 51

Looking at the test credentials shared above, the $Initiator field should be populated by the value of Initiator Name (Shortcode 1) which in this case is Safaricomapi. In production, It is the name of the API initiator that you create on the org portal.

The $SecurityCredential field is populated by encrypting the value of Security Credential (Shortcode 1) using the production certificate when in production or sandbox certificate when in sandbox. The security credential value does not change unless you change the initiator password in the org portal in prod. You may generate the security credential by doing an encryption of its value below the credentials as shown on the screenshot below.

Screenshot 2021-01-06 at 11 35 42
Whizboy-Arnold commented 2 years ago

Hi All, There exists an issue in the documentation and I intend to make a fix to it.

To clarify, the check account balance method takes these parameters: $CommandID, $Initiator, $SecurityCredential, $PartyA, $IdentifierType, $Remarks, $QueueTimeOutURL, $ResultURL

The issue of initiator information is invalid is caused by you using a wrong combination of $Initiator and $SecurityCredential. Looking at my test credentials below,

Screenshot 2021-01-04 at 14 03 51

Looking at the test credentials shared above, the $Initiator field should be populated by the value of Initiator Name (Shortcode 1) which in this case is Safaricomapi. In production, It is the name of the API initiator that you create on the org portal.

The $SecurityCredential field is populated by encrypting the value of Security Credential (Shortcode 1) using the production certificate when in production or sandbox certificate when in sandbox. The security credential value does not change unless you change the initiator password in the org portal in prod. You may generate the security credential by doing an encryption of its value below the credentials as shown on the screenshot below. Screenshot 2021-01-06 at 11 35 42

where do i get these test credentials in the new api. there is no such a page and the ones on the simulator don;t seem towork

Whizboy-Arnold commented 2 years ago

where do i get these test credentials in the new api. there is no such a page and the ones on the simulator don;t seem towork

asifsami73 commented 2 years ago

While trying to b2c, I'm getting this error, The initiator's information is invalid, I can't find any solution to this problem.

tmhevencent commented 11 months ago

Has anyone succeeded in this securityCredential thing, i have tried using certificate and openssl but still getting the error. Safaricom daraja has no clear formats of generating securityCredential.

qlint commented 10 months ago

Has anyone succeeded in this securityCredential thing, i have tried using certificate and openssl but still getting the error. Safaricom daraja has no clear formats of generating securityCredential.

Try this:

$pass = "YOUR-PASSWORD-IN-https://org.ke.m-pesa.com";
$publicKey = file_get_contents('./your-path-to/ProductionCertificate.cer');
openssl_public_encrypt($pass, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING);
$securityCredentials = base64_encode($encrypted);
MarkxOBrian commented 2 months ago

For those like me who are here looking for Test credentials for Daraja API V2 Sandbox, you can send an email to APIfeedback@safaricom.co.ke or APIsupport@safaricom.co.ke and they will send you updated credentials. I realised they get updated after a while.