Today we are going to see one Amazing thing that every developer needs in their Applications.
Let's See How to Integrate RazorPay Payment Gateway on Android Application
Step 1 : Add Dependency (app gradle).
implementation 'com.razorpay:checkout:1.6.12'
Step 2 :Create your Layout.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Payment Gateway Demo"
android:textSize="20dp"
android:textAlignment="center"
android:minHeight="120dp"
android:layout_gravity="center"
android:gravity="center"
/>
<EditText
android:layout_margin="6dp"
android:id="@+id/edit_amount"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Amount" />
<Button
android:layout_marginTop="40dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pay Now !!"
android:id="@+id/btn_pay"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="14sp"
android:layout_margin="8dp"
android:id="@+id/paymentStatus"
android:text="Payment Status"/>
</LinearLayout>
Step 3 : Write the method for RazorPay.
class MainActivity : AppCompatActivity(), PaymentResultListener {
lateinit var txtPaymentStatus: TextView
lateinit var editAmount: EditText
lateinit var btnPayNow: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
txtPaymentStatus = findViewById(R.id.paymentStatus)
editAmount = findViewById(R.id.edit_amount)
btnPayNow = findViewById(R.id.btn_pay)
btnPayNow.setOnClickListener {
savePayments(editAmount.text.toString().trim().toInt())
}
Checkout.preload(this@MainActivity)
}
private fun savePayments(amount: Int) {
val checkout = Checkout()
checkout.setKeyID("rzp_test_lyVg4EbhAATCjE")
try {
val options = JSONObject()
options.put("name", "Razorpay Demo")
options.put("description", "If You Like these Tutorials. Buy me a Coffee")
//options.put("image","")
options.put("theme.color", "#3399cc")
options.put("currency", "INR")
options.put("amount", amount * 100)
val retryObj = JSONObject()
retryObj.put("enabled", true)
retryObj.put("max_count", 4)
options.put("retry", retryObj)
checkout.open(this@MainActivity, options)
} catch (e: Exception) {
Toast.makeText(this@MainActivity, "Error in Payment : " + e.message, Toast.LENGTH_LONG)
.show()
e.printStackTrace()
}
}
override fun onPaymentSuccess(p0: String?) {
txtPaymentStatus.text = p0
txtPaymentStatus.setTextColor(Color.GREEN)
}
override fun onPaymentError(p0: Int, p1: String?) {
txtPaymentStatus.text = p1
txtPaymentStatus.setTextColor(Color.RED)
}
}
Test Cards
Card Network | Domestic / International | Card Number |
---|---|---|
Mastercard | Domestic | 5267 3181 8797 5449 |
Visa | Domestic | 4111 1111 1111 1111 |
Mastercard | International | 5555 5555 5555 4444 |
Visa | International | 4012 8888 8888 1881 |
Thank You !!
0 Comments