initSocketServer method

Future<void> initSocketServer(
  1. String roomName,
  2. BuildContext context
)

Implementation

Future<void> initSocketServer(String roomName, BuildContext context) async {
  this.context = context;
  if (socket.connected) {
    return;
  }

  socket.connect();
  socket.on('connect', (_) {
    socket.emit('joinRoom', roomName);
  });

  socket.on('disconnect', (_) {});

  socket.on("broadcast", (data) async {
    if (data is Map && data.containsKey('transactionId')) {
      final id = data['transactionId'];
      wasInBackground = false;
      final checkoutReponse = await fetchDataFromApi(id, referrerId!);
      final status = checkoutReponse.data!.transInfo.status;
      if (status == "success") {
        onPaymentSuccess?.call();
        if (Enviroment.isWallet) {
          if (UniversalPlatform.isWeb) {
            Enviroment.setIsWallet(false);
            Navigator.of(context).pop();
            BottomsheetWeb.show(context, SuccessPageWallet());
          } else {
            Enviroment.setIsWallet(false);
            Navigator.of(context).pop();
            Navigator.of(context).push(MaterialPageRoute(
                builder: (context) => SuccessScreenWallet()));
          }
        } else {
          final displaySuccessPage =
              checkoutReponse.data!.checkoutPageConfig.displaySuccessPage;
          if (displaySuccessPage == true) {
            final tranInfoSuccesss = checkoutReponse.data!.transInfo;
            final billerInfoSuccess = checkoutReponse.data!.biller;
            final checkConfage = checkoutReponse.data!.checkoutPageConfig;

            if (UniversalPlatform.isWeb) {
              Navigator.of(context).pop();
              BottomsheetWeb.show(
                  context,
                  MainSuccessWebDialog(
                      darkMode: B24PaymentSdk.storeDarkMode!,
                      biller: billerInfoSuccess,
                      transInfo: tranInfoSuccesss));
            } else {
              Navigator.of(context).popUntil((route) => route.isFirst);
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (contents) => ScreenSuccess(
                    biller: billerInfoSuccess,
                    transInfo: tranInfoSuccesss,
                    checkoutPageConfig: checkConfage,
                  ),
                ),
              );
            }
          } else {
            if (UniversalPlatform.isWeb) {
              final redirectUrl = checkoutReponse.data!.transInfo.redirectUrl;
              if (redirectUrl.isNotEmpty) {
                html.window.open(redirectUrl, '_self');
              }
            } else {
              // wasInBackground = false;
              var redirectUrl = checkoutReponse.data!.transInfo.redirectUrl;
              if (redirectUrl.startsWith('https') ||
                  redirectUrl.startsWith('https://')) {
                final uri = Uri.tryParse(redirectUrl);
                if (uri == null) {
                  Navigator.of(context).popUntil((route) => route.isFirst);
                  return;
                }
                final path = uri.path.isEmpty ? '/' : uri.path;
                final fullPath =
                    uri.query.isNotEmpty ? '$path?${uri.query}' : path;
                final router = GoRouter.of(context);
                final pathUri = Uri.parse(fullPath);
                final matchList = router.configuration.findMatch(pathUri);
                if (matchList.matches.isNotEmpty) {
                  // Route exists
                  Navigator.of(context).popUntil((route) => route.isFirst);
                  context.go(fullPath);
                } else {
                  Navigator.of(context).popUntil((route) => route.isFirst);
                  // Route doesn't exist - launch externally
                  if (await canLaunchUrl(Uri.parse(redirectUrl))) {
                    await launchUrl(uri, mode: LaunchMode.platformDefault);
                  }
                }
              } else if (Uri.tryParse(redirectUrl)?.hasScheme ?? false) {
                final uri = Uri.parse(redirectUrl);
                Navigator.of(context).popUntil((route) => route.isFirst);
                if (await canLaunchUrl(uri)) {
                  await launchUrl(uri, mode: LaunchMode.externalApplication);
                }
              } else {
                Navigator.of(context).popUntil((route) => route.isFirst);
              }
            }
          }
        }
      }
    }
  });
}