Skip to content

Instantly share code, notes, and snippets.

@wattanakorn495
Created February 24, 2023 10:42
Show Gist options
  • Save wattanakorn495/1cc40f40ee80e02947dc9924b1e29720 to your computer and use it in GitHub Desktop.
Save wattanakorn495/1cc40f40ee80e02947dc9924b1e29720 to your computer and use it in GitHub Desktop.
topup webview
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../api/get_api.dart';
import '../../config.dart';
import '../../widgets/custom_dialog.dart';
import 'package:webview_flutter/webview_flutter.dart';
class TopupCreditCardsOTP extends StatefulWidget {
const TopupCreditCardsOTP(
{Key? key, this.url, this.fromTopup = false, required this.amount})
: super(key: key);
final String? url;
final bool fromTopup;
final String amount;
@override
State<TopupCreditCardsOTP> createState() => TopupCreditCardsOTPState();
}
class TopupCreditCardsOTPState extends State<TopupCreditCardsOTP> {
String? url;
bool? verify;
String creditURL = '$gatewayGBWallet/credit_cards/callback_otp';
String deepLink = 'https://app.villamarket.com/topup/bill';
bool hideWebView = false;
bool isFailed = false;
@override
void initState() {
super.initState();
url = widget.url;
}
@override
void dispose() {
GetAPI.creditCards();
super.dispose();
}
@override
Widget build(BuildContext context) {
final Completer<WebViewController> controller =
Completer<WebViewController>();
return Stack(
children: [
Image.asset(
'assets/images/new_bg.png',
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
fit: BoxFit.fill,
),
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: Text(
'Confirm Topup',
style: TextStyle(color: Theme.of(context).primaryColor),
),
),
body: hideWebView
? Container()
: WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: url,
debuggingEnabled: true,
onWebViewCreated: (WebViewController webViewController) {
controller.complete(webViewController);
},
onPageStarted: (String url) async {
url = url.replaceAll("amp;", "");
print("raw url: ${url}");
Uri? u = Uri.tryParse(url);
if (u != null) {
// handle Uri
print("host: ${u.host} ${u.origin}");
print("query: ${u.queryParameters}");
if (url.contains(deepLink)) {
print(
"resultCode: ${u.queryParameters["resultCode"]} ${u.queryParameters["resultCode"] == '00'}");
switch (u.queryParameters["resultCode"]) {
case "00":
{
// success
if (mounted) {
setState(() {
hideWebView = true;
});
await Get.dialog(
CustomDialog(
title: 'Top_up_successfully'.tr,
content:
'${'you_topup_for'.tr} ${widget.amount} ${'baht'.tr}',
exclamation: false,
),
barrierDismissible: false,
);
await GetAPI.balance();
Navigator.popUntil(
context, ModalRoute.withName("/Menu"));
}
}
break;
default:
{
// something error
switch (u.queryParameters["resultMessage"]) {
case "":
case null:
// user cancel
print("user cancel");
Get.back();
break;
default:
{
print(
"topup via credit card error with resultMessage, hide webview and show something went wrong dialog");
if (mounted) {
setState(() {
hideWebView = true;
});
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return CustomDialog(
title: 'Something_went_wrong'.tr,
content: u.queryParameters[
"resultMessage"] ??
"Please try again later".tr,
avatar: false,
onPressedConfirm: () {
Navigator.popUntil(context,
ModalRoute.withName("/Menu"));
},
);
},
);
}
}
}
}
}
// if (u.queryParameters["resultMessage"] != null) {
// // has result message
// print(
// "topup via credit card error with resultMessage, hide webview and show something went wrong dialog");
// if (mounted) {
// setState(() {
// hideWebView = true;
// });
// showDialog(
// barrierDismissible: false,
// context: context,
// builder: (context) {
// return CustomDialog(
// title: 'Something_went_wrong'.tr,
// content: u.queryParameters["resultMessage"] ??
// "Please try again later".tr,
// avatar: false,
// );
// },
// );
// }
// } else {
// print(
// "topup via credit card success, hide webview and show dialog");
// if (u.queryParameters["resultCode"] != null) {
// if (u.queryParameters["resultCode"] == "00") {
// // success
// if (mounted) {
// setState(() {
// hideWebView = true;
// });
// await Get.dialog(
// CustomDialog(
// title: 'Top_up_successfully'.tr,
// content:
// '${'you_topup_for'.tr} ${widget.amount} ${'baht'.tr}',
// exclamation: false,
// ),
// barrierDismissible: false,
// );
// await GetAPI.balance();
// Navigator.popUntil(
// context, ModalRoute.withName("/Menu"));
// }
// }
// }
// }
}
}
},
onPageFinished: (String url) {
debugPrint('Page finished loading: $url');
},
gestureNavigationEnabled: true,
),
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment