0

我正在开发一个带有 Mpesa 支付模块的 android 应用程序。我已经能够在媒体上生成本教程指导的 STKPush:https ://medium.com/@lentimo/m-pesa-integration-on-android-part-2-75430ccda2fe 。我的目标是能够确定客户收到STKPush后是否处理交易完成。因此,我打算使用 Lipa Na M-Pesa 查询请求 API。要在此处发出请求,您需要checkoutRequestId作为参数之一。因此我的问题是,如何动态地从代码中引用checkoutRequestId ?有谁知道在 android java 环境中有关 Lipa Na M-Pesa 查询请求 API 的开源代码?最后,有没有更简单的方法来实现上述目标?

以下是我想参考的内容: 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" }

我在沙盒环境中操作。

提前致谢。

4

1 回答 1

1

当您发出成功的请求时,在 onResponse 块中,解析 json 响应并提取 CheckoutRequestID。保持引用,然后在进行状态查询时可以根据需要使用它。

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
            }
        });

mpesa api docs一个接受的请求响应的示例如下所示

  {
    "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
            }
          ]
        }
      }
    }
  }
于 2020-04-09T12:33:25.243 回答