Update: 2026-05-07 03:06:15
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import 'package:get/get.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
|
||||
class SubscriptionController extends GetxController {
|
||||
var plans = <Map<String, dynamic>>[].obs;
|
||||
var currentSubscription = Rxn<Map<String, dynamic>>();
|
||||
var myPayments = <Map<String, dynamic>>[].obs;
|
||||
var isLoading = true.obs;
|
||||
var isCreatingPayment = false.obs;
|
||||
var activePaymentRequest = Rxn<Map<String, dynamic>>();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
loadAll();
|
||||
}
|
||||
|
||||
Future<void> loadAll() async {
|
||||
isLoading.value = true;
|
||||
await Future.wait([
|
||||
loadPlans(),
|
||||
loadCurrentSubscription(),
|
||||
loadMyPayments(),
|
||||
]);
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
Future<void> loadPlans() async {
|
||||
try {
|
||||
final res = await DioClient().client.get('subscriptions/plans');
|
||||
if (res.data['success'] == true && res.data['data'] != null) {
|
||||
plans.value = List<Map<String, dynamic>>.from(res.data['data']);
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to load plans', e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadCurrentSubscription() async {
|
||||
try {
|
||||
final res = await DioClient().client.get('subscriptions/current');
|
||||
if (res.data['success'] == true && res.data['data'] != null) {
|
||||
currentSubscription.value = Map<String, dynamic>.from(res.data['data']);
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to load subscription', e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadMyPayments() async {
|
||||
try {
|
||||
final res = await DioClient().client.get('payments/my-requests');
|
||||
if (res.data['success'] == true && res.data['data'] != null) {
|
||||
myPayments.value = List<Map<String, dynamic>>.from(res.data['data']);
|
||||
// Check for active pending payment
|
||||
final pending = myPayments.firstWhereOrNull((p) => p['status'] == 'pending');
|
||||
activePaymentRequest.value = pending;
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to load my payments', e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> createPaymentRequest(String planId) async {
|
||||
try {
|
||||
isCreatingPayment.value = true;
|
||||
final res = await DioClient().client.post('payments/create', data: {'plan_id': planId});
|
||||
if (res.data['success'] == true && res.data['data'] != null) {
|
||||
final result = Map<String, dynamic>.from(res.data['data']);
|
||||
activePaymentRequest.value = result;
|
||||
await loadMyPayments();
|
||||
return result;
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to create payment', e);
|
||||
} finally {
|
||||
isCreatingPayment.value = false;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user