safaricom/LNMOnlineAndroidSample

Mpesa STK push Error

Closed this issue · 5 comments

I run my code several times succesfully, then this error started displaying. What could be the problem?
{ "requestId":"27746-3661277-1", "errorCode": "400.002.02", "errorMessage": "Bad Request - Invalid Timestamp" } { "requestId":"27746-3661277-1", "errorCode": "400.002.02", "errorMessage": "Bad Request - Invalid Timestamp" }

I figured it out by creating another application in the sandbox.

timestamp should be in the form of yyyymmddhhiiss

timestamp should be in the form of format('YmdHMS');

@staticmethod
def generate_password() -> tuple:
unformatted_time = datetime.now()
formatted_time = unformatted_time.strftime("%Y%m%d%H%M%S")
data_to_encode = MPESA_BUSINESS_SHORT_CODE + MPESA_PASSKEY + formatted_time
encoded_string = base64.b64encode(data_to_encode.encode())
decoded_password = encoded_string.decode('utf-8')
return decoded_password, formatted_time

//==> This method will sort you out

I had the same error. For quite a while I tried to solve it, then I found 2 bugs:

  1. I had not declared addHeader("Content-Type", "application/json").
  2. There were some field that are required in the request body that I had missed.