Razorpay with Android in Kotlin
Overview
Razorpay is basically used to conduct various type of transactions.
Follow the following steps
- Create your merchant in Razorpay and download Key ID and Key Secret from the following link
- Download "checkout-1.6.15.aar" from Maven website and put it inside "libs" folder of the project
- Add the required dependency in gradle.build (Module) file
implementation "com.razorpay:checkout:1.6.12"
- Add the following code in proguard-rules.pro
-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*(...);
}- Create an Order in Server
- Initiate Payment
class MainActivity : AppCompatActivity() , PaymentResultListener
{
private var broadcastReceiver : RzpTokenReceiver? = null
companion object
{
const val TAG = "MainActivity"
}
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Checkout.preload(applicationContext)
broadcastReceiver = RzpTokenReceiver()
}
override fun onStart()
{
super.onStart()
btnPay.setOnClickListener {
payWithRazorpay()
}
}
override fun onResume()
{
super.onResume()
val intentFilter = IntentFilter()
intentFilter.addAction("rzp.device_token.share")
registerReceiver(broadcastReceiver , intentFilter)
}
private fun payWithRazorpay()
{
val checkout = Checkout().apply {
setKeyID("rzp_test_A5BBPhdwQangOg")
}
val activity : MainActivity = this
try
{
val options : JSONObject = JSONObject().apply {
put("name" , "Raj Kanchan")
put("description" , "Reference No #123456")
put(
"image" , "https://s3.amazonaws.com/rzp-mobile/images/rzp.png"
); //put("order_id" , "order_DBJOWzybf0sJbb")
put("theme.color" , "#3399cc")
put("currency" , "INR")
put("amount" , "500")
put("prefill.email" , "iamraajkanchan@gmail.com");
put("prefill.contact" , "7718881920")
val retry = JSONObject().apply {
put("enabled" , true)
put("max_count" , 3)
}
put("retry" , retry)
}
checkout.open(activity , options)
} catch (e : Exception)
{
Log.d(TAG , "payWithRazorpay :: Error : $e")
}
}
override fun onPaymentSuccess(s : String?)
{
tvNotification.text = "Successful Payment ID : $s"
}
override fun onPaymentError(i : Int , s : String?)
{
tvNotification.text = "Failed and cause is : $s"
}
}
class RzpTokenReceiver : BroadcastReceiver()
{
override fun onReceive(context : Context? , intent : Intent?)
{
if (intent?.equals("rzp.device_token.share") == true)
{
Toast.makeText(context , "Switching to Razorpay" , Toast.LENGTH_LONG).show()
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.razorpay_android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Razorpay_Android">
<receiver
android:name="com.example.razorpay_android.RzpTokenReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="rzp.device_token.share" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- Follow the first link of reference
References
- https://razorpay.com/docs/payment-gateway/android-integration/standard/
- https://github.com/razorpay/razorpay-android-sample-app/issues/248
Comments
Post a Comment