checkoutItem method

void checkoutItem(
  1. String itemId,
  2. int quantity,
  3. CheckoutItem checkout
)

Implementation

void checkoutItem(String itemId, int quantity, CheckoutItem checkout) async {
  checkout.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'checkout',
    "appKey": appKey,
    "action": "checkout",
    "isMobile": "yes",
    "hostname": "window.location.hostname",
    "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 == "variants-found") {
        checkout.onVariants(data['variants']);
        checkout.onLoadfinished();
        return;
      }
      if (status == "please-login" || status == "session-expired") {
        checkout.onNotLoggedIn();
        checkout.onLoadfinished();
        return;
      }
      if (status == "pincode-not-serviceable") {
        checkout.onPinCodeNotServicable();
        checkout.onLoadfinished();
        return;
      }
      if (status == "app-expired") {
        checkout.onAppNotActive(data['info']['appName']);
        checkout.onLoadfinished();
        return;
      }
      if (status == "no-addresses-available") {
        checkout.onAddressNotAvailable();
        checkout.onLoadfinished();
        return;
      }
      if (status == "item-not-found" ||
          status == "not-in-stock" ||
          status == "cart-empty") {
        checkout.onError(status);
        checkout.onLoadfinished();
        return;
      }

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

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

    if (info['totalItems'] == 0) {
      checkout.onEmpty();
      checkout.onLoadfinished();
      return;
    }

    if (info['timeSlots']['status']) {
      checkout.onTimeSlots(
          info['timeSlots']['dates'], info['timeSlots']['slots']);
    }

    // if (info.containsKey('paymentGatewayKey') &&
    //     info.containsKey('paymentGatewayType') &&
    //     info.containsKey('paymentGatewayName')) {
    //   checkout.onPaymentGateway(info['paymentGatewayName'],
    //       info['paymentGatewayType'], info['paymentGatewayKey']);
    // }

    if (info.containsKey("paymentGateway")) {
      var paymentGateway = info['paymentGateway'];
      if (paymentGateway['type'] == 0) {
        checkout.onRazorpay(paymentGateway['key']);
      }
      if (paymentGateway['type'] == 1) {
        checkout.onStripe(
            paymentGateway['clientSecret'], paymentGateway['publishableKey']);
      }
      if (paymentGateway['type'] == 2) {
        checkout.onPhonePe(paymentGateway['paymentLink']);
      }
    }

    checkout.onResult(
      info['appName'],
      info['appIcon'],
      info['currency'],
      info['totalItems'],
      info['currencySymbol'],
      info['totalAmount'].toDouble(),
      info['orderId'],
      customerInfo['name'],
      customerInfo['address'],
      customerInfo['city'],
      customerInfo['state'],
      customerInfo['postalCode'],
      customerInfo['phoneNumber'],
      info['paymentGateway']['paymentType'],
      data['items'],
    );
    checkout.onLoadfinished();
  } catch (e) {
    checkout.onError(e.toString());
    checkout.onLoadfinished();
  }
}