completed velocity integration

This commit is contained in:
2026-05-27 19:01:24 +02:00
parent fec46fb6e9
commit 6fdd3183fb
29 changed files with 2760 additions and 2293 deletions

View File

@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'dart:async'; // Add Timer import
import 'dart:async';
import 'package:qpay/http/http.dart';
import 'package:qpay/models/api_response.dart';
import 'package:qpay/screens/transaction_controller.dart';
import 'package:provider/provider.dart';
@@ -50,10 +51,11 @@ class GatewayController extends ChangeNotifier {
notifyListeners();
}
Future<void> poll(String uid) async {
Future<ApiResponse<Map<String, dynamic>>> poll(String uid) async {
startLoading();
try {
dynamic workflowResponse = await http.get('/public/transaction/poll/$uid');
dynamic workflowResponse =
await http.get('/public/transaction/poll/$uid');
logger.i(workflowResponse.toString());
@@ -63,41 +65,65 @@ class GatewayController extends ChangeNotifier {
transactionController.model.paymentStatus = response['paymentStatus'];
finishLoading();
if(model.status == 'SUCCESS') {
if (model.status == 'SUCCESS') {
transactionController.updateReceiptData(response);
}
notifyListeners();
return ApiResponse.success(Map<String, dynamic>.from(response));
} catch (e) {
finishLoading();
model.status = 'FAILED';
model.errorMessage = "Network error. Please try again or contact support";
logger.e(e);
_showErrorSnackBar(
notifyListeners();
return ApiResponse.failure(
"Network error. Please try again or contact support",
);
}
}
Future<void> pollTransaction(String uid) async {
// only poll for 5 minutes
int max = 30;
while (!model.isCancelled) {
max++;
// Check cancellation flag instead of true
// Wait 5 seconds before the next poll
Future<ApiResponse<Map<String, dynamic>>> pollTransaction(String uid) async {
// poll up to 30 times (~5 minutes at 10-second intervals)
int maxAttempts = 30;
int attempt = 0;
while (!model.isCancelled && attempt < maxAttempts) {
attempt++;
// Wait 10 seconds before each poll request
await Future.delayed(const Duration(seconds: 10));
// Check again after delay in case it was cancelled during the delay
if (model.isCancelled) break;
await poll(uid);
ApiResponse<Map<String, dynamic>> result = await poll(uid);
if(max > 30) {
model.isCancelled = true;
// Only stop on success or failure
if (result.isSuccess) {
String status = model.status;
// If status is SUCCESS, we're done
if (status == 'SUCCESS') {
return result;
}
// Otherwise (e.g. PENDING), continue polling
} else {
// Network error — show failure UI and stop
return result;
}
}
// Timed out after max attempts
model.isCancelled = true;
model.status = 'FAILED';
model.errorMessage = 'We failed to check your transaction status';
finishLoading();
notifyListeners();
return ApiResponse.failure("Polling timed out");
}
// Alternative method using Timer instead of while loop
void pollTransactionWithTimer(String uid) {
Future<ApiResponse<Map<String, dynamic>>> pollTransactionWithTimer(
String uid) async {
_pollTimer = Timer.periodic(const Duration(seconds: 5), (timer) async {
if (model.isCancelled) {
timer.cancel();
@@ -121,6 +147,10 @@ class GatewayController extends ChangeNotifier {
);
}
});
// This returns immediately; the caller should use the timer callback approach.
// Consider refactoring this to use poll(String uid) directly for a consistent API.
return ApiResponse.failure(
"Polling via timer started; use a different flow for the result");
}
@override
@@ -129,4 +159,4 @@ class GatewayController extends ChangeNotifier {
_pollTimer?.cancel(); // Cancel timer if it exists
super.dispose();
}
}
}