getCart method

void getCart(
  1. GetCart getCart
)

Implementation

void getCart(GetCart getCart) async {
  getCart.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'cart',
    "appKey": appKey,
    "action": "getCart",
  };

  try {
    final response = await http.post(
      Uri.parse(url),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer $token'
      },
      body: body,
    );

    final data = json.decode(response.body);
    int code = data['response']['code'];
    String status = data['response']['status'];

    if (code == 400) {
      if (status == "cart-empty") {
        getCart.onEmpty();
        getCart.onLoadfinished();
        return;
      }

      if (status == "session-expired" || status == "please-login") {
        getCart.onNotLoggedIn();
        getCart.onLoadfinished();
        return;
      }

      if (status == "app-expired") {
        getCart.onAppNotActive(data['info']['appName']);
        getCart.onLoadfinished();
        return;
      }

      getCart.onError(status);
      getCart.onLoadfinished();
      return;
    }

    Map<String, dynamic> info = data['info'];
    List<dynamic> items = data['items'];

    getCart.onResult(
      info['appName'],
      info['currency'],
      info['currencySymbol'],
      info['totalAmount'].toDouble().toDouble(),
      info['totalItems'],
      items,
    );
    getCart.onLoadfinished();
  } catch (e) {
    getCart.onError(e.toString());
    getCart.onLoadfinished();
  }
}