callGenerateLinkAPI function

Future<void> callGenerateLinkAPI(
  1. BuildContext context,
  2. String bankIds,
  3. String tranNos,
  4. String referrerId,
)

Implementation

Future<void> callGenerateLinkAPI(BuildContext context, String bankIds,
    String tranNos, String referrerId) async {
  showDialog(
    context: context,
    barrierDismissible: false,
    barrierColor: Colors.transparent,
    builder: (
      BuildContext context,
    ) {
      return Center(
        child: CircularProgressIndicator(
          backgroundColor: Colors.transparent,
          color: loadingColor,
        ),
      );
    },
  );

  bool? isProduction = B24PaymentSdk.storeIsProduction;
  bool? darkMode = B24PaymentSdk.storeDarkMode;
  final urlGenerate =
      isProduction == false ? envDemo().UrlGenerate : envPro().UrlGenerate;

  final Map<String, dynamic> data = {
    "bank_id": bankIds,
    "tran_id": tranNos,
  };

  final Map<String, String> headers = {
    'token': envDemo().token,
    'X-Referrer-Key': referrerId,
    'Content-Type': 'application/json',
  };

  final response = await http.post(
    Uri.parse(urlGenerate),
    body: jsonEncode(data),
    headers: headers,
  );

  if (response.statusCode == 200) {
    final decodedResponse = json.decode(response.body);
    final mobileDeepLink = decodedResponse['data']['mobile_deep_link'];
    final webPaymentUrl = decodedResponse['data']['web_payment_url'];
    final ulrMobileDeepLink = Uri.parse(mobileDeepLink);
    Navigator.of(context, rootNavigator: true).pop();
    if (mobileDeepLink == null && webPaymentUrl == null ||
        mobileDeepLink.isEmpty && webPaymentUrl.isEmpty) {
      showMessagePopup(context,
          bgColor: Color(0XFFFDEDEE),
          iconColor: Color(0XFFEE4D61),
          textColor: Color(0XFF131C18),
          content: translation.getTranslation('errorGetData'),
          icons: Icons.info_rounded);
    } else {
      if (Platform.isIOS) {

        await launchUrl(ulrMobileDeepLink,
            mode: LaunchMode.externalApplication);
      } else if (Platform.isAndroid) {
        await launchUrl(
          ulrMobileDeepLink,
        );
      } else if (!await canLaunchUrl(ulrMobileDeepLink)) {
        final urls = webPaymentUrl;
        Navigator.of(context).pop();
        Bottomsheets.show(
          context,
          Column(
            children: [
              Center(
                child: Container(
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(top: 10.0),
                  height: heightLine,
                  width: widthLine,
                  decoration: BoxDecoration(
                      color: darkMode == false
                          ? ThemeLight().indicatorColor
                          : ThemeDark().indicatorColor,
                      borderRadius: BorderRadius.circular(29.0)),
                ),
              ),
              SizedBox(
                height: 17.0,
              ),
              Flexible(
                child: WebView(
                  debuggingEnabled: true,
                  gestureNavigationEnabled: true,
                  initialUrl: urls,
                  javascriptMode: JavascriptMode.unrestricted,
                  gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{
                    Factory<OneSequenceGestureRecognizer>(
                        () => EagerGestureRecognizer())
                  },
                ),
              ),
            ],
          ),
        );
      }
    }

  } else {
    print('Failed to call GenerateLink API: ${response.statusCode}');
  }
}