Search code examples
javaandroidjsonrazorpay

In Razorpay onPaymentError method Intent not working


I have implemented the Razorpay payment gateway in one of my project. I am using the test key. When payment gets success, onPaymentSuccess method called successfully and went to the next activity as mentioned in onPaymentSuccess method.

But when payment gets failed, onPaymentError called and Toast message is showing but not going to the next activity. Please help.

Checkout checkout = new Checkout();
checkout.setKeyID("rzp_test_key");
checkout.setImage(R.drawable.ic_logo_short);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", name);
jsonObject.put("description", description);
jsonObject.put("order_id", order_id);
jsonObject.put("currency", "INR");
jsonObject.put("amount", finalAmount);
jsonObject.put("prefill.contact", mobile);
jsonObject.put("prefill.email", email);

JSONObject retryObj = new JSONObject();
retryObj.put("enabled", false);
jsonObject.put("retry", retryObj);

checkout.open(PaymentActivity.this, jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}

@Override
    public void onPaymentSuccess(String s, PaymentData paymentData) {

        try {
            Intent intent = new Intent(PaymentActivity.this, PaymentStatusActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("payment_status", paymentData.getData().toString());
            startActivity(intent);
            finish();

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

    @Override
    public void onPaymentError(int i, String s, PaymentData paymentData) {

        try {
            Utility.showToastMessage(this, s);
            Intent intent = new Intent(PaymentActivity.this, PaymentStatusActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("payment_status", paymentData.getData().toString());
            startActivity(intent);
            finish();

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

    }



Solution

  • Remove this line: intent.putExtra("payment_status", paymentData.getData().toString()); from onPaymentError method works because there is no data in paymentData when payment gets failed.