Razorpay_payment method | Razorpay method to do payment | How to add razorpay payment method

 In this blog we are see that how to make payment in The appliction of Razorpay .

So first we make some files like razor to write our full code of razorpay .

void main(){
  runApp(Myapp());
}
class Myapp extends StatelessWidget {
  const Myapp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Razorpay payment method',
      home: Razor(),
    );
  }

In above code u can see that the first function of the code.

Like in our previous code we make new files to adding some code 

Lets start our code with a new file .

First we add the dependency of the flutter Razorpay method  

Or a flutter toast method

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:razorpay_flutter/razorpay_flutter.dart';

class Razor extends StatefulWidget {
  const Razor({super.key});

  @override
  State<Razor> createState() => _RazorState();
}

class _RazorState extends State<Razor> {
  late Razorpay _razorpay;
  TextEditingController amcontroller = TextEditingController();

In above code u can see that the t=dependency and some logics like 

Texteditiong controller => simply the controller function used to define the controller of the app it helps to make to make our application simple .

Late => this function is used to define the razorpay payment gateway application.

  void openCheckout(int amount) async {
    amount = amount * 100;
    var options = {
      'key': 'rzp_test_1DPSmmjsjs',
      'amount': amount,
      'name': 'Shivam',
      'prefill': {'contact': '1234567890', 'email': 'test@gmail.com'},
      'external': {
        'wallets': ['paytm']
      }
    };

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

 

In Above code

We can see that the function we make name opencheckout this function is used to make the form filling already

and we defined amount and add amount *100 then we defind var to add some logics 

first key

second amount

third shivam

third prefill it used fill the form without any persons

then we use external wallets like paytm ,phonepay,googlepay ad other we can add any 

then we add try catch block to catch the valur of the and

void handlePaymentSuccess(PaymentSuccessResponse response) {
    Fluttertoast.showToast(
      msg: 'Payment Successful: ' + response.paymentId!,
      toastLength: Toast.LENGTH_SHORT,
    );
  }

 
  void handlePaymentFailure(PaymentFailureResponse response) {
    Fluttertoast.showToast(
      msg: 'Payment Failed: ' + response.message!,
      toastLength: Toast.LENGTH_SHORT,
    );
  }

 
  void handleExternalWallet(ExternalWalletResponse response) {
    Fluttertoast.showToast(
      msg: 'External Wallet Selected: ' + response.walletName!,
      toastLength: Toast.LENGTH_SHORT,
    );
  }

 

In above code 

We can see that the handler function who handle the payment and make payment easy 

then we use flutter toast it show the msg those we r write that we add on payment methods 

like flutter toast

flutterpayment failure massage

and flutter external wallets

@override
  void initState() {
    super.initState();
    _razorpay = Razorpay();
    _razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, handlePaymentSuccess);
    _razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, handlePaymentFailure);
    _razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, handleExternalWallet);

  }

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

Void initstates

in the initstates we add on function to on the all functions accordingly

it control the textfrom field according to the user requirement

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      body: SingleChildScrollView(
        child: Column(
          children: [
            const SizedBox(height: 20),
            Image.network(
              'https://ca.pinterest.com/pin/shivam-name-tattoo--288441551150377856/',
              height: 200,
              width: 100,
            ),
            const SizedBox(height: 20),
            const Text(
              'Welcome to Razorpay Payment Method',
              style: TextStyle(color: Colors.white, fontSize: 20),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 20),
            Padding(
              padding: const EdgeInsets.all(20),
              child: TextFormField(
                cursorColor: Colors.white,
                autofocus: false,
                style: const TextStyle(color: Colors.white),
                decoration: InputDecoration(
                  labelText: 'Enter amount',
                  labelStyle: const TextStyle(
                    color: Colors.black,
                    fontSize: 20,
                  ),
                  border: OutlineInputBorder(
                    borderSide: const BorderSide(width: 2.0),
                    borderRadius: BorderRadius.circular(10),
                  ),
                 
                 
                  errorStyle: const TextStyle(
                      color: Colors.redAccent, fontSize: 20),
                ),
                controller: amcontroller,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter amount';
                  }
                  return null;
                },
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                if (amcontroller.text.toString().isNotEmpty) {
                  setState(() {
                    int amount = int.parse(amcontroller.text.toString());
                    openCheckout(amount);
                  });
                }
              },
              child: const Padding(
                padding: EdgeInsets.all(10),
                child: Text('Make Payment',style: TextStyle(color: Colors.white),),
              ),
              style: ElevatedButton.styleFrom(backgroundColor: Colors.green,),
            ),
          ],
        ),
      ),
    );
  }
}

In above code

u can see the previous explanation widegts not any here new widgets

Post a Comment

0 Comments