Created
April 19, 2020 13:55
-
-
Save vijayinyoutube/bed69cc9387ace5438be16f5e4632f5f to your computer and use it in GitHub Desktop.
Flutter and Razorpay integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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