updateCart method

void updateCart(
  1. String itemId,
  2. int quantity,
  3. UpdateCart updateCart
)

Implementation

void updateCart(String itemId, int quantity, UpdateCart updateCart) async {
  updateCart.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'cart',
    "appKey": appKey,
    "action": "updateCart",
    "itemId": itemId,
    "quantity": quantity.toString(),
  };

  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 == "session-expired" || status == "please-login") {
        updateCart.onNotLoggedIn();
        updateCart.onLoadfinished();
        return;
      }
      if (status == "app-expired") {
        updateCart.onAppNotActive(data['info']['appName']);
        updateCart.onLoadfinished();
        return;
      }

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

    updateCart.onSuccess(status);
    updateCart.onLoadfinished();
  } catch (e) {
    updateCart.onError(e.toString());
    updateCart.onLoadfinished();
  }
}