Home > Back-end >  How to run a block of code when we have successful payment in Razorpay API?
How to run a block of code when we have successful payment in Razorpay API?

Time:07-05

I am building a bus booking app and in it I am using the Razorpay API. Right now the intent in below code is being run when the payment is not successful as well.Right now if I just click the done button and go back, the intent is called. I only want to call the intent when the payment is successful. How do I run the intent only when the payment is successfully complete?

        buttonDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SeatAL.size()==24) {
                    Toast.makeText(Book_Bus.this, "Select a minimum of 1 seat to pay", Toast.LENGTH_SHORT).show();
                } else{

                    Toast.makeText(Book_Bus.this, "Redirecting...", Toast.LENGTH_SHORT).show();
                    int amount=count*Integer.valueOf(price)*100;
                    count=0;
                    RazorPayAPI(amount);

                    String FinalSeats="";
                    for(int i=0;i<SeatsAL.size();i  ){
                        FinalSeats =SeatsAL.get(i);
                        if(i!=SeatsAL.size()-1){
                            FinalSeats =",";
                        }
                    }
                    if(flag) {
                        Intent intent = new Intent(Book_Bus.this, Ticket.class);
                        intent.putExtra("BusID", busRVModel.getBusID());
                        intent.putExtra("Source", busRVModel.getSrc());
                        intent.putExtra("Destination", busRVModel.getDest());
                        intent.putExtra("Timings", busRVModel.getTimings());
                        intent.putExtra("Seats", FinalSeats);
                        startActivity(intent);
                        finish();
                    }else{
                        Toast.makeText(Book_Bus.this, "Try again", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            private void RazorPayAPI(int amount) {
                Checkout checkout=new Checkout();
                checkout.setKeyID("<keyID>");
                JSONObject object=new JSONObject();
                try {
                    object.put("name","<name>");
                    object.put("Description","Bus Ticket");
                    object.put("theme.color",bg_yellow);
                    object.put("currency","INR");
                    object.put("amount",amount);
                    object.put("prefill.contact","<phone>");
                    object.put("prefill.email","<email>");
                    checkout.open(Book_Bus.this,object);
                    flag=true;
                } catch (JSONException e) {
                    e.printStackTrace();
                    flag=false;
                }
            }
        });
    }

    @Override
    public void onPaymentSuccess(String s) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Payment ID");
        Toast.makeText(this, "Payment Successful", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPaymentError(int i, String s) {
        Toast.makeText(getApplicationContext(), "Payment Cancelled", Toast.LENGTH_SHORT).show();
    }
}

CodePudding user response:

I solved the above problem by writing that block of code in the onPaymentSucess method and declaring the busRVModel outside the oncreate method and initialising the busRVModel inside the onCreate method

 buttonDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SeatAL.size()==24) {
                    Toast.makeText(Book_Bus.this, "Select a minimum of 1 seat to pay", Toast.LENGTH_SHORT).show();
                } else{

                    Toast.makeText(Book_Bus.this, "Redirecting...", Toast.LENGTH_SHORT).show();
                    int amount=count*Integer.valueOf(price)*100;
                    count=0;
                    razorPayAPI(amount);
                }
            }

            private void razorPayAPI(int amount) {
                Checkout checkout=new Checkout();
                checkout.setKeyID("<keyID>");
                JSONObject object=new JSONObject();
                try {
                    object.put("name","<name>");
                    object.put("Description","Bus Ticket");
                    object.put("theme.color",bg_yellow);
                    object.put("currency","INR");
                    object.put("amount",amount);
                    object.put("prefill.contact","<phone>");
                    object.put("prefill.email","<email>");
                    checkout.open(Book_Bus.this,object);
                    flag=true;
                } catch (JSONException e) {
                    e.printStackTrace();
                    flag=false;
                }
            }
        });
    }

    @Override
    public void onPaymentSuccess(String s) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Payment ID");
        Toast.makeText(this, "Payment Successful", Toast.LENGTH_SHORT).show();
        String FinalSeats="";
                    for(int i=0;i<SeatsAL.size();i  ){
                        FinalSeats =SeatsAL.get(i);
                        if(i!=SeatsAL.size()-1){
                            FinalSeats =",";
                        }
                    }
        Intent intent = new Intent(Book_Bus.this, Ticket.class);
                        intent.putExtra("BusID", busRVModel.getBusID());
                        intent.putExtra("Source", busRVModel.getSrc());
                        intent.putExtra("Destination", busRVModel.getDest());
                        intent.putExtra("Timings", busRVModel.getTimings());
                        intent.putExtra("Seats", FinalSeats);
                        startActivity(intent);
                        finish();
    }

    @Override
    public void onPaymentError(int i, String s) {
        Toast.makeText(getApplicationContext(), "Payment Cancelled", Toast.LENGTH_SHORT).show();
    }
}

NOTE: In my case when I wrote the above block of code I got the error that busRVModel is not available, so be sure to declare the busRVModel object outside the oncreate method and initialise it inside the oncreate method.

  • Related