Update: 2026-05-07 03:06:15
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
import 'subscription_controller.dart';
|
||||
|
||||
class PaymentReceiptController extends GetxController {
|
||||
var payment = {}.obs;
|
||||
var isUploading = false.obs;
|
||||
final referenceController = TextEditingController();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
if (Get.arguments != null) {
|
||||
payment.value = Get.arguments;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
referenceController.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> submitReference() async {
|
||||
final ref = referenceController.text.trim();
|
||||
if (ref.isEmpty) {
|
||||
AppSnackbar.showWarning('تنبيه', 'الرجاء إدخال رقم المرجع أولاً');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
isUploading.value = true;
|
||||
String paymentId = payment['id'];
|
||||
|
||||
final res = await DioClient().client.post(
|
||||
'payments/verify-reference',
|
||||
data: {
|
||||
'payment_id': paymentId,
|
||||
'bank_reference': ref,
|
||||
},
|
||||
);
|
||||
|
||||
if (res.data['success'] == true) {
|
||||
final data = res.data['data'];
|
||||
|
||||
// Refresh subscription info
|
||||
if (Get.isRegistered<SubscriptionController>()) {
|
||||
Get.find<SubscriptionController>().loadAll();
|
||||
}
|
||||
|
||||
Get.back(); // close the screen
|
||||
|
||||
if (data['status'] == 'approved') {
|
||||
AppSnackbar.showSuccess('مبروك!', data['message']);
|
||||
} else {
|
||||
AppSnackbar.showInfo('تم الحفظ', data['message']);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to submit reference', e);
|
||||
AppSnackbar.showError('خطأ', 'فشل التحقق من رقم المرجع. تأكد من صحته أو حاول لاحقاً.');
|
||||
} finally {
|
||||
isUploading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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