PhonePe Payment Gateway Not Working Inside Java Android

384 views Asked by At

I am implementing PhonePe Payment Gateway inside my application, but its displays toast Payment Cancelled always whenever I run my application. I am using Android Studio with Java as backend. Here is my code:

private void payUsingPhonePe() throws PhonePeInitException {
    String MERCHANT_ID = "MERCHANTUAT";
    String salt = "a6334ff7-da0e-4d51-a9ce-76b97d518b1e";
    String apiEndPoint = "/pg/v1/pay";
    PhonePe.init(Checkout.this, PhonePeEnvironment.RELEASE, MERCHANT_ID, "com.igcshop.igc");
    HashMap<String, Object> data = new HashMap<>();
    data.put("merchantId", MERCHANT_ID);
    data.put("merchantTransactionId", orderId);
    data.put("merchantUserId", custID);
    data.put("amount", total * 100);
    data.put("mobileNumber", phone);
    data.put("callbackUrl", "https://webhook.site/callback-url");
    HashMap<String, Object> paymentInstrument = new HashMap<>();
    paymentInstrument.put("type", "UPI_INTENT");
    paymentInstrument.put("targetApp", "com.phonepe.app");
    data.put("paymentInstrument", paymentInstrument);
    HashMap<String, Object> deviceContext = new HashMap<>();
    deviceContext.put("deviceOS", "ANDROID");
    data.put("deviceContext", deviceContext);
    String payloadBase64 = convertDataToBase64(data);
    String checksum = generateChecksum(payloadBase64, apiEndPoint, salt);
    B2BPGRequest b2BPGRequest = new B2BPGRequestBuilder()
            .setData(Objects.requireNonNull(payloadBase64))
            .setChecksum(Objects.requireNonNull(checksum))
            .setUrl(apiEndPoint)
            .build();
    List<UPIApplicationInfo> upiApps = PhonePe.getUpiApps();
    startActivityForResult(PhonePe.getImplicitIntent(this, b2BPGRequest, upiApps.toString()), 777);
}

public static String convertDataToBase64(HashMap<String, Object> data) {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(data);
        byte[] base64Bytes = Base64.encode(jsonString.getBytes(), Base64.DEFAULT);
        return new String(base64Bytes);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public static String generateChecksum(String payloadBase64, String apiEndPoint, String salt) {
    try {
        String dataToHash = payloadBase64 + apiEndPoint + salt;
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(dataToHash.getBytes());
        byte[] byteData = md.digest();
        StringBuilder checksumBuilder = new StringBuilder();
        for (byte b : byteData) {
            checksumBuilder.append(String.format("%02x", b));
        }
        return checksumBuilder + "###1";
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    String result = null;
    if (data != null) {
        result = data.getStringExtra("key_txn_result");
        if (data.getExtras() != null && data.getExtras().keySet().size() > 0) {
            Log.i(TAG, "result has extras");
            for (String key : data.getExtras().keySet()) {
                Log.i(TAG, key + " = " + data.getExtras().get(key));
            }
        } else {
            Log.i(TAG, "result has 0 extras");
        }
    }
    if (resultCode != Activity.RESULT_CANCELED) {
        Toast.makeText(this, "Result: " + result, Toast.LENGTH_SHORT).show();
    } else {
        if (requestCode == 777 && data != null && data.getExtras().containsKey("key_error_result"))
            result = data.getStringExtra("key_error_result");
        Toast.makeText(this, "Failed: " + result, Toast.LENGTH_SHORT).show();
    }
}

Please look the above code and let me know what could be possible reason of getting such error.

0

There are 0 answers