This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.dart
213 lines (197 loc) · 6.14 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import 'package:flutter/material.dart';
import 'package:upi_india/upi_india.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test UPI',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future<UpiResponse>? _transaction;
UpiIndia _upiIndia = UpiIndia();
List<UpiApp>? apps;
TextStyle header = TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
);
TextStyle value = TextStyle(
fontWeight: FontWeight.w400,
fontSize: 14,
);
@override
void initState() {
_upiIndia.getAllUpiApps(mandatoryTransactionId: false).then((value) {
setState(() {
apps = value;
});
}).catchError((e) {
apps = [];
});
super.initState();
}
Future<UpiResponse> initiateTransaction(UpiApp app) async {
return _upiIndia.startTransaction(
app: app,
receiverUpiId: "9078600498@ybl",
receiverName: 'Md Azharuddin',
transactionRefId: 'TestingUpiIndiaPlugin',
transactionNote: 'Not actual. Just an example.',
amount: 1.00,
);
}
Widget displayUpiApps() {
if (apps == null)
return Center(child: CircularProgressIndicator());
else if (apps!.length == 0)
return Center(
child: Text(
"No apps found to handle transaction.",
style: header,
),
);
else
return Align(
alignment: Alignment.topCenter,
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Wrap(
children: apps!.map<Widget>((UpiApp app) {
return GestureDetector(
onTap: () {
_transaction = initiateTransaction(app);
setState(() {});
},
child: Container(
height: 100,
width: 100,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.memory(
app.icon,
height: 60,
width: 60,
),
Text(app.name),
],
),
),
);
}).toList(),
),
),
);
}
String _upiErrorHandler(error) {
switch (error) {
case UpiIndiaAppNotInstalledException:
return 'Requested app not installed on device';
case UpiIndiaUserCancelledException:
return 'You cancelled the transaction';
case UpiIndiaNullResponseException:
return 'Requested app didn\'t return any response';
case UpiIndiaInvalidParametersException:
return 'Requested app cannot handle the transaction';
default:
return 'An Unknown error has occurred';
}
}
void _checkTxnStatus(String status) {
switch (status) {
case UpiPaymentStatus.SUCCESS:
print('Transaction Successful');
break;
case UpiPaymentStatus.SUBMITTED:
print('Transaction Submitted');
break;
case UpiPaymentStatus.FAILURE:
print('Transaction Failed');
break;
default:
print('Received an Unknown transaction status');
}
}
Widget displayTransactionData(title, body) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("$title: ", style: header),
Flexible(
child: Text(
body,
style: value,
)),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('UPI'),
),
body: Column(
children: <Widget>[
Expanded(
child: displayUpiApps(),
),
Expanded(
child: FutureBuilder(
future: _transaction,
builder: (BuildContext context, AsyncSnapshot<UpiResponse> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(
child: Text(
_upiErrorHandler(snapshot.error.runtimeType),
style: header,
), // Print's text message on screen
);
}
// If we have data then definitely we will have UpiResponse.
// It cannot be null
UpiResponse _upiResponse = snapshot.data!;
// Data in UpiResponse can be null. Check before printing
String txnId = _upiResponse.transactionId ?? 'N/A';
String resCode = _upiResponse.responseCode ?? 'N/A';
String txnRef = _upiResponse.transactionRefId ?? 'N/A';
String status = _upiResponse.status ?? 'N/A';
String approvalRef = _upiResponse.approvalRefNo ?? 'N/A';
_checkTxnStatus(status);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
displayTransactionData('Transaction Id', txnId),
displayTransactionData('Response Code', resCode),
displayTransactionData('Reference Id', txnRef),
displayTransactionData('Status', status.toUpperCase()),
displayTransactionData('Approval No', approvalRef),
],
),
);
} else
return Center(
child: Text(''),
);
},
),
)
],
),
);
}
}