Razorpay integration android — kotlin
Hi, In this article will see how to integrate razorpay integration with simple steps.
- SignUp razorpay — no need to provide kyc for testing(just signup with basic details)
- In razonpay dashboard — settings where you can generate api key and save it in to your local machine.
- Go to Android Studio, Create project
- Add one button in the layout, when clicking on button we will add the razor pay code.
- add the razorpay sdk like below
implementation 'com.razorpay:checkout:1.6.12'
6. In the manifest.xml file add these two things like below
<meta-data
android:name="com.razorpay.ApiKey"
android:value="rzp_test_aaaaaaaa" />
<meta-data
android:name="com.google.gms.wallet.api.enabled"
android:value="true" />
copy rzp_test from razorpay site.
add internet permission in manifest like below.
<uses-permission android:name="android.permission.INTERNET"/>
7. Now Climax — Open the activity in the button click write the code like below.
private fun startPayment() {
/*
* You need to pass current activity in order to let Razorpay create CheckoutActivity
* */
val activity:Activity = this
val co = Checkout()
try {
val options = JSONObject()
options.put("name","Test Corp")
options.put("description","test Charges")
//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("theme.color", "#3399cc");
options.put("currency","INR");
options.put("amount","12300 ")//pass amount in currency subunits
/* val retryObj = JSONObject()
retryObj.put("enabled", true);
retryObj.put("max_count", 4);
options.put("retry", retryObj);*/
val prefill = JSONObject()
prefill.put("email","abcxyz@example.com")
prefill.put("contact","99xxxxxxxxx")
options.put("prefill",prefill)
co.open(activity,options)
}catch (e: Exception){
Toast.makeText(activity,"Error in payment: "+ e.message,Toast.LENGTH_LONG).show()
e.printStackTrace()
}
}
Here, add your mobile to get the otp.
8. Last step, add listeners to know payment is success or fail, for that add the listeners like below.
class MainActivity : AppCompatActivity(),PaymentResultListener{override fun onPaymentSuccess(p0: String?) {
Toast.makeText(this,"Success : ",Toast.LENGTH_LONG).show() }
override fun onPaymentError(p0: Int, p1: String?) {
Toast.makeText(this,"Failed : ",Toast.LENGTH_LONG).show() }}
That’s all, run the app and choose any payment and click on success. After successful go to the razorpay dashboard -transactions where you can see your transaction.
Thanks for reading this article, I hope you like this article, if you like please clap for me.