callGenerateLinkAPI function
Future<void>
callGenerateLinkAPI(
- BuildContext context,
- String bankIds,
- String tranNos,
- 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 urlGenerate = BaseUrl.baseUrl! + api().GenerateLinkEndpoint;
final Map<String, dynamic> data = {
"bank_id": bankIds,
"tran_id": tranNos,
};
final Map<String, String> headers = {
'token':
//'342jjdfenfwnf',
envDemo().token,
'X-Referrer-Key': referrerId,
'Content-Type': 'application/json',
};
http.Response? response;
try {
response = await http.post(
Uri.parse(urlGenerate),
body: jsonEncode(data),
headers: headers,
);
} catch (e) {
Navigator.of(context, rootNavigator: true).pop();
showMessagePopup(
context,
bgColor: Color(0XFFFDEDEE),
iconColor: Color(0XFFEE4D61),
textColor: Color(0XFF131C18),
content: translation.getTranslation('errorGetData'),
icons: Icons.info_rounded,
);
return;
}
// Dismiss the loading dialog immediately after API returns
Navigator.of(context, rootNavigator: true).pop();
if (response.statusCode != 200) {
showMessagePopup(
context,
bgColor: Color(0XFFFDEDEE),
iconColor: Color(0XFFEE4D61),
textColor: Color(0XFF131C18),
content: translation.getTranslation('errorGetData'),
icons: Icons.info_rounded,
);
}
if (response.statusCode == 200) {
final decodedResponse = json.decode(response.body);
final mobileDeepLink = decodedResponse['data']['mobile_deep_link'];
final webPaymentUrl = decodedResponse['data']['web_payment_url'];
//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('msg_unable_to_open_payment'),
icons: Icons.info_rounded,
);
} else if (!kIsWeb) {
bool couldOpen = false;
// 1. Try mobileDeepLink natively
if (mobileDeepLink != null && mobileDeepLink.isNotEmpty) {
try {
final uri = Uri.parse(mobileDeepLink);
couldOpen =
await launchUrl(uri, mode: LaunchMode.externalApplication);
} catch (e) {
couldOpen = false; // Malformed URL or OS restriction
}
}
// 2. Fallback to webPaymentUrl in external browser if step 1 failed
if (!couldOpen && webPaymentUrl != null && webPaymentUrl.isNotEmpty) {
try {
final fallbackUri = Uri.parse(webPaymentUrl);
couldOpen = await launchUrl(fallbackUri,
mode: LaunchMode.externalApplication);
if (!couldOpen) {
couldOpen = await launchUrl(fallbackUri); // fallback to default
}
} catch (e) {
couldOpen = false;
}
}
// 3. Display Error logic if everywhere failed
if (!couldOpen) {
showMessagePopup(
context,
bgColor: Color(0XFFFDEDEE),
iconColor: Color(0XFFEE4D61),
textColor: Color(0XFF131C18),
content: translation.getTranslation('msg_unable_to_open_payment'),
icons: Icons.info_rounded,
);
}
} else if (UniversalPlatform.isWeb) {
html.window.open(webPaymentUrl, '_self');
}
} else {
print('Failed to call GenerateLink API: ${response.statusCode}');
}
}