safexpay 0.0.9 safexpay: ^0.0.9 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.PRODUCTION); //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: 'https://1bd1c7cd06b7.ngrok.io/Performance/public/simApp',
failureUrl: 'https://1bd1c7cd06b7.ngrok.io/Performance/public/simApp',
countryCode: 'IND');
safeXPayGateway.setUserDetails(
name: 'Nagendra',
emailId: 'nagendra@safexpay.com',
mobile: '7710910181');
safeXPayGateway.allowedPaymentMethods(
allowCardPayment: true,
allowNetBankingPayment: true,
allowWalletPayment: true,
allowUPIPayment: true);
/* safeXPayGateway.setUdf(
UDF1: '73432653',
UDF2: '',
UDF3: '5215241',
UDF4: '',
UDF5: '');*/
/* MHSafeXPayGateway safeXPayGateway = MHSafeXPayGateway(
orderNo: '${Random().nextInt(1000)}',
amount: double.parse(controller.text),
currency: 'INR',
transactionType: 'SALE',
channel: 'MOBILE',
successUrl: 'http://localhost/Performance/public/simApp',
failureUrl: 'http://localhost/Performance/publi/simApp',
countryCode: 'IND',
pgDetails: '|DC||',
customerDetails: 'Nagendra|nagesh@safexpay.com|7710910181| |Y',
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 orderID,
String transactionID,
String paymentID,
String paymentStatus,
String date,
String time,
String paymode,
String amount,
String udf1,
String udf2,
String udf3,
String udf4,
String udf5) {
print( 'onInitiatePaymentFailure : $orderID -- $transactionID -- $paymentID -- $paymentStatus -- $date -- $time -- $paymode -- $amount -- $udf1 -- $udf2 -- $udf3 -- $udf4 -- $udf5');
/*Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!,
message:
'$orderID -- $transactionID -- $paymentID -- $paymentStatus -- $date -- $time -- $paymode-- $amount -- $udf1 -- $udf2 -- $udf3 -- $udf4 -- $udf5');*/
}
@override
void onPaymentCancelled() {
/* Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!, message: 'Transaction Cancelled');*/
print( 'onPaymentCancelled : Transaction Cancelled');
}
@override
void onPaymentComplete(
String orderID,
String transactionID,
String paymentID,
String paymentStatus,
String date,
String time,
String paymode,
String amount,
String udf1,
String udf2,
String udf3,
String udf4,
String udf5) {
print( 'onPaymentComplete : $orderID -- $transactionID -- $paymentID -- $paymentStatus -- $date -- $time -- $paymode -- $amount -- $udf1 -- $udf2 -- $udf3 -- $udf4 -- $udf5');
/* Utility.showSnackBarMessage(
state: _scaffoldKey.currentState!,
message:
'$orderID -- $transactionID -- $paymentID -- $paymentStatus -- $date -- $time -- $paymode -- $amount -- $udf1 -- $udf2 -- $udf3 -- $udf4 -- $udf5');*/
}
}
class NextPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
// AppHeader(),
Expanded(
child: MaterialApp(
debugShowCheckedModeBanner: false,
// home: LoginScreen(),
),
),
],
);
}
}