I have implemented Razor Pay payment gateway in my Android application. Everything is working fine for debit/credit card.
I am facing problem with UPI payment. Actually for UPI payment user has to visit UPI app to make the payment. Everything is working for UPI as well but the only problem is callback methods are not invoked if payment is successful or not until and unless I visit the app again.
This is big problem for me because sometime if user pay via UPI app and does not open the app again, it's been difficult for me to save the entries in the database for the payment.
I am saving entries in the database every time success callback method is invoked. How do I call success method when app is in background but not closed.
Here is the code snippet:
To open the payment activity:
Intent intent = new Intent(context, PaymentActivity.class);
intent.putExtra("orderId", order_id);
intent.putExtra("totalAmount", String.valueOf(totalPrice));
intent.putExtra("email", email);
intent.putExtra("phoneNo", phone_number);
intent.putExtra("userId", user_id);
startActivityForResult(intent, 2);
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
To call the Razor pay Activity:
final Activity activity = this;
price = Double.parseDouble(total_price);
double paisa_double = price * 100;
int paisa_int = (int) paisa_double;
final Checkout checkout = new Checkout();
checkout.setKeyID(getResources().getString(R.string.razor_pay_key));
try {
JSONObject options = new JSONObject();
options.put("name", "Razorpay Corp");
options.put("description", "Order No: " + order_id);
options.put("order_id", razor_id);
//You can omit the image option to fetch the image from dashboard
options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
options.put("currency", "INR");
options.put("amount", String.valueOf(paisa_int));
//options.put("amount", "100");
JSONObject preFill = new JSONObject();
preFill.put("email", email);
preFill.put("contact", phone_no);
options.put("prefill", preFill);
JSONObject notes = new JSONObject();
notes.put("notes", order_id);
options.put("notes", notes);
checkout.open(activity, options);
} catch (Exception e) {
Toast.makeText(activity, "Error in payment: " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
And these are the callback methods:
@Override
public void onPaymentSuccess(String razorpayPaymentID, PaymentData paymentData) {
try {
//Toast.makeText(this, "Payment Successful: " + razorpayPaymentID, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("razorpayPaymentID", razorpayPaymentID);
setResult(2, intent);
finish();
} catch (Exception e) {
Log.e(TAG, "Exception in onPaymentSuccess", e);
}
}
@Override
public void onPaymentError(int code, String response, PaymentData paymentData) {
try {
Toast.makeText(this, "Payment failed: " + code + " " + response, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e(TAG, "Exception in onPaymentError", e);
}
}
Use Razor pay web hooks in the back end code for the above problem.