Updated 31 March 2023
In this blog, we are going to learn about Razorpay payment gateway integration in android with SDK approach.
Before starting integration first, we need to know what a payment gateway is.
A payment gateway is a merchant service provided by an e-commerce application service provider. Which authorizes card or direct payment processing.
Razorpay payment gateway provides quick and secure online transactions. It provides the clean and developer-friendly API and integration.
If you are interested in Razorpay payment and want Razorpay payment gateway integration in android with SDK then this blog will help you to do so.
Razorpay payment gateway work on the below steps:
Add below dependency in your app-level build.gradle file.
1 2 3 4 5 6 7 |
repositories { mavenCentral() } dependencies { implementation 'com.razorpay:checkout:1.6.20' } |
SDK PaymentForm provides the UI interface for payment. So you have to create or initialize the view.
We are using the Razorpay Checkout
activity that will contain the UI for payment. So we have to start the Activity for result like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private fun processPayment() { val checkout = Checkout() checkout.setKeyID("YOUR_KEY_ID") try { val options = JSONObject() options.put("name", "razorpay_merchantName") options.put("order_id", "razorpay_order_id") options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png") options.put("currency", "INR") options.put("amount", 20) options.put("prefill.name", "customer_name") options.put("prefill.email", "customer_email") options.put("prefill.contact", "customer_phone_number") checkout.open(mFragmentContext.activity as CheckoutActivity, options) checkout.setImage(R.mipmap.ic_launcher) } catch (e: Exception) { (mFragmentContext.context as CheckoutActivity).mContentViewBinding.loading = false e.message?.let { ToastHelper.showToast(mFragmentContext.context!!, it) } Log.e("PaymentMehtodHandler", "Error in starting Razorpay Checkout", e) } } |
Key id is the Razorpay id collected from the dashboard.
It’s time to handle the payment callback. For that, you need to extend the PaymentResultListener interface like below:
1 |
class CheckoutActivity : PaymentResultListener {} |
Implement abstract method like below:
1 2 3 4 5 6 7 |
override fun onPaymentError(p0: Int, p1: String) { Log.d("TEST_LOG", "$p0 onPaymentError: $p1" ) } override fun onPaymentSuccess(p0: String) { Log.d("TEST_LOG", "transactionId= $p0" ) } |
For the release build add the below line in your proguard-rules.pro file
1 2 3 4 5 6 7 8 |
#RazoPay -keepclassmembers class * { @android.webkit.JavascriptInterface <methods>;} -keepattributes JavascriptInterface -keepattributes *Annotation* -dontwarn com.razorpay.** -keep class com.razorpay.** {*;} -optimizations !method/inlining/* -keepclasseswithmembers class * { public void onPayment*(...);} |
In this blog, you have learned about the implementation of the Razorpay payment gateway integration by using SDK.
For more information regarding the Razorpay payment gateway follow the link.
Thanks for reading this blog. You can also check other blogs from here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.