safexpay 0.0.4 safexpay: ^0.0.4 copied to clipboard
A new Flutter plugin to create payment gateway using SafexPay
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:safexpay/constants/strings.dart';
import 'package:safexpay/constants/utility.dart';
import 'package:safexpay/safexpay.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
MerchantConstants.setDetails(
mId: 'Here-merchant-id',
mKey: 'Here-merchant-key',
aggId: 'Here-aggregator-id',
environment: Environment.TEST);//Environment.PRODUCTION
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await Safexpay.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false, home: UserInputPage());
}
}
class UserInputPage extends StatefulWidget {
@override
_UserInputPageState createState() => _UserInputPageState();
}
class _UserInputPageState extends State<UserInputPage>
implements SafeXPayPaymentCallback {
TextEditingController controller = TextEditingController();
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
late SafeXPayPaymentCallbackObservable _safeXPayPaymentCallbackObservable;
@override
void initState() {
super.initState();
_safeXPayPaymentCallbackObservable = SafeXPayPaymentCallbackObservable();
_safeXPayPaymentCallbackObservable.register(this);
}
@override
void dispose() {
_safeXPayPaymentCallbackObservable.unRegister(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('DemoPaymentApp'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
TextField(
controller: controller,
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: 'Enter the amount to pay'),
),
const SizedBox(
height: 32,
),
ElevatedButton(
onPressed: () {
String amount = controller.text.toString().trim();
if (amount.isEmpty) {
Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!,
message: 'Please enter amount to pay');
} else {
SafeXPayGateway safeXPayGateway = SafeXPayGateway(
orderNo: '${Random().nextInt(1000)}',
amount: double.parse(controller.text),
currency: 'INR',
transactionType: 'SALE',
channel: 'MOBILE',
successUrl: 'http://localhost/safexpay/response.php',
failureUrl: 'http://localhost/safexpay/response.php',
countryCode: 'IND');
safeXPayGateway.setUserDetails(
name: 'Customer-Name',
emailId: 'Customer-mail-id',
mobile: 'Customer-mobile-number');
safeXPayGateway.allowedPaymentMethods(
allowCardPayment: false,
allowNetBankingPayment: true,
allowWalletPayment: true,
allowUPIPayment: true);
/*MHSafeXPayGateway safeXPayGateway = MHSafeXPayGateway(
orderNo: '${Random().nextInt(1000)}',
amount: double.parse(controller.text),
currency: 'INR',
transactionType: 'SALE',
channel: 'MOBILE',
successUrl: 'http://localhost/safexpay/response.php',
failureUrl: 'http://localhost/safexpay/response.php',
countryCode: 'IND',
pgDetails:'pg_id|paymode|scheme_id|emi_months',
customerDetails:'Customer_Name|Customer_Mail_ID|Customer_Number|Unique_id|Logged-in(Y/N)',
cardDetails:'||||',
billDetails:'||||',
shipDetails:'||||||',
itemDetails:'||',
upiDetails:'',
otherDetails:'||||');*/
MaterialPageRoute route =
MaterialPageRoute(builder: (context) => safeXPayGateway);
Navigator.push(context, route);
}
},
child: Text(
'Proceed to Payment',
style: TextStyle(color: Colors.black),
),
style: ElevatedButton.styleFrom(
primary: Colors.cyanAccent,
// shape: RoundedRectangleBorder(),
padding:
const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
),
)
],
),
),
);
}
@override
void onInitiatePaymentFailure(String errorMessage) {
Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!, message: 'Transaction failed');
}
@override
void onPaymentCancelled() {
Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!, message: 'Transaction Cancelled');
}
@override
void onPaymentComplete(String orderID, String transactionID, String paymentID,
String paymentStatus) {
Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!, message: 'Transaction Successful');
}
}
class NextPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
// AppHeader(),
Expanded(
child: MaterialApp(
debugShowCheckedModeBanner: false,
// home: LoginScreen(),
),
),
],
);
}
}