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 'dart:async'; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; import 'package:nylo_framework/helpers/helper.dart'; import 'package:nylo_framework/widgets/ny_state.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 flutterWebViewPlugin = new FlutterWebviewPlugin(); String payerId = ''; int intCount = 0; StreamSubscription _onUrlChanged; 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, 124); } String getPayPalPaymentType() { return Platform.isAndroid ? "PayPal - Android App" : "PayPal - IOS App"; } String getPayPalUrl() { bool liveMode = getEnv('PAYPAL_LIVE_MODE', defaultValue: false); return liveMode == true ? "https://www.paypal.com/cgi-bin/webscr" : "https://www.sandbox.paypal.com/cgi-bin/webscr"; } @override void initState() { super.initState(); setCheckoutShippingAddress( CheckoutSession.getInstance.billingDetails.shippingAddress); setState(() {}); _onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((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": "success", "payerId": payerId}); } else if (url.contains("payment_failure")) { Navigator.pop(context, {"status": "cancelled"}); } }); } @override void dispose() { _onUrlChanged.cancel(); flutterWebViewPlugin.dispose(); super.dispose(); } String _loadHTML() { return ''' ${trans(context, "Processing Payment")}...

${trans(context, "Please wait, your order is being processed and you will be redirected to the PayPal website.")}

$formCheckoutShippingAddress


${trans(context, "If you are not automatically redirected to PayPal within 5 seconds")}...

'''; } @override Widget build(BuildContext context) { return WebviewScaffold( url: Uri.dataFromString(_loadHTML(), mimeType: 'text/html').toString(), appBar: AppBar( centerTitle: true, automaticallyImplyLeading: false, title: Text( trans(context, "PayPal Checkout"), textAlign: TextAlign.center, ), ), ); } }