Flutter #111: Integrate Payments in Flutter with Razorpay Payment Gateway


Integrate Payments in Flutter with Razorpay Payment Gateway
Flutter #111: Integrate Payments in Flutter with Razorpay Payment Gateway


import 'package:flutter/material.dart';
import 'package:razorpay_flutter/razorpay_flutter.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main()=> runApp(const MyApp());
class MyApp extends StatelessWidget{
  const MyApp({Key?key}):super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Razorpay',
      theme: ThemeData(
          primarySwatch: Colors.blue
      ),
      home: const MyHomePage(title: 'Razorpay Integration'),
    );
  }
}
class MyHomePage extends StatefulWidget{
  const MyHomePage({Key? key, required this.title}):super(key: key);
  final String title;
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>{
  int totalAmount=0;
  late Razorpay _razorpay;
  @override
  void initState(){
    super.initState();
    _razorpay = Razorpay();
    _razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS,_handlePaymentSuccess);
    _razorpay.on(Razorpay.EVENT_PAYMENT_ERROR,_handlePaymentError);
    _razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET,_handleExternalWallet);
  }
  @override
  void dispose(){
    super.dispose();
    _razorpay.clear();
  }

  void launchPayment() async{
    var options = {
      'key':'rzp_test_RDopTXiL6afuen',
      'amount':totalAmount*100,
      'name':'Nilen Patel Inc.',
      'description': 'Test Payment for Flutter App',
      'prefill':{'contact':'','email':''},
      'external':{'wallets':[]}
    };
    try{
      _razorpay.open(options);
    }catch(e){
      debugPrint(e.toString());
    }
  }

  void _handlePaymentError(PaymentFailureResponse response) {
    Fluttertoast.showToast(msg: 'Error'+response.code.toString() + ' '+ response.message.toString(),
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0
    );
  }

  void _handlePaymentSuccess(PaymentSuccessResponse response) {
    Fluttertoast.showToast(msg: 'Success'+response.paymentId.toString(),
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.green,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }

  void _handleExternalWallet(ExternalWalletResponse response) {
    Fluttertoast.showToast(msg: 'Success'+response.walletName.toString(),
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.green,
        textColor: Colors.black,
        fontSize: 16.0
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('Demo to make payment inside flutter app'),
            LimitedBox(
              maxWidth: 150.0,
              child: TextField(
                keyboardType: TextInputType.number,
                decoration: const InputDecoration(
                  hintText: 'Enter Amount'
                ),
                onChanged: (val){
                  setState(() {
                    totalAmount = num.parse(val).toInt();
                  });
                },
              ),
            ),
            const SizedBox(height: 15.0,),
            ElevatedButton(onPressed:(){ launchPayment();},child: const Text('PAY NOW'), )
          ],
        ),
      ),
    );
  }
}

#fluttertutorial #fluttertutorialforbeginners #flutterkicks #flutterfullcourse #flutterapp #flutterproject #fluttercourse #flutterappdevelopment #flutteranimation #flutterapi #flutterapptutorial #flutterapiintegration #flutterappproject #flutterbloc #flutterbasics #flutterbloctutorial #flutterbeginnertutorial #flutterbackend #flutterblocpattern #flutterbackgroundservice #fluttercrashcourse #fluttercompletetutorial #cookieswirlcfluttershydoll #flutterdevelopment #flutterdesktop #flutterdownload #flutterdesign #flutterdesktopapp #flutterdart #flutterd #flutterecommerceapp #flutterexplained

12 views0 comments