import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:provider/provider.dart'; import 'package:qpay/http/http.dart'; import 'package:qpay/models/api_response.dart'; import 'package:qpay/screens/pay/pay_controller.dart' as pc; import 'package:qpay/screens/transaction_controller.dart' as tc; import 'package:qpay/screens/home/home_controller.dart' as hc; class ReceiptModel { Map? receiptData; bool isLoading = false; String? errorMessage; String status = ''; } class ReceiptController extends ChangeNotifier { final ReceiptModel model = ReceiptModel(); final Http http = Http(); late tc.TransactionController transactionController; late SharedPreferences prefs; BuildContext context; ReceiptController(this.context) { transactionController = Provider.of( context, listen: false, ); } void setLoading(bool value) { model.isLoading = value; notifyListeners(); } Future>> doIntegration(String id) async { try { model.isLoading = true; model.status = ''; notifyListeners(); dynamic workflowResponse = await http.get( '/public/transaction/integration/$id', ); logger.i(workflowResponse.toString()); dynamic response = workflowResponse['body']; model.isLoading = false; if (response['status'] == 'SUCCESS') { model.status = response['status']; transactionController.updateReceiptData(response); notifyListeners(); return ApiResponse.success(Map.from(response)); } else { model.status = response['status']; model.errorMessage = response['errorMessage']; notifyListeners(); return ApiResponse.failure( response['errorMessage'] ?? "Integration failed", ); } } catch (e) { model.isLoading = false; model.errorMessage = e.toString(); notifyListeners(); return ApiResponse.failure(e.toString()); } } Future repeatTransaction(BuildContext context) async { setLoading(true); // fetch provider & update tran controller String providerId = model.receiptData?['billClientId'] ?? ''; Map provider = await http.get( '/public/providers/client/$providerId', ); transactionController.model.selectedProvider = hc.BillProvider.fromJson( provider, ); // update tran controller formdata with credit account, email, name, phone & providerLabel transactionController.model.formData = transactionController.model.formData .copyWith( creditAccount: model.receiptData?['creditAccount'] ?? '', creditPhone: model.receiptData?['creditPhone'] ?? '', creditName: model.receiptData?['creditName'] ?? '', creditEmail: model.receiptData?['creditEmail'] ?? '', ); // fetch product and update tran controller formdata String providerUid = transactionController.model.selectedProvider?.uid ?? ''; String productUid = model.receiptData?['productUid'] ?? ''; if (productUid.isNotEmpty) { Map product = await http.get( '/public/providers/$providerUid/products/$productUid', ); transactionController.model.selectedProduct = pc.BillProduct.fromJson( product, ); } // update credit amount, notification phone number, payment processor transactionController .model .formData = transactionController.model.formData.copyWith( amount: model.receiptData?['amount'].toStringAsFixed(2) ?? '', debitPhone: model.receiptData?['debitPhone'] ?? '', paymentProcessorLabel: model.receiptData?['paymentProcessorLabel'] ?? '', paymentProcessorName: model.receiptData?['paymentProcessorName'] ?? '', id: null, ); setLoading(false); context.push('/make-payment'); } Future>> getReceiptData(String id) async { setLoading(true); try { Map response = await http.get('/public/transaction/$id'); model.receiptData = response; setLoading(false); return ApiResponse.success(response); } catch (e) { setLoading(false); return ApiResponse.failure( "Problem fetching transaction data, are you connected to the internet?", ); } } void updateReceiptData(Map data) { model.receiptData = data; notifyListeners(); } }