startBooking method

void startBooking(
  1. BuildContext context,
  2. bool isReserve, {
  3. void onSuccess()?,
  4. void goToDonation()?,
})

Implementation

void startBooking(BuildContext context, bool isReserve,
    {void Function()? onSuccess, void Function()? goToDonation}) async {
  if (hasDiscountedFlight) {
    int unusedExclusiveBookings = authenticationController
            .userProfileModel.value.data?.unusedExclusiveBookings ??
        0;
    if (unusedExclusiveBookings <= 0) {
      BookingLimitExceededDialog.show();
      return;
    }
  }

  if (isRoundTrip) {
    final outBoundDate = DateTime.parse(
        selectedExclusiveOffer.value?.departureTrip?.flights?.lastOrNull?.arrivalDate ??
            DateTime(1990).toString());
    final inBoundDate = DateTime.parse(
        selectedExclusiveOffer.value?.returnTrip?.flights?.firstOrNull?.departureDate ??
            DateTime(1990).toString());
    final outBoundArrivalTime = FlightSearchUtils.convertHourtoMinutes(
        selectedExclusiveOffer.value?.departureTrip?.flights?.lastOrNull?.arrivalTime ?? '0');
    final inBoundDepartureTime = FlightSearchUtils.convertHourtoMinutes(
        selectedExclusiveOffer.value?.returnTrip?.flights?.firstOrNull?.departureTime ?? '0');
    bool result = await checkFlightTimes(outBoundDate, outBoundArrivalTime,
        inBoundDate, inBoundDepartureTime, context);

    if (!result) {
      return;
    }
  }

  if (currentDonation.value == 0) {
    final res = await ClubImpactDialog.show();

    if (res == 1) {
      if (goToDonation != null) {
        goToDonation();
      }
      return;
    } else if (res != 2) {
      return;
    }
  }

  try {
    final data = {
      "offerId": selectedExclusiveOffer.value?.offerId?? '',
      "passengers": selectedPassengers.map((p) => p?.id ?? '').toList(),
      "donationAmount": savedDonation.value
    };

    if (isReserve) {
      data["tempReserve"] = true;
    }

    if (isRoundTrip) {
      data["returnTripId"] = selectedExclusiveOffer.value?.returnTrip?.id ?? '';
    }
    debugPrint(data.toString());

    var response = await Requests.getDio().post(
      "booking/v4/discounted",
      data: data,
    );

    if (response.statusCode == 200) {
      bookedTripId = response.data['data']['id'];
      debugPrint(bookedTripId);

      PaymentDetails paymentDetails = PaymentDetails(
          isNonDiscounted: selectedExclusiveOffer.value?.isNonDiscounted ?? false,
          route: Routes.FLIGHT_SEARCH,
          bookingId: bookedTripId,
          tripType:
              isRoundTrip ? TripType.roundTrip.text : TripType.oneWay.text,
          departureTrip: selectedExclusiveOffer.value?.departureTrip,
          returnTrip: selectedExclusiveOffer.value?.returnTrip,
          totalOnlinePrice: departureTotalOnlinePrice + returnTotalOnlinePrice,
          totalDiscountedPrice: departureTotalDiscountedPrice + returnTotalDiscountedPrice,
          adults: searchAdults.value,
          children: searchChildren.value,
          infantsInLap: searchInfantsInLap.value,
          infantsInSeat: searchInfantsInSeats.value,
          searchCreditEarned: searchCreditEarned.value,
          searchCreditUsed: searchCreditUsed.value,
          totalTax: (selectedExclusiveOffer.value?.departureTrip?.totalTaxes ?? 0) + (selectedExclusiveOffer.value?.returnTrip?.totalTaxes ?? 0),
          totalDiscountedPriceBTC: (selectedExclusiveOffer.value?.departureTrip?.totalDiscountedPriceBTC ?? 0) + (selectedExclusiveOffer.value?.returnTrip?.totalDiscountedPriceBTC ?? 0),
          milesUsed: (useNeoMiles.value ? 0 : 0),
          discountPercentage:
              isRoundTrip ? ((departureDiscountPercentage +
                returnDiscountPercentage) /
            2)
          .round().toString() : departureDiscountPercentage.toString(),

          totalPassengers: totalPassengers,
          fromAirport: FromAirport(
            iataCode:
                selectedExclusiveOffer.value?.departureTrip?.flights?.first.fromAirportCode,
          ),
          toAirport: FromAirport(
            iataCode:
                selectedExclusiveOffer.value?.departureTrip?.flights?.last.toAirportCode,
          ),
          totalMilesEarned: selectedExclusiveOffer.value?.milesToGain?.toInt(),
          email: userEmail,
          donationAmount: savedDonation.value);

      if (isReserve) {
        hasConfirmedBooking.value = true;
        goto(Routes.DASHBOARD_FLGIHTS_BOOKED);
      } else {
        paymentController.paymentType.value = PaymentType.flightBooking;
        paymentController.paymentExpireStartDate.value = DateTime.now();
        await paymentController.initPayment(paymentDetails,
            gotoPayment: false);
        hasConfirmedBooking.value = true;
        if (onSuccess != null) {
          onSuccess();
        }
        _stopSearchExpireTimer();
        _startPaymentExpireTimer(bookedTripId);
      }
    } else {
      debugPrint("error ${response.data}");

      CustomFlashWidget.showFlashMessage(
          title: "Error",
          message: response.data["error"] ??
              response.data["message"] ??
              'Error booking flight',
          type: FlashType.error);
    }
  } catch (e) {
    debugPrint("error ${e.toString()}");

    CustomFlashWidget.showFlashMessage(
        title: "Error",
        message: 'Error booking flight',
        type: FlashType.error);
  }
}