diff --git a/lib/api/services/cheque_service.dart b/lib/api/services/cheque_service.dart index 4ca0626..d4502e1 100644 --- a/lib/api/services/cheque_service.dart +++ b/lib/api/services/cheque_service.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:dio/dio.dart'; class Cheque { final String? type; @@ -104,9 +106,16 @@ class ChequeService { String? stopAmount, String? stopComment, String? chequeIssueDate, + required String tpin, }) async { final response = await _dio.post( '/api/cheque/stop', + + options: Options( + validateStatus: (int? status) => true, + receiveDataWhenStatusError: true, + ), + data: { 'accountNumber': accountno, 'stopFromChequeNo': stopFromChequeNo, @@ -117,11 +126,12 @@ class ChequeService { 'stopAmount': stopAmount, 'stopComment': stopComment, 'chqIssueDate': chequeIssueDate, + 'tpin':tpin, }, ); - if (response.statusCode != 200) { - throw Exception("Error"); - } - return response.toString(); + if (response.statusCode != 200) { + throw Exception(jsonEncode(response.data)); + } + return response.toString(); } } diff --git a/lib/features/cheque/screens/stop_multiple_cheques_screen.dart b/lib/features/cheque/screens/stop_multiple_cheques_screen.dart index ffc76f1..672d2e2 100644 --- a/lib/features/cheque/screens/stop_multiple_cheques_screen.dart +++ b/lib/features/cheque/screens/stop_multiple_cheques_screen.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:kmobile/api/services/cheque_service.dart'; import 'package:kmobile/data/models/user.dart'; import 'package:kmobile/di/injection.dart'; +import 'package:kmobile/features/fund_transfer/screens/transaction_pin_screen.dart'; class StopMultipleChequesScreen extends StatefulWidget { final User selectedAccount; @@ -81,7 +82,7 @@ class _StopMultipleChequesScreenState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Stop Single Cheque'), + title: const Text('Stop Multiple Cheques'), ), body: Padding( padding: const EdgeInsets.all(16.0), @@ -209,32 +210,62 @@ class _StopMultipleChequesScreenState extends State { ), const SizedBox(height: 32), ElevatedButton( - onPressed: () async { + onPressed: () { if (_formKey.currentState!.validate()) { - try { - final response = await _chequeService.stopCheque( - accountno: widget.selectedAccount.accountNo!, - stopFromChequeNo: _stopFromChequeNoController.text, - instrType: widget.instrType, - stopToChequeNo: _stopToChequeNoController.text, - stopIssueDate: _stopIssueDateController.text, - stopExpiryDate: _stopExpiryDateController.text, - stopAmount: _stopAmountController.text, - stopComment: _stopCommentController.text, - chequeIssueDate: widget.date, - ); - if (!mounted) return; - final decodedResponse = jsonDecode(response); - final status = decodedResponse['status']; - final message = decodedResponse['message']; - if (status == 'SUCCESS') { - _showResponseDialog('Success', message); - } else { - _showResponseDialog('Error', message); - } - } catch (e) { - _showResponseDialog('Error', e.toString()); - } + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => TransactionPinScreen( + onPinCompleted: (ctx, pin) async { + Navigator.pop(context); + try { + final response = await _chequeService.stopCheque( + accountno: widget.selectedAccount.accountNo!, + stopFromChequeNo: + _stopFromChequeNoController.text, + instrType: widget.instrType, + stopToChequeNo: + _stopToChequeNoController.text, + stopIssueDate: _stopIssueDateController.text, + stopExpiryDate: + _stopExpiryDateController.text, + stopAmount: _stopAmountController.text, + stopComment: _stopCommentController.text, + chequeIssueDate: widget.date, + tpin: pin, + ); + if (!mounted) return; + final decodedResponse = jsonDecode(response); + final status = decodedResponse['status']; + final message = decodedResponse['message']; + if (status == 'SUCCESS') { + _showResponseDialog('Success', message); + } else { + _showResponseDialog('Error', message); + } + } on Exception catch (e) { + print('inside catch block'); + print(e.toString()); + + try { + final errorBodyString = e.toString().split('Exception: ')[1]; + final errorBody = jsonDecode(errorBodyString); + if (errorBody.containsKey('error') && errorBody['error'] == 'INCORRECT_TPIN') { + _showResponseDialog('Invalid TPIN', + 'The TPIN you entered is incorrect. Please try again.'); + } else { + _showResponseDialog( + 'Error', 'Internal Server Error'); + } + } catch (_) { + _showResponseDialog( + 'Error', 'Internal Server Error'); + } + } + }, + ), + ), + ); } }, child: const Text('Stop Cheque'), diff --git a/lib/features/cheque/screens/stop_single_cheque_screen.dart b/lib/features/cheque/screens/stop_single_cheque_screen.dart index c0c08df..4e4a2f3 100644 --- a/lib/features/cheque/screens/stop_single_cheque_screen.dart +++ b/lib/features/cheque/screens/stop_single_cheque_screen.dart @@ -1,9 +1,9 @@ import 'dart:convert'; - -import 'package:flutter/material.dart'; -import 'package:kmobile/api/services/cheque_service.dart'; import 'package:kmobile/data/models/user.dart'; import 'package:kmobile/di/injection.dart'; +import 'package:flutter/material.dart'; +import 'package:kmobile/api/services/cheque_service.dart'; +import 'package:kmobile/features/fund_transfer/screens/transaction_pin_screen.dart'; class StopSingleChequeScreen extends StatefulWidget { final User selectedAccount; @@ -180,32 +180,62 @@ class _StopSingleChequeScreenState extends State { ), const SizedBox(height: 32), ElevatedButton( - onPressed: () async { + onPressed: () { if (_formKey.currentState!.validate()) { - try { - final response = await _chequeService.stopCheque( - accountno: widget.selectedAccount.accountNo!, - stopFromChequeNo: _stopFromChequeNoController.text, - instrType: widget.instrType, - stopToChequeNo: _stopFromChequeNoController.text, - stopIssueDate: _stopIssueDateController.text, - stopExpiryDate: _stopExpiryDateController.text, - stopAmount: _stopAmountController.text, - stopComment: _stopCommentController.text, - chequeIssueDate: widget.date, - ); - if (!mounted) return; - final decodedResponse = jsonDecode(response); - final status = decodedResponse['status']; - final message = decodedResponse['message']; - if (status == 'SUCCESS') { - _showResponseDialog('Success', message); - } else { - _showResponseDialog('Error', message); - } - } catch (e) { - _showResponseDialog('Error', e.toString()); - } + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => TransactionPinScreen( + onPinCompleted: (ctx, pin) async { + Navigator.pop(context); + try { + final response = await _chequeService.stopCheque( + accountno: widget.selectedAccount.accountNo!, + stopFromChequeNo: + _stopFromChequeNoController.text, + instrType: widget.instrType, + stopToChequeNo: + _stopFromChequeNoController.text, + stopIssueDate: _stopIssueDateController.text, + stopExpiryDate: + _stopExpiryDateController.text, + stopAmount: _stopAmountController.text, + stopComment: _stopCommentController.text, + chequeIssueDate: widget.date, + tpin: pin, + ); + if (!mounted) return; + final decodedResponse = jsonDecode(response); + final status = decodedResponse['status']; + final message = decodedResponse['message']; + if (status == 'SUCCESS') { + _showResponseDialog('Success', message); + } else { + _showResponseDialog('Error', message); + } + } on Exception catch (e) { + print('inside catch block'); + print(e.toString()); + + try { + final errorBodyString = e.toString().split('Exception: ')[1]; + final errorBody = jsonDecode(errorBodyString); + if (errorBody.containsKey('error') && errorBody['error'] == 'INCORRECT_TPIN') { + _showResponseDialog('Invalid TPIN', + 'The TPIN you entered is incorrect. Please try again.'); + } else { + _showResponseDialog( + 'Error', 'Internal Server Error'); + } + } catch (_) { + _showResponseDialog( + 'Error', 'Internal Server Error'); + } + } + }, + ), + ), + ); } }, child: const Text('Stop Cheque'),