I echoed this and fetched in ajax .
$result= $this->mpesa->STKPushQuery($checkoutRequestID, 174379, "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919");
The results am getting in json is:
{"requestId":"","errorCode":"400.002.02","errorMessage":"Bad Request - Invalid CheckoutRequestID"}
Now in my php code I need to get the Keys of errorCode that sometimes is a successCode so when I try this:
if ($result->errorCode=="400.002.02"){ $Data = '{"status":"Submit payment before Checking for it"}'; echo $Data;
Its fine because the errorCode is found in Json. When there is a success message i.e:
if ($result->successCode=="0"){ $Data = '{"status":"Payment Successful"}'; echo $Data;
}
I get an error with the first statement. Because errorCode is not found in Json So what I actually need is to get the key of json(which will be either errorCode or successCode) i.e
$mystatusCode== Get the either the errorCode or SuccessCode (key in Json array[1]) if ($results->mystatusCode=="400.002.02"){ $Data = '{"status":"Submit payment before Checking for it"}'; echo $Data; }else if ($results->mystatusCode=="0"){ $Data = '{"status":"Payment has been processed successfully"}'; echo $Data; }
Advertisement
Answer
Using isset
will accomplish your goal:
$mystatusCode = ( isset($result->errorCode) ? $result->errorCode : ( isset($result->successCode) ? $result->successCode : NULL ) );
Or you can just use it in the if statements directly without making a new single var:
if (isset($result->errorCode) && $result->errorCode == "400.002.02") { ... } elseif (isset($result->successCode) && $result->successCode == "0") { ... }