Skip to content

Instantly share code, notes, and snippets.

@vijayinyoutube
Created April 19, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayinyoutube/bed69cc9387ace5438be16f5e4632f5f to your computer and use it in GitHub Desktop.
Save vijayinyoutube/bed69cc9387ace5438be16f5e4632f5f to your computer and use it in GitHub Desktop.
Flutter and Razorpay integration
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:razorpay_flutter/razorpay_flutter.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int totalamount = 0;
static const platform = const MethodChannel("razorpay_flutter");
Razorpay _razorpay;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Razorpay Sample App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: "Enter the amount"),
onChanged: (value) {
setState(() {
totalamount = num.parse(value);
});
},
),
SizedBox(
height: 25,
),
RaisedButton(onPressed: openCheckout, child: Text('Pay'))
])),
),
);
}
@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 openCheckout() async {
var options = {
'key': '',
'amount': totalamount*100,
'name': 'My Corp',
'description': 'Payments',
'prefill': {'contact': '', 'email': ''},
'external': {
'wallets': ['paytm']
}
};
try {
_razorpay.open(options);
} catch (e) {
debugPrint(e);
}
}
void _handlePaymentSuccess(PaymentSuccessResponse response) {
Fluttertoast.showToast(msg: "SUCCESS: " + response.paymentId);
}
void _handlePaymentError(PaymentFailureResponse response) {
Fluttertoast.showToast(
msg: "ERROR: " + response.code.toString() + " - " + response.message);
}
void _handleExternalWallet(ExternalWalletResponse response) {
Fluttertoast.showToast(msg: "EXTERNAL_WALLET: " + response.walletName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment