initiateBookingCryptoPayment method

void initiateBookingCryptoPayment()

Implementation

void initiateBookingCryptoPayment() async {
  try {
    var url = '';
    var type = '';
    var amountText = '';
    var data = <String, dynamic>{
      "currency": selectedCryptoCurrency.value.ticker,
      "network": selectedCryptoCurrency.value.network,
    };

    if (isFlightBooking) {
      data["bookingId"] = paymentDetails.value.bookingId;
      data["useNeoMiles"] = useNeoMiles.value;
      url = "payments/crypto/initiate/booking";
      type =
          '${paymentDetails.value.fromAirport?.code ?? ''} - ${paymentDetails.value.toAirport?.code ?? ''} ';
      amountText = 'Flight Booking';
    }

    if (isSearchCredit) {
      if (creditPackage.value == null) {
        CustomFlashWidget.showFlashMessage(
          type: FlashType.error,
          title: "Error",
          message: 'Please Select a search credit package first',
        );
        // DefaultSnackbar.show('Error', 'Please Select a search credit package first');
        return;
      }
      data["packageId"] = creditPackage.value!.id;
      url = "payments/crypto/initiate/search-credit";
      type = 'Search Credit package';
      amountText = 'Search Credit';
    }

    if (isMembership) {
      type = subscriptionName.value;
      data = _getSubscriptionPaymentPayload();
      url = _getSubscriptionPaymentUrl();
      amountText = 'Membership';
    }

    var response = await Requests.getDio().post(
      url,
      data: data,
    );

    log("Params: $data");
    log('Initiate Booking: $response');

    if (response.statusCode == 200) {
      // // Remove: for testing
      // //================================
      // final bookingConfirmController = Get.put(BookingConfirmationController());
      // log("ID: ${paymentDetails.value.bookingId}");
      // bookingConfirmController.initConfirmation(paymentDetails.value.bookingId);
      // //================================
      bookedPaymentId = isMembership
          ? subscriptionType.value == SubscriptionType.corporateAddon
              ? response.data['data']['id']
              : response.data['data']['paymentId']
          : response.data['data']['id'];

      String? paymentAddress = (isMembership
              ? subscriptionType.value == SubscriptionType.corporateAddon
                  ? response.data['data']['paymentAddress']
                  : response.data['data']['wallet']
              : response.data['data']['paymentAddress']) ??
          response.data['data']?['address'] ??
          '';
      // String paymentAddress = await getAddress();
      if ((paymentAddress ?? '').isEmpty) {
        startTimer.value = false;
        CustomFlashWidget.showFlashMessage(
          type: FlashType.error,
          title: "Contact Support",
          message: "Payment Address is not found",
        );
        // DefaultSnackbar.show(
        //     "Contact Support", "Payment Address is not found");
        return;
      }
      print(paymentDetails.value.tripType.toString());
      final String cryptoPrice = response.data['data']?['amount_to_pay'] ??
          response.data['data']?['payment']?['amount_to_pay'] ??
          '0';
      final String usdPrice = response.data['data']?['total_amount'] ??
          response.data['data']?['payment']?['total_amount'] ??
          '0';
      _startPaymentExpireTimer();
      CryptoPaymentDialog.show(
        barrierDismissible: false,
        wallet: paymentAddress ?? '',
        icon: selectedCryptoCurrency.value.image ?? "",
        cryptoPrice: cryptoPrice.withCommas,
        usdPrice: usdPrice.withCommas,
        network: capitalizeFirstLetter(
            selectedCryptoCurrency.value.network ?? ""),
        currency: capitalizeFirstLetter(
            selectedCryptoCurrency.value.ticker ?? ""),
        type: type,
        amountType: amountText,
        timeLimit: (isFlightBooking && paymentDetails.value.isNonDiscounted) ? 480 : 900,
        onTap: () {
          startTimer.value = false;
          isAgreedPayment.value=false;
          isAgreedExclusive.value=false;
          isAgreedExclusivePlus.value=false;
          isAgreedTerms.value=false;
          closeDialog();
        },
        onChangeMethod: () {
          startTimer.value = false;
          isAgreedPayment.value=false;
          isAgreedExclusive.value=false;
          isAgreedExclusivePlus.value=false;
          isAgreedTerms.value=false;
          closeDialog();
          if (isSearchCredit) {
            PurchaseCreditDialog.show();
          }
        },
      );

      if (response.data['data']['status'] == "PENDING" ||
          response.data['data']['status'] == "PENDING_PAYMENT") {
        startTimer.value = true;
        timer = Timer.periodic(const Duration(seconds: 10), (timer) {
          startTimer.value ? getPaymentInfo() : null;
        });
      }
    } else {
      CustomFlashWidget.showFlashMessage(
        type: FlashType.error,
        title: "Error",
        message: response.data['message'],
      );
      // DefaultSnackbar.show("Error", response.data['message']);
    }
  } catch (e) {
    debugPrint(e.toString());
    CustomFlashWidget.showFlashMessage(
      type: FlashType.error,
      title: "Error",
      message: 'Something unexpected happened. Please try again later!',
    );
  }
}