PhonePe 支付网关在 Java Android Studio 中不起作用

PhonePe Payment Gateway Not Working in Java Android Studio

提问人:Mr. Programmer 提问时间:11/17/2023 更新时间:11/17/2023 访问量:23

问:

这是我的代码:

private void payUsingPhonePe() throws JSONException, NoSuchAlgorithmException {
    String MERCHANT_ID = "MERCHANTUAT";
    String salt = "a6334ff7-da0e-4d51-a9ce-76b97d518b1e";
    String apiEndPoint = "/pg/v1/pay";
    PhonePe.init(this, PhonePeEnvironment.UAT, MERCHANT_ID, "com.igcshop.igc");
    JSONObject data = new JSONObject();
    data.put("merchantTransactionId", orderId);
    data.put("merchantId", MERCHANT_ID);
    data.put("amount", total);
    data.put("mobileNumber", phone);
    data.put("callbackUrl", "https://webhook.site/callback-url");
    JSONObject paymentInstrument = new JSONObject();
    paymentInstrument.put("type", "UPI_INTENT");
    paymentInstrument.put("targetApp", "com.phonepe.app");
    data.put("paymentInstrument", paymentInstrument);
    JSONObject deviceContext = new JSONObject();
    deviceContext.put("deviceOS", "ANDROID");
    data.put("deviceContext", deviceContext);
    String payloadBase64 = Base64.encodeToString(data.toString().getBytes(
            Charset.defaultCharset()
    ), Base64.NO_WRAP);
    String checksum = sha256(payloadBase64 + apiEndPoint + salt) + "###1";
    B2BPGRequest b2BPGRequest = new B2BPGRequestBuilder()
            .setData(payloadBase64)
            .setChecksum(checksum)
            .setUrl(apiEndPoint)
            .build();
    try {
        startActivityForResult(PhonePe.getImplicitIntent(this, b2BPGRequest, "com.phonepe.app"), 777);
    } catch (PhonePeInitException ignored) {
    }
}

这是 onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 777) {
        Toast.makeText(this, "Payment Success", Toast.LENGTH_SHORT).show();
        checkStatus();
    } else {
        Toast.makeText(this, "Failure !!!", Toast.LENGTH_SHORT).show();
    }
}

这是 sha256 函数:

private String sha256(String input) throws NoSuchAlgorithmException {
    byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] digest = md.digest(bytes);
    StringBuilder stringBuilder = new StringBuilder();
    for (byte b : digest) {
        try (Formatter formatter = new Formatter(stringBuilder)) {
            formatter.format("%02x", b);
        }
    }
    return stringBuilder.toString();
}

我在后端使用 android studio 和 java。我正在测试此代码,但它不起作用。

这是行不通的。它显示成功消息,但不显示任何内容!!

java android-studio phonepe

评论


答:

1赞 Owais Yosuf 11/17/2023 #1

在方法中,检查并相应地处理响应。PhonePe 应用程序将发送付款状态作为付款活动的结果。onActivityResultresultCode

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 777) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Payment Success", Toast.LENGTH_SHORT).show();
            checkStatus();
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Payment Cancelled", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Payment Failed", Toast.LENGTH_SHORT).show();
        }
    }
}

评论

0赞 Mr. Programmer 11/17/2023
它显示“付款已取消”。可能是什么原因?