Files
velocity-pay-flutter/lib/screens/confirm/confirm_controller.dart
2025-07-14 01:04:53 +02:00

137 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:qpay/screens/transaction_controller.dart';
import '../../http/http.dart';
part 'confirm_controller.freezed.dart';
part 'confirm_controller.g.dart';
class ConfirmModel {
bool isLoading = false;
String status = '';
String? errorMessage;
bool isCancelled = false;
}
@freezed
abstract class ConfirmData with _$ConfirmData {
const factory ConfirmData({
required String status,
required String errorMessage,
}) = _ConfirmData;
factory ConfirmData.fromJson(Map<String, dynamic> json) =>
_$ConfirmDataFromJson(json);
}
class ConfirmController extends ChangeNotifier {
final ConfirmModel model = ConfirmModel();
late TransactionController transactionController;
final Http http = Http();
BuildContext context;
ConfirmController(this.context) {
transactionController = Provider.of<TransactionController>(
context,
listen: false,
);
}
void _showErrorSnackBar(String? message) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message.toString()),
behavior: SnackBarBehavior.floating,
backgroundColor: Colors.deepOrange,
),
);
});
}
Future<void> doTransaction() async {
try {
model.isLoading = true;
notifyListeners();
transactionController
.model
.formData = transactionController.model.formData.copyWith(
type: "REQUEST",
trace: transactionController.model.confirmationData['trace'],
authType: transactionController.model.selectedPaymentProcessor.authType,
);
dynamic response = await http.post(
'/transaction',
transactionController.model.formData,
);
logger.i(response.toString());
if (response['status'] != 'FAILED') {
model.status = response['status'];
transactionController.updateReceiptData(response);
if (transactionController.model.selectedPaymentProcessor.authType ==
"WEB") {
if (context.mounted) {
context.push(
'/gateway',
extra: transactionController.model.receiptData?['targetUrl'],
);
}
} else {
await pollTransaction(transactionController.model.receiptData?['id']);
context.push('/receipt');
}
} else {
model.status = response['status'];
model.errorMessage = response['errorMessage'];
_showErrorSnackBar(response['errorMessage']);
}
} catch (e) {
logger.e(e);
model.errorMessage = e.toString();
_showErrorSnackBar(
"Problem completing the transaction, please try again.",
);
}
model.isLoading = false;
notifyListeners();
}
Future<void> pollTransaction(String uid) async {
while (!model.isCancelled) {
// Check again after delay in case it was cancelled during the delay
if (model.isCancelled) break;
try {
dynamic response = await http.get('/transaction/poll/$uid');
logger.i(response.toString());
if (response['status'] == 'SUCCESS') {
model.status = response['status'];
transactionController.updateReceiptData(response);
notifyListeners();
break;
}
} catch (e) {
logger.e(e);
_showErrorSnackBar(
"Network error. Please try again or contact support",
);
}
// Check cancellation flag instead of true
// Wait 5 seconds before the next poll
await Future.delayed(const Duration(seconds: 5));
}
}
}