-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.dart
364 lines (329 loc) · 16.4 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import 'dart:developer' as dev;
import 'package:flutter/material.dart';
import 'package:flutter_bkash/flutter_bkash.dart';
void main() {
// it should be the first line in main method
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.pink,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
debugShowCheckedModeBanner: false,
home: const HomePage(title: 'bKash Demo'),
);
}
}
/// paymentType: payWithAgreement, payWithoutAgreement, createAgreement
/// enum values: as per your requirement
enum PaymentType { payWithAgreement, payWithoutAgreement, createAgreement }
class HomePage extends StatefulWidget {
final String title;
const HomePage({Key? key, required this.title}) : super(key: key);
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final TextEditingController _amountController = TextEditingController();
final TextEditingController _agreementIdController = TextEditingController();
bool isLoading = false;
PaymentType _paymentType = PaymentType.payWithoutAgreement;
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(title: Text(widget.title)),
body: Stack(
children: [
isLoading
? const Center(
child: CircularProgressIndicator(
color: Colors.pink,
),
)
: SingleChildScrollView(
padding: const EdgeInsets.all(40),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (_paymentType != PaymentType.createAgreement) ...[
const Text(
'Amount :',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(
height: 10,
),
TextFormField(
controller: _amountController,
decoration: const InputDecoration(
hintText: "1240",
contentPadding: EdgeInsets.symmetric(
horizontal: 10, vertical: 0),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(5)),
borderSide: BorderSide(color: Colors.grey)),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.pink, width: 2.0),
),
// hintText: reviewTitle,
),
keyboardType: TextInputType.number,
maxLines: 1,
minLines: 1,
),
if (_paymentType == PaymentType.payWithAgreement) ...[
const SizedBox(
height: 20,
),
const Text(
'AgreementID :',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(
height: 10,
),
TextFormField(
controller: _agreementIdController,
decoration: const InputDecoration(
hintText: "User Agreement Id",
contentPadding: EdgeInsets.symmetric(
horizontal: 10, vertical: 0),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(5)),
borderSide: BorderSide(color: Colors.grey)),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(color: Colors.pink, width: 2.0),
),
// hintText: reviewTitle,
),
keyboardType: TextInputType.text,
maxLines: 1,
minLines: 1,
),
],
const SizedBox(height: 20.0),
],
const Divider(),
ListTile(
title: const Text('Pay (without agreement)'),
leading: Radio(
value: PaymentType.payWithoutAgreement,
groupValue: _paymentType,
onChanged: (value) {
setState(() => _paymentType = value!);
},
),
dense: true,
),
ListTile(
title: const Text('Pay with Agreement'),
leading: Radio(
value: PaymentType.payWithAgreement,
groupValue: _paymentType,
onChanged: (value) {
setState(() => _paymentType = value!);
},
),
dense: true,
),
ListTile(
title: const Text('Create agreement'),
leading: Radio(
value: PaymentType.createAgreement,
groupValue: _paymentType,
onChanged: (value) {
setState(() => _paymentType = value!);
},
),
dense: true,
),
const Divider(),
Center(
child: TextButton(
style: TextButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(3.0),
),
backgroundColor: Colors.pink),
child: const Text(
"Checkout",
style: TextStyle(color: Colors.white),
),
onPressed: () async {
setState(() {
isLoading = true;
});
/// create an instance of FlutterBkash
final flutterBkash =
FlutterBkash(logResponse: true);
/// if the payment type is createAgreement
if (_paymentType == PaymentType.createAgreement) {
try {
// remove focus from TextField to hide keyboard
FocusManager.instance.primaryFocus?.unfocus();
/// call createAgreement method to create an agreement as parameter pass the context
final result = await flutterBkash
.createAgreement(context: context);
/// show the log
dev.log(result.toString());
/// show the snack-bar
_showSnackbar(
"(Success) AgreementId: ${result.agreementId}");
} on BkashFailure catch (e, st) {
/// if any error occurred then show the log
dev.log(e.message, error: e, stackTrace: st);
/// show the snack-bar
_showSnackbar(e.message);
} catch (e, st) {
/// if any error occurred then show the log
dev.log("Something went wrong",
error: e, stackTrace: st);
/// show the snack-bar
_showSnackbar("Something went wrong");
}
setState(() {
isLoading = false;
});
return;
}
/// if the payment type is payWithoutAgreement
if (_paymentType ==
PaymentType.payWithoutAgreement) {
final amount = _amountController.text.trim();
if (amount.isEmpty) {
// if the amount is empty then show the snack-bar
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Amount is empty. Without amount you can't pay. Try again")));
setState(() {
isLoading = false;
});
return;
}
/// remove focus from TextField to hide keyboard
FocusManager.instance.primaryFocus?.unfocus();
/// Goto BkashPayment page & pass the params
try {
/// call pay method to pay without agreement as parameter pass the context, amount, merchantInvoiceNumber
final result = await flutterBkash.pay(
context: context,
// need the context as BuildContext
amount: double.parse(amount),
// need it double type
merchantInvoiceNumber: "tranId",
);
/// if the payment is success then show the log
dev.log(result.toString());
/// if the payment is success then show the snack-bar
_showSnackbar(
"(Success) tranId: ${result.trxId}");
} on BkashFailure catch (e, st) {
/// if something went wrong then show the log
dev.log(e.message, error: e, stackTrace: st);
/// if something went wrong then show the snack-bar
_showSnackbar(e.message);
} catch (e, st) {
/// if something went wrong then show the log
dev.log("Something went wrong",
error: e, stackTrace: st);
/// if something went wrong then show the snack-bar
_showSnackbar("Something went wrong");
}
setState(() {
isLoading = false;
});
return;
}
/// if the payment type is payWithAgreement
if (_paymentType == PaymentType.payWithAgreement) {
/// amount & agreementId is required
final amount = _amountController.text.trim();
final agreementId =
_agreementIdController.text.trim();
/// if the amount is empty then show the snack-bar
if (amount.isEmpty) {
// if the amount is empty then show the snack-bar
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Amount is empty. Without amount you can't pay. Try again")));
setState(() {
isLoading = false;
});
return;
}
/// is the agreementId is empty then show the snack-bar
if (agreementId.isEmpty) {
// if the agreementId is empty then show the snack-bar
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"AgreementId is empty. Without AgreementId you can't pay. Try again")));
setState(() {
isLoading = false;
});
return;
}
/// remove focus from TextField to hide keyboard
FocusManager.instance.primaryFocus?.unfocus();
/// Goto BkashPayment page & pass the params
try {
/// call payWithAgreement method to pay with agreement as parameter pass the context, amount, agreementId, marchentInvoiceNumber
final result =
await flutterBkash.payWithAgreement(
context: context,
amount: double.parse(amount),
agreementId: agreementId,
marchentInvoiceNumber:
"merchantInvoiceNumber",
);
/// print the result
dev.log(result.toString());
/// show the snack-bar with success message
_showSnackbar(
"(Success) tranId: ${result.trxId}");
} on BkashFailure catch (e, st) {
/// print the error message & stackTrace
dev.log(e.message, error: e, stackTrace: st);
_showSnackbar(e.message);
} catch (e, st) {
/// print the error message & stackTrace
dev.log("Something went wrong",
error: e, stackTrace: st);
/// show the snack-bar with error message
_showSnackbar("Something went wrong");
}
setState(() {
isLoading = false;
});
return;
}
},
),
)
],
),
),
],
),
);
}
/// show snack-bar with message
void _showSnackbar(String message) => ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}