Skip to content

Commit a55d235

Browse files
committed
code and error handling improvements
1 parent c674ef0 commit a55d235

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

example/lib/main.dart

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,24 @@ class HomePageState extends State<HomePage> {
175175
setState(() {
176176
isLoading = true;
177177
});
178-
final flutterBkash = FlutterBkash(
179-
bkashCredentials: const BkashCredentials(
180-
appKey: "",
181-
appSecret: "",
182-
password: "",
183-
username: "",
184-
isSandbox: false));
178+
final flutterBkash = FlutterBkash();
185179

186180
if (_paymentType == PaymentType.createAgreement) {
187181
try {
188182
// remove focus from TextField to hide keyboard
189183
FocusManager.instance.primaryFocus?.unfocus();
190184
final result = await flutterBkash
191185
.createAgreement(context: context);
192-
dev.log(
193-
name: "createAgreement -> ",
194-
result.toString());
186+
dev.log(result.toString());
187+
_showSnackbar(
188+
"(Success) AgreementId: ${result.agreementId}");
195189
} on BkashFailure catch (e, st) {
196190
dev.log(e.message, error: e, stackTrace: st);
197-
} catch (e) {
198-
dev.log("Something went wrong");
191+
_showSnackbar(e.message);
192+
} catch (e, st) {
193+
dev.log("Something went wrong",
194+
error: e, stackTrace: st);
195+
_showSnackbar("Something went wrong");
199196
}
200197
setState(() {
201198
isLoading = false;
@@ -222,20 +219,23 @@ class HomePageState extends State<HomePage> {
222219
// Goto BkashPayment page & pass the params
223220

224221
try {
225-
final res = await flutterBkash.pay(
222+
final result = await flutterBkash.pay(
226223
context: context,
227224
amount: double.parse(
228225
amount), // need it double type
229226
merchantInvoiceNumber: "tranId",
230227
);
231228

232-
dev.log(
233-
name: "withoutAgreement -> ",
234-
res.toString());
229+
dev.log(result.toString());
230+
_showSnackbar(
231+
"(Success) tranId: ${result.trxId}");
235232
} on BkashFailure catch (e, st) {
236233
dev.log(e.message, error: e, stackTrace: st);
237-
} catch (e) {
238-
dev.log("Something went wrong");
234+
_showSnackbar(e.message);
235+
} catch (e, st) {
236+
dev.log("Something went wrong",
237+
error: e, stackTrace: st);
238+
_showSnackbar("Something went wrong");
239239
}
240240
setState(() {
241241
isLoading = false;
@@ -286,10 +286,15 @@ class HomePageState extends State<HomePage> {
286286
);
287287

288288
dev.log(result.toString());
289+
_showSnackbar(
290+
"(Success) tranId: ${result.trxId}");
289291
} on BkashFailure catch (e, st) {
290292
dev.log(e.message, error: e, stackTrace: st);
291-
} catch (e) {
292-
dev.log("Something went wrong");
293+
_showSnackbar(e.message);
294+
} catch (e, st) {
295+
dev.log("Something went wrong",
296+
error: e, stackTrace: st);
297+
_showSnackbar("Something went wrong");
293298
}
294299
setState(() {
295300
isLoading = false;
@@ -306,4 +311,8 @@ class HomePageState extends State<HomePage> {
306311
),
307312
);
308313
}
314+
315+
void _showSnackbar(String message) => ScaffoldMessenger.of(context)
316+
..hideCurrentSnackBar()
317+
..showSnackBar(SnackBar(content: Text(message)));
309318
}

lib/src/flutter_bkash.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FlutterBkash {
6060
(l) async => throw l,
6161
(apiRes) async {
6262
if (apiRes.statusCode != "0000") {
63-
throw BkashFailure();
63+
throw BkashFailure(message: apiRes.statusMessage);
6464
}
6565

6666
final bkashPaymentStatus = await Navigator.push<BkashPaymentStatus>(
@@ -87,7 +87,7 @@ class FlutterBkash {
8787
(r) {
8888
// failed to execute
8989
if (r.statusCode != "0000") {
90-
throw BkashFailure();
90+
throw BkashFailure(message: r.statusMessage);
9191
}
9292
return BkashPaymentResponse(
9393
r.paymentExecuteTime,
@@ -103,7 +103,7 @@ class FlutterBkash {
103103
if (bkashPaymentStatus == BkashPaymentStatus.canceled) {
104104
throw BkashFailure(message: "Payment Cancelled");
105105
}
106-
throw BkashFailure();
106+
throw BkashFailure(message: "Payment Failed");
107107
},
108108
);
109109
},
@@ -138,7 +138,7 @@ class FlutterBkash {
138138
(l) async => throw l,
139139
(agrRes) async {
140140
if (agrRes.statusCode != "0000") {
141-
throw BkashFailure();
141+
throw BkashFailure(message: agrRes.statusMessage);
142142
}
143143

144144
final bkashPaymentStatus = await Navigator.push<BkashPaymentStatus>(
@@ -165,7 +165,7 @@ class FlutterBkash {
165165
(r) {
166166
// failed to execute
167167
if (r.statusCode != "0000") {
168-
throw BkashFailure();
168+
throw BkashFailure(message: r.statusMessage);
169169
}
170170
return BkashAgreementResponse(
171171
r.agreementExecuteTime,
@@ -180,7 +180,7 @@ class FlutterBkash {
180180
if (bkashPaymentStatus == BkashPaymentStatus.canceled) {
181181
throw BkashFailure(message: "Agreement creation Cancelled");
182182
}
183-
throw BkashFailure();
183+
throw BkashFailure(message: "Agreement creation Failed");
184184
},
185185
);
186186
},
@@ -217,7 +217,7 @@ class FlutterBkash {
217217
(l) async => throw l,
218218
(apiRes) async {
219219
if (apiRes.statusCode != "0000") {
220-
throw BkashFailure();
220+
throw BkashFailure(message: apiRes.statusMessage);
221221
}
222222

223223
final bkashPaymentStatus = await Navigator.push<BkashPaymentStatus>(
@@ -244,7 +244,7 @@ class FlutterBkash {
244244
(r) {
245245
// failed to execute
246246
if (r.statusCode != "0000") {
247-
throw BkashFailure();
247+
throw BkashFailure(message: r.statusMessage);
248248
}
249249
return BkashPaymentResponse(
250250
r.paymentExecuteTime,

0 commit comments

Comments
 (0)