107 lines
3.8 KiB
Dart
107 lines
3.8 KiB
Dart
|
|
// ignore_for_file: file_names, unused_element, unused_local_variable
|
||
|
|
|
||
|
|
import 'dart:convert';
|
||
|
|
import 'dart:math';
|
||
|
|
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
|
|
import 'package:http/http.dart' as http;
|
||
|
|
import 'package:mobile_pos/Provider/profile_provider.dart';
|
||
|
|
|
||
|
|
import '../../../Const/api_config.dart';
|
||
|
|
import '../../../Repository/constant_functions.dart';
|
||
|
|
import '../../../http_client/customer_http_client_get.dart';
|
||
|
|
import '../Model/payment_credential_model.dart';
|
||
|
|
import '../Model/subscription_plan_model.dart';
|
||
|
|
|
||
|
|
class SubscriptionPlanRepo {
|
||
|
|
final _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
|
||
|
|
final Random _rnd = Random();
|
||
|
|
|
||
|
|
String getRandomString(int length) => String.fromCharCodes(Iterable.generate(length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
|
||
|
|
|
||
|
|
Future<List<SubscriptionPlanModelNew>> fetchAllPlans() async {
|
||
|
|
CustomHttpClientGet clientGet = CustomHttpClientGet(client: http.Client());
|
||
|
|
final uri = Uri.parse('${APIConfig.url}/plans');
|
||
|
|
|
||
|
|
final response = await clientGet.get(url: uri);
|
||
|
|
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
final parsedData = jsonDecode(response.body) as Map<String, dynamic>;
|
||
|
|
|
||
|
|
final partyList = parsedData['data'] as List<dynamic>;
|
||
|
|
return partyList.map((category) => SubscriptionPlanModelNew.fromJson(category)).toList();
|
||
|
|
// Parse into Party objects
|
||
|
|
} else {
|
||
|
|
throw Exception('Failed to fetch Products');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<List<SubscriptionPlanModel>> fetchAllPlansPrevious() async {
|
||
|
|
CustomHttpClientGet clientGet = CustomHttpClientGet(client: http.Client());
|
||
|
|
final uri = Uri.parse('${APIConfig.url}/plans');
|
||
|
|
|
||
|
|
final response = await clientGet.get(url: uri);
|
||
|
|
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
final parsedData = jsonDecode(response.body) as Map<String, dynamic>;
|
||
|
|
|
||
|
|
final partyList = parsedData['data'] as List<dynamic>;
|
||
|
|
return partyList.map((category) => SubscriptionPlanModel.fromJson(category)).toList();
|
||
|
|
// Parse into Party objects
|
||
|
|
} else {
|
||
|
|
throw Exception('Failed to fetch Products');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<PaymentCredentialModel> getPaymentCredential() async {
|
||
|
|
CustomHttpClientGet clientGet = CustomHttpClientGet(client: http.Client());
|
||
|
|
final uri = Uri.parse('${APIConfig.url}/gateways');
|
||
|
|
final response = await clientGet.get(url: uri);
|
||
|
|
print(response.statusCode);
|
||
|
|
print(response.body);
|
||
|
|
if (response.statusCode == 200) {
|
||
|
|
final parsedData = jsonDecode(response.body) as Map<String, dynamic>;
|
||
|
|
|
||
|
|
final data = parsedData['data'];
|
||
|
|
return PaymentCredentialModel.fromJson(data);
|
||
|
|
} else {
|
||
|
|
throw Exception('Failed to fetch credential');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> subscribePlan({
|
||
|
|
required WidgetRef ref,
|
||
|
|
required BuildContext context,
|
||
|
|
required int planId,
|
||
|
|
required String paymentMethod,
|
||
|
|
}) async {
|
||
|
|
final uri = Uri.parse('${APIConfig.url}/subscribes');
|
||
|
|
|
||
|
|
var responseData = await http.post(uri, headers: {
|
||
|
|
"Accept": 'application/json',
|
||
|
|
'Authorization': await getAuthToken(),
|
||
|
|
}, body: {
|
||
|
|
'plan_id': planId.toString(),
|
||
|
|
'subscriptionMethod': paymentMethod,
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
final parsedData = jsonDecode(responseData.body);
|
||
|
|
|
||
|
|
if (responseData.statusCode == 200) {
|
||
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Subscribe successful!')));
|
||
|
|
var data = ref.refresh(businessInfoProvider);
|
||
|
|
ref.refresh(getExpireDateProvider(ref));
|
||
|
|
|
||
|
|
Navigator.pop(context);
|
||
|
|
} else {
|
||
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Subscribe creation failed: ${parsedData['message']}')));
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
// Handle unexpected errors gracefully
|
||
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('An error occurred: $error')));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|