startBookingOffers method

Future<void> startBookingOffers(
  1. BuildContext context, {
  2. void onSuccess()?,
  3. void goToDonation()?,
})

Implementation

Future<void> startBookingOffers(BuildContext context,
    {void Function()? onSuccess, void Function()? goToDonation}) async {
  if (isRoundTrip) {
    final outBoundDate = DateTime.parse(selectedStandardOffer
            .value?.trips?.firstOrNull?.flights?.lastOrNull?.arrivalDate ??
        DateTime(1990).toString());
    final inBoundDate = DateTime.parse(selectedStandardOffer
            .value?.trips?.lastOrNull?.flights?.firstOrNull?.departureDate ??
        DateTime(1990).toString());
    final outBoundArrivalTime = FlightSearchUtils.convertHourtoMinutes(
        selectedStandardOffer.value?.trips?.firstOrNull?.flights?.lastOrNull
                ?.arrivalTime ??
            '0');
    final inBoundDepartureTime = FlightSearchUtils.convertHourtoMinutes(
        selectedStandardOffer.value?.trips?.lastOrNull?.flights?.firstOrNull
                ?.departureTime ??
            '0');
    bool result = await checkFlightTimes(outBoundDate, outBoundArrivalTime,
        inBoundDate, inBoundDepartureTime, context);

    if (!result) {
      return;
    }
  }

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

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

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

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

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

      PaymentDetails paymentDetails = PaymentDetails(
          route: Routes.FLIGHT_SEARCH,
          isNonDiscounted: selectedStandardOffer.value?.isNonDiscounted ?? true,
          bookingId: bookedTripId,
          tripType:
              isRoundTrip ? TripType.roundTrip.text : TripType.oneWay.text,
          offer: selectedStandardOffer.value,
          totalOnlinePrice: selectedStandardOffer.value?.totalOnlinePrice ?? 0,
          totalDiscountedPrice:  selectedStandardOffer.value?.totalDiscountedPrice ?? 0,
          discountPercentage:
              selectedStandardOffer.value?.discountedPercentage ?? '0',
          adults: searchAdults.value,
          children: searchChildren.value,
          infantsInLap: searchInfantsInLap.value,
          infantsInSeat: searchInfantsInSeats.value,
          searchCreditEarned: searchCreditEarned.value,
          searchCreditUsed: searchCreditUsed.value,
          totalPassengers: totalPassengers,
          totalDiscountedPriceBTC: selectedStandardOffer.value?.totalDiscountedPriceBTC ?? 0,
          totalTax: selectedStandardOffer.value?.taxUsd ?? 0,
          milesUsed: (useNeoMiles.value ? 0 : 0),
          fromAirport: FromAirport(
            iataCode: selectedStandardOffer.value?.trips?.firstOrNull?.flights
                ?.firstOrNull?.fromAirportCode,
          ),
          toAirport: FromAirport(
            iataCode: selectedStandardOffer.value?.trips?.firstOrNull?.flights
                ?.firstOrNull?.toAirportCode,
          ),
          totalMilesEarned: totalMilesEarned.toInt(),
          email: userEmail,
          donationAmount: savedDonation.value);

      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);
  }
}