简体   繁体   English

Razorpay API 支付成功后如何运行代码块?

[英]How to run a block of code when we have successful payment in Razorpay API?

I am building a bus booking app and in it I am using the Razorpay API.我正在构建一个巴士预订应用程序,并在其中使用 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.现在,当支付不成功时,下面代码中的意图也在运行。现在,如果我点击完成按钮并返回 go,就会调用意图。 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();
    }
}

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我通过在 onPaymentSucess 方法中编写该代码块并在 oncreate 方法之外声明busRVModel 并在 onCreate 方法中初始化busRVModel 解决了上述问题

 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.注意:在我编写上述代码块的情况下,我得到了 busRVModel 不可用的错误,因此请务必在 oncreate 方法之外声明 busRVModel object 并在 oncreate 方法中对其进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM