141 lines
3.9 KiB
Dart
141 lines
3.9 KiB
Dart
import 'package:flutter/foundation.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<Map<String, dynamic>?> doTransaction() async {
|
|
try {
|
|
model.isLoading = true;
|
|
notifyListeners();
|
|
|
|
// todo: find out if we still need to update these fields
|
|
transactionController
|
|
.model
|
|
.formData = transactionController.model.formData.copyWith(
|
|
type: "REQUEST",
|
|
trace: transactionController.model.confirmationData['trace'],
|
|
id: transactionController.model.confirmationData['id'],
|
|
authType: transactionController.model.selectedPaymentProcessor.authType,
|
|
);
|
|
|
|
dynamic workflowResponse = await http.get(
|
|
'/public/transaction/request/'
|
|
'${transactionController.model.confirmationData['id']}/'
|
|
'${transactionController.model.selectedPaymentProcessor.authType}'
|
|
);
|
|
logger.i(workflowResponse.toString());
|
|
|
|
dynamic response = workflowResponse['body'];
|
|
|
|
if (response['status'] != 'FAILED') {
|
|
model.status = response['status'];
|
|
|
|
transactionController.updateReceiptData(response);
|
|
|
|
model.isLoading = false;
|
|
notifyListeners();
|
|
return response;
|
|
} 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();
|
|
return null;
|
|
}
|
|
|
|
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('/public/transaction/poll/$uid');
|
|
logger.i(response.toString());
|
|
|
|
if (response['status'] == 'SUCCESS') {
|
|
model.status = response['status'];
|
|
transactionController.updateReceiptData(response);
|
|
model.isCancelled = true;
|
|
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));
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// TODO: implement dispose
|
|
super.dispose();
|
|
}
|
|
}
|