initializeWebView method
Future<void>
initializeWebView(
)
Implementation
Future<void> initializeWebView() async {
String autoSubmitHtml = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Auto Submit Payment</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
font-family: Arial, sans-serif;
}
#paymentIframe {
display: none;
width: 100vw;
height: calc(100vh - 30px);
margin-top: 45px;
border: none;
}
</style>
<script>
async function submitForm() {
const formData = {
ApplicationID: "${transactionInfoResponse?.applicationID}",
Currency: "USD",
MerchantReference: "${transactionInfoResponse?.reference}",
ReturnUrl: "$returnUrl",
Amount: ${(double.parse(_paymentModel.amount).round() * 100)},
PAN: "${_paymentModel.cardNumber}",
ExpiryDate: "${_paymentModel.expiryDate}",
CardSecurityCode: "${_paymentModel.cvv}"
};
try {
const form = document.createElement("form");
form.method = "POST";
form.action = "$actionUrl";
form.target = "paymentIframe";
Object.keys(formData).forEach((key) => {
const input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = formData[key];
form.appendChild(input);
});
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
const iframe = document.getElementById("paymentIframe");
iframe.style.display = "block";
} catch (err) {
document.body.innerHTML += "<p>Error: " + err + "</p>";
}
}
window.onload = submitForm;
</script>
</head>
<body>
<iframe id="paymentIframe" name="paymentIframe"></iframe>
</body>
</html>
''';
webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..enableZoom(true)
..setNavigationDelegate(
NavigationDelegate(
onNavigationRequest: (NavigationRequest request) async {
debugPrint("Navigating to: ${request.url}");
if (request.url.contains("paymentProcessing")) {
await trackPaymentRequest();
return NavigationDecision.prevent; // 🚫 Stop navigation
// No need for await unless required
}else if(request.url.contains("paymentProcessing")){
await trackPaymentRequest();
return NavigationDecision.prevent; // 🚫 Stop navigation
// No need for await unless required
}
return NavigationDecision.navigate;
},
onPageStarted: (String url) {
debugPrint("Page started loading: $url");
},
onPageFinished: (String url) {
debugPrint("Page finished loading: $url");
},
onWebResourceError: (WebResourceError error) {
debugPrint("Error: ${error.description}");
},
),
)
..loadHtmlString(autoSubmitHtml);
isWebView = true;
rebuildUi();
}