Skip to main content

Razorpay Payment Gateway Integration with Flutter and Dart

Integrating a payment gateway is crucial for any Flutter application that involves financial transactions. Razorpay is a popular choice in India, offering a robust and developer-friendly platform. This blog post will guide you through the process of integrating Razorpay into your Flutter application using Dart, ensuring a seamless and secure payment experience for your users.

Setting Up Your Razorpay Account and Obtaining API Keys

Before diving into the code, you'll need a Razorpay account. Head over to the Razorpay website and create an account. Once your account is set up and activated, navigate to the API Keys section in the Razorpay dashboard. Generate API keys; you'll receive a Key ID and a Key Secret. Treat these keys with utmost care, as they grant access to your Razorpay account. Store them securely and avoid committing them directly to your code repository. Consider using environment variables or secure storage mechanisms.

Important Note: While in development, use the test keys provided by Razorpay to avoid real transactions. Switch to live keys only when your application is ready for production.

Installing the Razorpay Flutter Plugin

Flutter provides a convenient plugin for integrating Razorpay. To add the plugin to your Flutter project, open your pubspec.yaml file and add the following dependency:

dependencies:
  razorpay_flutter: ^1.3.6 # Use the latest version

Replace ^1.3.6 with the latest version available on pub.dev. After adding the dependency, run flutter pub get in your terminal to download and install the plugin.

Implementing the Payment Flow in Your Flutter App

Now, let's implement the payment flow in your Flutter application. First, import the Razorpay plugin into your Dart file:

import 'package:razorpay_flutter/razorpay_flutter.dart';

Next, initialize the Razorpay instance:

Razorpay _razorpay = Razorpay();

@override
void initState() {
  super.initState();
  _razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
  _razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, _handlePaymentError);
  _razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, _handleExternalWallet);
}

Implement the handler functions:


void _handlePaymentSuccess(PaymentSuccessResponse response) {
    // Payment successful
    print("Payment Successful: ${response.paymentId}");
}

void _handlePaymentError(PaymentFailureResponse response) {
    // Payment failed
    print("Payment Error: ${response.code} - ${response.message}");
}

void _handleExternalWallet(ExternalWalletResponse response) {
    // External wallet selected
    print("External Wallet: ${response.walletName}");
}

@override
void dispose() {
  super.dispose();
  _razorpay.clear();
}

Finally, create the options object and open the Razorpay checkout when the user initiates the payment:

var options = {
  'key': 'YOUR_KEY_ID', // Replace with your Key ID
  'amount': 100 * 100, // Amount in paise (e.g., 100 INR = 10000 paise)
  'name': 'Your Company Name',
  'description': 'Payment for your product/service',
  'prefill': {
    'name': 'Customer Name',
    'email': 'customer@example.com',
    'contact': '9876543210'
  },
  'theme': {
    'color': '#F37254'
  }
};

try {
  _razorpay.open(options);
} catch (e) {
  debugPrint('Error: $e');
}

Remember to replace YOUR_KEY_ID with your actual Key ID and adjust the other parameters (amount, name, description, etc.) accordingly.

Handling Payment Success and Failure

The _handlePaymentSuccess and _handlePaymentError functions are crucial for handling the different outcomes of the payment process. In _handlePaymentSuccess, you can verify the payment status with Razorpay's servers and update your application's state accordingly (e.g., mark the order as paid). In _handlePaymentError, you should display an appropriate error message to the user and allow them to retry the payment.

  • Important: Always verify the payment status on your server-side to prevent fraudulent transactions.
  • Consider implementing retries and offering alternative payment methods in case of failure.

Conclusion

Integrating Razorpay into your Flutter application opens up a world of possibilities for accepting payments seamlessly. By following the steps outlined in this guide, you can create a secure and user-friendly payment experience. Remember to prioritize security, handle errors gracefully, and always verify payment status on your server-side. Happy coding!

Key Takeaways:

  • Securely store and manage your Razorpay API keys.
  • Utilize the official Razorpay Flutter plugin for easy integration.
  • Implement robust error handling and payment verification.

Automated post via TechCognita Automation Framework

Comments

Popular posts from this blog

Stripe vs Razorpay: Which Is Better for Indian Devs?

In the booming Indian startup and freelancer economy , online payments are the fuel that keeps projects running. Two names dominate this space for developers building SaaS products , client dashboards , or mobile apps: Stripe and Razorpay . But which one is better for Indian developers in 2025? Let’s break it down based on features, ease of use, integration, pricing, and local support. 💳 1. Onboarding & KYC Stripe: Offers international-level onboarding. But Stripe India requires you to be a registered business (no individual freelancers allowed). Razorpay: Allows both individuals and companies to sign up. Faster KYC for Indian users. 🏆 Winner: Razorpay (more accessible for freelancers & students) 🧑‍💻 2. Developer Experience Stripe: World-class documentation, SDKs for every language ( Node.js , Python , Flutter , etc.), sandbox testing, CLI tools . Razorpay: Good documentation, JS SDK , mobile SDKs available, but slightly less matu...

Google Summer of Code (GSoC) 2026 – Your Complete Guide to Getting Started

🎯 Introduction If you’re a coder with ambition—whether you’re a student, a self-taught developer, or early in your career—then the Google Summer of Code (GSoC) offers a powerful launchpad. Since its inception in 2005, GSoC has enabled thousands of developers from around the globe to contribute to open-source software , work with real mentors, and build impressive portfolio projects. As we look ahead to GSoC 2026 , there’s no better time to prepare deliberately—with strategy, clarity, and precision. This blog will give you the full scope: what GSoC is, why you should participate, how to position yourself for success, and actionable steps to get ahead. 💡 What Is GSoC? At its core, GSoC is a global, remote program where open-source organizations partner with contributors to complete meaningful projects during the summer. Key highlights: You’ll collaborate with open-source organizations and real mentors. You’ll work on live codebases impacting real users. The entire prog...

Razorpay vs PayU vs Cashfree: A Payment Gateway Integration Showdown

Choosing the right payment gateway is crucial for any business operating online. It's the bridge between your customers and your bank account, so a smooth, secure, and reliable integration is paramount. In India, Razorpay , PayU , and Cashfree are three of the most popular options, each offering a suite of features and benefits. This article dives deep into comparing these three giants to help you decide which one best suits your specific business needs. Understanding Key Features and Pricing Before diving into a head-to-head comparison, let's outline some of the core features each payment gateway offers and their general pricing structures. Razorpay is known for its developer-friendly APIs and a wide range of integrations. PayU boasts a strong focus on security and fraud prevention , while Cashfree is often praised for its efficient payouts and bulk payment options . Pricing varies, but generally includes transaction fees, setup fees (sometimes waived), and pos...