0

I am working on an android app with an Mpesa payment module. I have already been able to generate the STKPush giuded by this tutorial on medium: https://medium.com/@lentimo/m-pesa-integration-on-android-part-2-75430ccda2fe . My goal is to be able to determine whether after receiving the STKPush, the customer processes the transaction to completion. Thus, I intend to use the Lipa Na M-Pesa Query Request API. To make a request here, you require the checkoutRequestId as one of the parameters. Thus my question, how can you reference the the checkoutRequestId from the code dynamically? Does anyone know of open source code pertaining Lipa Na M-Pesa Query Request API in android java environment? And lastly, is there a simpler way to achieve the aforesaid goal?

Below is what i want to reference: HTTP/1.1 200 OK Cache-Control: no-store Connection: keep-alive Content-Length: 314 Content-Type: application/json;charset=UTF-8 Date: Tue, 10 Mar 2020 18:25:08 GMT { "MerchantRequestID": "7041-3944257-1", **"CheckoutRequestID": "ws_CO_100320202125091065",** "ResponseCode": "0", "ResponseDescription": "Success. Request accepted for processing", "CustomerMessage": "Success. Request accepted for processing" }

I am operating in sandbox environment.

Thanks in advance.

1 Answers1

1

When you make a successful request, in the onResponse block, parse the json response and extract the CheckoutRequestID. Keep reference to it, then when making the status query you can use it as required.

mApiClient.mpesaService().sendPush(stkPush).enqueue(new Callback<STKPush>() {
            @Override
            public void onResponse(@NonNull Call<STKPush> call, @NonNull Response<STKPush> response) {
                try {
                    if (response.isSuccessful()) {
                        //Parse JSON response here to extract CheckoutRequestID
                    } else {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<STKPush> call, @NonNull Throwable t) {
                //handle error
            }
        });

As from the mpesa api docs an example of an accepted request response looks like the below

  {
    "Body":{
      "stkCallback":{
        "MerchantRequestID":"19465-780693-1",
        "CheckoutRequestID":"ws_CO_27072017154747416",
        "ResultCode":0,
        "ResultDesc":"The service request is processed successfully.",
        "CallbackMetadata":{
          "Item":[
            {
              "Name":"Amount",
              "Value":1
            },
            {
              "Name":"MpesaReceiptNumber",
              "Value":"LGR7OWQX0R"
            },
            {
              "Name":"Balance"
            },
            {
              "Name":"TransactionDate",
              "Value":20170727154800
            },
            {
              "Name":"PhoneNumber",
              "Value":254721566839
            }
          ]
        }
      }
    }
  }
Peter M.
  • 26
  • 3