last 16-10-2025
This commit is contained in:
commit
5fdfdd9302
146 files changed
+10525
No files matched your search
@@ -0,0 +1,9 @@
|
||||
class ConstsApi {
|
||||
static const String baseUrl = "https://project2022.amrithaa.com";
|
||||
|
||||
// Example endpoints
|
||||
static const String home = "$baseUrl/ecomdemo/wp-json/homeapi/v1/data";
|
||||
static const String allproduct = "$baseUrl/ecomdemo/wp-json/wc/v3/products";
|
||||
static const String cart =
|
||||
"$baseUrl/ecomdemo/wp-json/custom-cart/v1/view?cart_token=cart_66fa27d8d44e4";
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:wordpress/providers/api_const.dart';
|
||||
import 'package:wordpress/providers/api_repositoryprovider.dart';
|
||||
import 'package:wordpress/viewmodel/all_product_model.dart';
|
||||
import 'package:wordpress/viewmodel/cart_model.dart';
|
||||
import 'package:wordpress/viewmodel/detail_model.dart';
|
||||
import 'package:wordpress/viewmodel/product_home_page_model.dart';
|
||||
|
||||
final apiRepositoryProvider = Provider((ref) => ApiRepository());
|
||||
|
||||
final productHomePageProvider =
|
||||
FutureProvider.autoDispose<ProductHomePageModel>((ref) async {
|
||||
final repo = ref.read(apiRepositoryProvider);
|
||||
return repo.fetchHomePage(ConstsApi.home);
|
||||
});
|
||||
final allProductProvider = FutureProvider.autoDispose<List<AllProductModel>>((
|
||||
ref,
|
||||
) async {
|
||||
final repo = ref.read(apiRepositoryProvider);
|
||||
return repo.fetchAllProducts(ConstsApi.allproduct);
|
||||
});
|
||||
|
||||
final cartProvider = FutureProvider.autoDispose<CartResponse>((ref) async {
|
||||
final repo = ref.read(apiRepositoryProvider);
|
||||
return repo.fetchCart(ConstsApi.cart);
|
||||
});
|
||||
|
||||
final productDetailProvider = FutureProvider.autoDispose
|
||||
.family<DetailModel, int>((ref, id) async {
|
||||
final repo = ref.read(apiRepositoryProvider);
|
||||
return repo.fetchProductDetail(id);
|
||||
});
|
||||
final cartHelperProvider = Provider((ref) => WooCartHelper());
|
||||
|
||||
final cartIdProvider = StateProvider<int?>((ref) => null);
|
||||
|
||||
final cartItemsProvider = FutureProvider<List<CartItem>>((ref) async {
|
||||
final cartId = ref.watch(cartIdProvider);
|
||||
if (cartId == null) return [];
|
||||
|
||||
final cartHelper = ref.read(cartHelperProvider);
|
||||
return await cartHelper.getCartItems();
|
||||
});
|
||||
|
||||
final cartItemCountProvider = Provider<int>((ref) {
|
||||
final cartItems = ref.watch(cartItemsProvider);
|
||||
return cartItems.when(
|
||||
data: (items) => items.fold(0, (sum, item) => sum + item.quantity),
|
||||
loading: () => 0,
|
||||
error: (_, __) => 0,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,283 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:wordpress/providers/api_const.dart';
|
||||
import 'package:wordpress/viewmodel/all_product_model.dart';
|
||||
import 'package:wordpress/viewmodel/cart_model.dart';
|
||||
import 'package:wordpress/viewmodel/detail_model.dart';
|
||||
import 'package:wordpress/viewmodel/product_home_page_model.dart';
|
||||
|
||||
class ApiRepository {
|
||||
static const String cartToken = "cart_66fa27d8d44e4";
|
||||
static const String secretKey = "my_secret_key_ecom2025";
|
||||
static const String baseUrl =
|
||||
"https://project2022.amrithaa.com/ecomdemo/wp-json/custom-cart/v1";
|
||||
|
||||
// Fetch homepage data
|
||||
Future<ProductHomePageModel> fetchHomePage(String url) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {
|
||||
'x-api-key': 's8f7g6h5j4k3l2m1n0p9q8r7s6t5u4v3',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> decoded = json.decode(response.body);
|
||||
final Map<String, dynamic> jsonData = decoded.containsKey('data')
|
||||
? decoded['data']
|
||||
: decoded;
|
||||
return ProductHomePageModel.fromJson(jsonData);
|
||||
} else {
|
||||
throw Exception('Failed to load home page data: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<AllProductModel>> fetchAllProducts(String url) async {
|
||||
final uri = Uri.parse(url).replace(
|
||||
queryParameters: {
|
||||
'consumer_key': 'ck_f9a199c7fdeb13e3c052578361f4d238c6199fec',
|
||||
'consumer_secret': 'cs_50688e7371ca6071a9d60ceb862477522b4e3b84',
|
||||
},
|
||||
);
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
'x-api-key': 's8f7g6h5j4k3l2m1n0p9q8r7s6t5u4v3',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final List decoded = json.decode(response.body);
|
||||
return decoded.map((json) => AllProductModel.fromJson(json)).toList();
|
||||
} else {
|
||||
throw Exception('Failed to load products: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<CartResponse> fetchCart(String url) async {
|
||||
final uri = Uri.parse(url);
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {'X-CART-SECRET': secretKey, 'Content-Type': 'application/json'},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final Map<String, dynamic> decoded = json.decode(response.body);
|
||||
return CartResponse.fromJson(decoded);
|
||||
} else {
|
||||
throw Exception('Failed to load cart: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<DetailModel> fetchProductDetail(int id) async {
|
||||
final uri = Uri.parse("${ConstsApi.allproduct}/$id").replace(
|
||||
queryParameters: {
|
||||
'consumer_key': 'ck_f9a199c7fdeb13e3c052578361f4d238c6199fec',
|
||||
'consumer_secret': 'cs_50688e7371ca6071a9d60ceb862477522b4e3b84',
|
||||
},
|
||||
);
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
'x-api-key': 's8f7g6h5j4k3l2m1n0p9q8r7s6t5u4v3',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final decoded = json.decode(response.body);
|
||||
return DetailModel.fromJson(decoded);
|
||||
} else {
|
||||
throw Exception('Failed to load product detail: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
|
||||
/// ✅ Add Item
|
||||
Future<void> addItem({required int productId, required int quantity}) async {
|
||||
final url = Uri.parse("$baseUrl/add");
|
||||
|
||||
final body = {
|
||||
"cart_token": cartToken,
|
||||
"product_id": productId,
|
||||
"quantity": quantity,
|
||||
};
|
||||
|
||||
final headers = {
|
||||
HttpHeaders.contentTypeHeader: 'application/json',
|
||||
'X-CART-SECRET': secretKey,
|
||||
};
|
||||
|
||||
print('🔗 Add URL: $url');
|
||||
print('📦 Add Body: $body');
|
||||
|
||||
try {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: headers,
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
print('📡 Add Status: ${response.statusCode}');
|
||||
print('📡 Add Body: ${response.body}');
|
||||
} catch (e) {
|
||||
print('💥 Add Exception: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// ✅ Remove Item
|
||||
Future<void> removeItem({
|
||||
required int productId,
|
||||
required int quantity,
|
||||
}) async {
|
||||
final url = Uri.parse("$baseUrl/remove");
|
||||
|
||||
final body = {
|
||||
"cart_token": cartToken,
|
||||
"product_id": productId,
|
||||
"quantity": quantity,
|
||||
};
|
||||
|
||||
final headers = {
|
||||
HttpHeaders.contentTypeHeader: 'application/json',
|
||||
'X-CART-SECRET': secretKey,
|
||||
};
|
||||
|
||||
print('🔗 Remove URL: $url');
|
||||
print('📦 Remove Body: $body');
|
||||
|
||||
try {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: headers,
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
print('📡 Remove Status: ${response.statusCode}');
|
||||
print('📡 Remove Body: ${response.body}');
|
||||
} catch (e) {
|
||||
print('💥 Remove Exception: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WooCartHelper {
|
||||
final String baseUrl =
|
||||
'https://project2022.amrithaa.com/ecomdemo/wp-json/wc/v3';
|
||||
final String consumerKey = 'ck_f9a199c7fdeb13e3c052578361f4d238c6199fec';
|
||||
final String consumerSecret = 'cs_50688e7371ca6071a9d60ceb862477522b4e3b84';
|
||||
|
||||
// Helper: build URL with credentials
|
||||
Uri buildUrl(String endpoint, [Map<String, String>? extraParams]) {
|
||||
final params = {
|
||||
'consumer_key': consumerKey,
|
||||
'consumer_secret': consumerSecret,
|
||||
...?extraParams,
|
||||
};
|
||||
return Uri.parse('$baseUrl$endpoint').replace(queryParameters: params);
|
||||
}
|
||||
|
||||
// Create a new cart (pending order)
|
||||
Future<int> createCart() async {
|
||||
final url = buildUrl('/orders');
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
"customer_id": 0,
|
||||
"payment_method": "bacs",
|
||||
"payment_method_title": "Bank Transfer",
|
||||
"set_paid": false,
|
||||
"status": "pending",
|
||||
"line_items": [],
|
||||
}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 201) {
|
||||
final data = jsonDecode(response.body);
|
||||
return data['id'];
|
||||
} else {
|
||||
throw Exception('Error creating cart: ${response.body}');
|
||||
}
|
||||
}
|
||||
|
||||
// Get cart items
|
||||
Future<List<CartItem>> getCartItems() async {
|
||||
final url = buildUrl(
|
||||
'https://project2022.amrithaa.com/ecomdemo/wp-json/custom-cart/v1/get?cart_token=my_secret_key_ecom2025',
|
||||
);
|
||||
final response = await http.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
final lineItems = data['line_items'] as List;
|
||||
return lineItems.map((item) => CartItem.fromJson(item)).toList();
|
||||
} else {
|
||||
throw Exception('Error getting cart: ${response.body}');
|
||||
}
|
||||
}
|
||||
|
||||
// Add item to cart
|
||||
Future<void> addToCart({
|
||||
required int productId,
|
||||
required int quantity,
|
||||
}) async {
|
||||
final url = Uri.parse(
|
||||
'https://project2022.amrithaa.com/ecomdemo/wp-json/custom-cart/v1/add',
|
||||
);
|
||||
|
||||
final body = {
|
||||
"cart_token": "cart_66fa27d8d44e4",
|
||||
"product_id": productId,
|
||||
"quantity": quantity,
|
||||
};
|
||||
|
||||
final headers = {
|
||||
HttpHeaders.contentTypeHeader: 'application/json',
|
||||
'X-CART-SECRET': 'my_secret_key_ecom2025', // 🔑 replace this
|
||||
};
|
||||
|
||||
print('🔗 URL: $url');
|
||||
print('📋 Headers: $headers');
|
||||
print('📦 Body: $body');
|
||||
|
||||
try {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: headers,
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
|
||||
print('📡 Status Code: ${response.statusCode}');
|
||||
print('📡 Response Body: ${response.body}');
|
||||
} catch (e) {
|
||||
print('💥 Exception: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove cart item
|
||||
|
||||
// Get cart total
|
||||
Future<Map<String, dynamic>> getCartTotal(int cartId) async {
|
||||
final url = buildUrl('/orders/$cartId');
|
||||
final response = await http.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
return {
|
||||
'subtotal': data['total'] ?? '0',
|
||||
'tax': data['total_tax'] ?? '0',
|
||||
'shipping': data['shipping_total'] ?? '0',
|
||||
'total': data['total'] ?? '0',
|
||||
};
|
||||
} else {
|
||||
throw Exception('Error getting cart total: ${response.body}');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user