import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_app/app/models/cart_line_item.dart'; import 'package:flutter_app/app/models/checkout_session.dart'; import 'package:flutter_app/app/models/customer_address.dart'; import 'package:flutter_app/bootstrap/app_helper.dart'; import 'package:flutter_app/bootstrap/helpers.dart'; import 'package:nylo_framework/nylo_framework.dart'; import 'dart:async'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:woosignal/models/response/woosignal_app.dart'; class PayPalCheckout extends StatefulWidget { final String? description; final String? amount; final List? cartLineItems; PayPalCheckout({this.description, this.amount, this.cartLineItems}); @override WebViewState createState() => WebViewState(); } class WebViewState extends NyState { final Completer _controller = Completer(); String? payerId = ''; int intCount = 0; StreamSubscription? _onUrlChanged; final WooSignalApp? _wooSignalApp = AppHelper.instance.appConfig; String? formCheckoutShippingAddress; setCheckoutShippingAddress(CustomerAddress customerAddress) { String tmp = ""; if (customerAddress.firstName != null) { tmp += '\n'; } if (customerAddress.lastName != null) { tmp += '\n'; } if (customerAddress.addressLine != null) { tmp += '\n'; } if (customerAddress.city != null) { tmp += '\n'; } if (customerAddress.customerCountry!.hasState() && customerAddress.customerCountry!.state!.name != null) { tmp += '\n'; } if (customerAddress.postalCode != null) { tmp += '\n'; } if (customerAddress.customerCountry!.countryCode != null) { tmp += '\n'; } formCheckoutShippingAddress = tmp; } String getPayPalItemName() { return truncateString( widget.description!.replaceAll(RegExp(r'[^\w\s]+'), ''), 124); } String getPayPalPaymentType() { return Platform.isAndroid ? "PayPal - Android App" : "PayPal - IOS App"; } String getPayPalUrl() { bool? liveMode = envVal('PAYPAL_LIVE_MODE', defaultValue: _wooSignalApp!.paypalLiveMode); return liveMode == true ? "https://www.paypal.com/cgi-bin/webscr" : "https://www.sandbox.paypal.com/cgi-bin/webscr"; } @override void initState() { super.initState(); if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); setCheckoutShippingAddress( CheckoutSession.getInstance.billingDetails!.shippingAddress!); setState(() {}); } @override void dispose() { if (_onUrlChanged != null) { _onUrlChanged!.cancel(); } super.dispose(); } String _loadHTML() { final String strProcessingPayment = trans("Processing Payment"); final String strPleaseWait = trans( "Please wait, your order is being processed and you will be redirected to the PayPal website."); final String strRedirectMessage = trans( "If you are not automatically redirected to PayPal within 5 seconds"); return ''' $strProcessingPayment...

$strPleaseWait

$formCheckoutShippingAddress


$strRedirectMessage...

''' .toString(); } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomInset: false, body: SafeArea( child: WebView( initialUrl: Uri.dataFromString(_loadHTML(), mimeType: 'text/html').toString(), javascriptMode: JavascriptMode.unrestricted, onWebViewCreated: (WebViewController webViewController) { _controller.complete(webViewController); }, onProgress: (int progress) {}, navigationDelegate: (NavigationRequest request) { return NavigationDecision.navigate; }, onPageStarted: (String url) {}, onPageFinished: (String url) { if (intCount > 0) { url = url.replaceAll("~", "_"); } intCount = intCount + 1; if (url.contains("payment_success")) { var uri = Uri.dataFromString(url); setState(() { payerId = uri.queryParameters['PayerID']; }); Navigator.pop(context, { "status": payerId == null ? "cancelled" : "success", "payerId": payerId }); } else if (url.contains("payment_failure")) { Navigator.pop(context, {"status": "cancelled"}); } }, gestureNavigationEnabled: false, ), ), ); } }