1

Hello I have implemented Razorpay payment gateway but when I click the pay button to open the payment gateway screen there is an error

I have 6 card views which shows the different amount and when the user select the card view and press the donate button it should bring up the payment gateway screen with the selected amount but this is the error im getting

Webview JS Error: Uncaught ReferenceError: otpPermissionCallback is not defined
I/chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: otpPermissionCallback is not defined", source: https://api.razorpay.com/v1/checkout/public?version=1.6.12&library=checkoutjs&platform=android (1)
E/ActivityThread: Activity com.razorpay.CheckoutActivity has leaked IntentReceiver com.razorpay.AutoReadOtpHelper@385afd1 that was originally registered here. Are you missing a call to unregisterReceiver()?

here is the doc for implementing Razorpay Doc

My code

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
            View view = inflater.inflate(R.layout.donate_fragment, container, false);
            // razorpay
            Checkout.preload(requireContext().getApplicationContext());

// Donate Btn
        donateBtn = view.findViewById(R.id.donateBtn);
        donateBtn.setOnClickListener(view12 -> {
            if (cardView1.isChecked()) {
                makePayment1();
            }
            if (cardView2.isChecked()) {
                makePayment2();
            }
            if (cardView3.isChecked()) {
                makePayment3();
            }
            if (cardView4.isChecked()) {
                makePayment4();
            }
            if (cardView5.isChecked()) {
                makePayment5();
            }
            if (cardView6.isChecked()) {
                makePayment6();
            }
        });

}

private void makePayment1() {
        checkout.setKeyID("<didnt included here>");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "didnt included here");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "50"); // Amount x 100
            options.put("prefill.email", "didnt included here");
            options.put("prefill.contact", "didnt included here");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    } // and same for other makePayment 2,3,4 ....

I think the error is probably related to this line

 checkout.open((Activity) requireContext(), options);

the method is in a fragment not in activity probably that's why this is giving an error, not sure

Entire Code

public class Donate extends Fragment implements PaymentResultListener {
    LinearLayout linearLayout1, linearLayout2;
    MaterialCardView cardView1, cardView2, cardView3, cardView4, cardView5, cardView6;
    MaterialButton donateBtn;
    Checkout checkout = new Checkout();

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.donate_fragment, container, false);
        // razorpay
        Checkout.preload(requireContext().getApplicationContext());
        //LinearLayout
        linearLayout1 = view.findViewById(R.id.linearLayout1);
        linearLayout2 = view.findViewById(R.id.linearLayout2);

        //CardView
        cardView1 = view.findViewById(R.id.cardView1);
        cardView2 = view.findViewById(R.id.cardView2);
        cardView3 = view.findViewById(R.id.cardView3);
        cardView4 = view.findViewById(R.id.cardView4);
        cardView5 = view.findViewById(R.id.cardView5);
        cardView6 = view.findViewById(R.id.cardView6);

        // Donate Btn
        donateBtn = view.findViewById(R.id.donateBtn);
        donateBtn.setOnClickListener(view12 -> {
            if (cardView1.isChecked()) {
                makePayment1();
            }
            if (cardView2.isChecked()) {
                makePayment2();
            }
            if (cardView3.isChecked()) {
                makePayment3();
            }
            if (cardView4.isChecked()) {
                makePayment4();
            }
            if (cardView5.isChecked()) {
                makePayment5();
            }
            if (cardView6.isChecked()) {
                makePayment6();
            }
        });


        View.OnClickListener listener1 = view1 -> {
            if (view1 instanceof MaterialCardView) {
                MaterialCardView card = (MaterialCardView) view1;
                boolean isChecked = card.isChecked();
                clearCardSelection(linearLayout1);
                clearCardSelection(linearLayout2);
                card.setChecked(!isChecked);

            }
        };
        View.OnClickListener listener2 = view2 -> {
            if (view2 instanceof MaterialCardView) {
                MaterialCardView card = (MaterialCardView) view2;
                boolean isChecked = card.isChecked();
                clearCardSelection(linearLayout1);
                clearCardSelection(linearLayout2);
                card.setChecked(!isChecked);

            }
        };
        View.OnClickListener listener3 = view3 -> {
            if (view3 instanceof MaterialCardView) {
                MaterialCardView card = (MaterialCardView) view3;
                boolean isChecked = card.isChecked();
                clearCardSelection(linearLayout1);
                clearCardSelection(linearLayout2);
                card.setChecked(!isChecked);

            }
        };
        View.OnClickListener listener4 = view4 -> {
            if (view4 instanceof MaterialCardView) {
                MaterialCardView card = (MaterialCardView) view4;
                boolean isChecked = card.isChecked();
                clearCardSelection(linearLayout1);
                clearCardSelection(linearLayout2);
                card.setChecked(!isChecked);

            }
        };
        View.OnClickListener listener5 = view5 -> {
            if (view5 instanceof MaterialCardView) {
                MaterialCardView card = (MaterialCardView) view5;
                boolean isChecked = card.isChecked();
                clearCardSelection(linearLayout1);
                clearCardSelection(linearLayout2);
                card.setChecked(!isChecked);

            }
        };
        View.OnClickListener listener6 = view6 -> {
            if (view6 instanceof MaterialCardView) {
                MaterialCardView card = (MaterialCardView) view6;
                boolean isChecked = card.isChecked();
                clearCardSelection(linearLayout1);
                clearCardSelection(linearLayout2);
                card.setChecked(!isChecked);

            }
        };
        cardView1.setOnClickListener(listener1);
        cardView2.setOnClickListener(listener2);
        cardView3.setOnClickListener(listener3);
        cardView4.setOnClickListener(listener4);
        cardView5.setOnClickListener(listener5);
        cardView6.setOnClickListener(listener6);


        return view;
    }

    private void makePayment1() {
        checkout.setKeyID("< -- >");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "< -- >");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
//            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "50"); // Amount x 100
            options.put("prefill.email", "< -- >");
            options.put("prefill.contact", "< -- >");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    } // and same for other makePayment 2,3,4 ....

    private void makePayment2() {
        Checkout checkout = new Checkout();
        checkout.setKeyID("< -- >");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "< -- >");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
//            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "100"); // Amount x 100
            options.put("prefill.email", "< -- >");
            options.put("prefill.contact", "< -- >");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    }

    private void makePayment3() {
        Checkout checkout = new Checkout();
        checkout.setKeyID("< -- >");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "< -- >");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
//            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "500"); // Amount x 100
            options.put("prefill.email", "< -- >");
            options.put("prefill.contact", "< -- >");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    }

    private void makePayment4() {
        Checkout checkout = new Checkout();
        checkout.setKeyID("< -- >");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "< -- >");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
//            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "1000"); // Amount x 100
            options.put("prefill.email", "< -- >");
            options.put("prefill.contact", "< -- >");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    }

    private void makePayment5() {
        Checkout checkout = new Checkout();
        checkout.setKeyID("< -- >");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "< -- >");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
//            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "5000"); // Amount x 100
            options.put("prefill.email", "< -- >");
            options.put("prefill.contact", "< -- >");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    }

    private void makePayment6() {
        Checkout checkout = new Checkout();
        checkout.setKeyID("< -- >");
        checkout.setImage(R.drawable.ic_baseline_favorite_24);
        try {
            JSONObject options = new JSONObject();

            options.put("name", "< -- >");
            options.put("description", "Reference No. #123456");
            options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
//            options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
            options.put("theme.color", "#3399cc");
            options.put("currency", "USD");
            options.put("amount", "10000"); // Amount x 100
            options.put("prefill.email", "< -- >");
            options.put("prefill.contact", "< -- >");
            JSONObject retryObj = new JSONObject();
            retryObj.put("enabled", true);
            retryObj.put("max_count", 4);
            options.put("retry", retryObj);

            checkout.open((Activity) requireContext(), options);

        } catch (Exception e) {
            Log.e("TAG", "Error in starting Razorpay Checkout", e);
        }
    }

    private void clearCardSelection(LinearLayout layout) {
        for (int i = 0; i < layout.getChildCount(); i++) {
            View child = layout.getChildAt(i);
            if (child instanceof MaterialCardView)
                ((MaterialCardView) child).setChecked(false);
        }
    }

    @Override
    public void onPaymentSuccess(String s) {
        Toast.makeText(getContext(), "Thank You So Much For Your Donation " + " Id is " + s, Toast.LENGTH_LONG).show();


    }

    @Override
    public void onPaymentError(int i, String s) {
        Toast.makeText(getContext(), "Payment Failed " + s, Toast.LENGTH_LONG).show();

    }
}
Vasant Raval
  • 257
  • 1
  • 12
  • 31

2 Answers2

0

It is mostly because of the amount

options.put("amount", "50"); // Amount x 100

Amount should be multiplier in 100 so at least try with 5000 or 1000 It will help.

Also your order Id should be generated from server and should be unique if you are not using server and just testing ir for now you can try removing line

options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
R7G
  • 1,000
  • 1
  • 10
  • 15
  • but I have an amount of 0.5 $ how will i do that – Vasant Raval Feb 20 '22 at 13:42
  • take a look at this amounts i want to implement https://drive.google.com/file/d/1uLo6iRVisDVQ28Z0noWzpcBJ1f1XBAsy/view?usp=sharing – Vasant Raval Feb 20 '22 at 13:43
  • how can I generate an order id, how can I do that from firebase, I didn't find anything related to it on the Doc's – Vasant Raval Feb 20 '22 at 13:49
  • Order Id should be generated from server not from firebase , you can try commenting it for now or ask your backend team to implement it. – R7G Feb 20 '22 at 13:54
  • also I don't know that razorpay can allow 0.5$ payment there is not mentioned anything like this in there document , but you can ask with them by raising a ticket . https://razorpay.com/docs/payments/dashboard/support/ – R7G Feb 20 '22 at 13:59
  • even if i removed the orderID for test purpose and i selected the 100$ (100 x 100 = 10000) and clicked the pay(donate btn) the issue is still there,ok one thing i forgot to mention in the question is that there is a toast message " Error : onPaymentError probably not implemented in your activity" when i click the pay btn – Vasant Raval Feb 20 '22 at 14:08
  • I don't have a backend team it's just a simple audio playing app, and I don't understand do I need to give an order id? and if yes why payment gateways tell us to do that with the servers if they are processing all the payments why they don't give an orderID – Vasant Raval Feb 20 '22 at 14:13
  • You need to implements PaymentResultListener on your activity class to handle the error or sucess . And for order ID it is general procedure to have your own server for handling this since this data is belong to seller so they must have some place to store it. – R7G Feb 20 '22 at 17:12
  • but i want to implement the payment in fragment not in activity , how can i do that, i have shared the entire code of the fragment – Vasant Raval Feb 20 '22 at 18:05
  • can you explain it how can i implement it in code – Vasant Raval Feb 23 '22 at 06:32
-1

I was trying to handle the PaymentFailureResponse and not getting PopUp for payment cancelled. Steps

  1. When payment gateway page open
  2. Try to cancel by clicking the close button and I have handled like this as per my required functionality response. Error?["metadata"]["payment_id"] but when I debugged my code I found null check is missing for payment_id so I got this error. and it is specifically occurred for iOS only so please replace

response. Error?["metadata"]["payment_id"] to response. Error?["metadata"]?["payment_id"]

Akshay
  • 1
  • 1