Razorpay Error - {"code":"BAD_REQUEST_ERROR","description":"ay_order_id is not a valid id"}

Shivam Kumar picture Shivam Kumar · Mar 13, 2019 · Viewed 9.1k times · Source

I'm implement Razorpay with PaymentResultWithDataListener. Actually i need order_id and signature so i use PaymentResultWithDataListener not used PaymentResultListener because there are no option to get order_id and signature. And I have follow these links

https://docs.razorpay.com/v1/page/orders#verifying-the-signature

https://razorpay.com/mobile/

https://github.com/razorpay/razorpay-android-sample-app

But not getting any solution.

Menifest File

<meta-data
    android:name="com.razorpay.ApiKey"
    android:value="rzp_test_PLbERPkkqGZkOF" />

build.gradle

api 'com.razorpay:checkout:1.5.4'

I got an error

{"code":"BAD_REQUEST_ERROR","description":"ay_order_id is not a valid id"}

I am trying with this code

public class CheckoutActivity extends AppCompatActivity implements View.OnClickListener, PaymentResultWithDataListener {
    private static final String TAG = CheckoutActivity.class.getSimpleName();

    Button mCheckOutView;

    String OrderId = "";
    String signature = "";
    String order_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment_method);

        Checkout.preload(getApplicationContext());

        mCheckOutView = findViewById(R.id.check_out);

        mCheckOutView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == mCheckOutView) {
            startPayment();
        }
    }

    public void startPayment() {
        /*
          You need to pass current activity in order to let Razorpay create CheckoutActivity
         */
        final Activity activity = this;

        final Checkout co = new Checkout();

        try {
            JSONObject options = new JSONObject();
            options.put("name","Test");
            options.put("description", getString(R.string.app_name));
            options.put("key", getString(R.string.api_key));
            options.put("order_id","razorpay_order_id");
            options.put("signature","razorpay_signature");

            options.put("currency", "INR");
            options.put("amount", 100);

            JSONObject preFill = new JSONObject();
            preFill.put("email", "[email protected]");
            preFill.put("contact", "9999999999");

            options.put("prefill", preFill);

            JSONObject notesData=new JSONObject();
            notesData.put("Order Id","order123");
            notesData.put("address","Test Address");

            options.put("notes", notesData);

            JSONObject theme=new JSONObject();
            theme.put("color","#738598");
            theme.put("emi_mode",true);

            options.put("theme", theme);

            co.open(activity, options);
        } catch (Exception e) {
            Toast.makeText(activity, "Error in payment: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    @Override
    public void onPaymentSuccess(String s, PaymentData paymentData) {
        String paymentId = paymentData.getPaymentId();
        String signature = paymentData.getSignature();  // got null
        String orderId = paymentData.getOrderId();      // got null
    }

    @Override
    public void onPaymentError(int i, String s, PaymentData paymentData) {
        Log.e(TAG,s);  //error {"code":"BAD_REQUEST_ERROR","description":"ay_order_id is not a valid id"}
    }
}

If i remove these 2 lines then this error not comes.

options.put("order_id","razorpay_order_id");
options.put("signature","razorpay_signature");

But paymentData.getSignature() and paymentData.getOrderId() is null.

Any help will be appreciated.

Answer

Pradeep Dhawan picture Pradeep Dhawan · Nov 17, 2019
options.put("order_id","**razorpay_order_id**");

you need to generate this order_id from razorpay Order_ID API, only after this order_id which comes as a response from the API you would be able to process smoothly. After getting this Order_id from API, send it in above code in value (in place of razorpay_order_id)

options.put("signature","razorpay_signature");

This is not required in request. This will be generated at your server and will be used when a response is received in PaymentResultWithDataListener function. Read Signature generation method in official Documentation here : https://razorpay.com/docs/payment-gateway/android-integration/standard/