Implementing a UPI (Unified Payments Interface) payment system in a Flutter app

Implementing a UPI (Unified Payments Interface) payment system in a Flutter app involves using a UPI payment package and integrating it into your Flutter project. Below is a simple example using the flutter_upi package. This package allows you to integrate UPI payments into your Flutter app.

Steps to Implement UPI Payment in Flutter:

1. Add Dependency:

Add the flutter_upi package to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_upi: ^2.0.0

Run flutter pub get to install the package.

2. Import the Package:

In your Dart file, import the necessary package:

import 'package:flutter/material.dart';
import 'package:flutter_upi/flutter_upi.dart';

3. Create a UPI Transaction:

class UpiPaymentApp extends StatelessWidget {
  Future<void> initiateTransaction() async {
    final List<ApplicationMeta> upiApps = await FlutterUpi.getAllInstalledUpiApps();

    // Choose a UPI app (e.g., use the first available app)
    final app = upiApps.first;

    // Set transaction details
    final UpiTransactionInfo transactionInfo = UpiTransactionInfo(
      uri: 'upi://pay',
      amount: '1.00',
      currency: 'INR',
      receiverName: 'Recipient Name',
      receiverUpiAddress: 'recipient@upi', // Replace with the recipient's UPI address
      transactionRefId: '123456789',
      transactionNote: 'Payment for the product',
    );

    try {
      final String response = await FlutterUpi.startTransaction(
        app: app, 
        receiverUpiAddress: transactionInfo.receiverUpiAddress,
        receiverName: transactionInfo.receiverName,
        transactionRefId: transactionInfo.transactionRefId,
        transactionNote: transactionInfo.transactionNote,
        amount: transactionInfo.amount,
        currency: transactionInfo.currency,
      );

      print('UPI Response: $response');
    } catch (e) {
      print('Error in UPI Payment: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('UPI Payment App'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              initiateTransaction();
            },
            child: Text('Initiate UPI Payment'),
          ),
        ),
      ),
    );
  }
}

void main() => runApp(UpiPaymentApp());

4. Test the App:

Run your Flutter app, and when you tap the “Initiate UPI Payment” button, it should open the selected UPI app installed on the device, allowing the user to complete the payment.

This is a basic example, and you should replace the placeholder values with actual values relevant to your use case. Additionally, ensure that the recipient’s UPI address is correctly set in the receiverUpiAddress field.

Note: The availability of UPI apps and the behavior of the UPI payment process may vary based on the device and location. Ensure that you test the app on a physical device that has a UPI app installed.

Leave a Reply

Your email address will not be published. Required fields are marked *