Sfoglia il codice sorgente

Handle active playstore/appstore subs & minor ui tweaks

Neeraj Gupta 4 anni fa
parent
commit
d3d80fd9a4

+ 2 - 0
lib/models/subscription.dart

@@ -2,6 +2,8 @@ import 'dart:convert';
 
 const kFreeProductID = "free";
 const kStripe = "stripe";
+const kAppStore = "appstore";
+const kPlayStore = "playstore";
 
 class Subscription {
   final int id;

+ 20 - 19
lib/ui/payment/payment_web_page.dart

@@ -14,8 +14,8 @@ import 'package:photos/utils/toast_util.dart';
 import '../loading_widget.dart';
 import '../progress_dialog.dart';
 
-const kMobilePaymentRedirect = "https://payments.ente.io/mobile";
-const kWebPaymentUrl = String.fromEnvironment("web-payment",
+const kWebPaymentRedirectUrl = "https://payments.ente.io/frameRedirect";
+const kWebPaymentBaseEndpoint = String.fromEnvironment("web-payment",
     defaultValue: "https://payments.ente.io");
 
 class PaymentWebPage extends StatefulWidget {
@@ -36,14 +36,15 @@ class _PaymentWebPage extends State<PaymentWebPage> {
   ProgressDialog _dialog;
   InAppWebViewController webView;
   double progress = 0;
-  String paymentWebToken;
-  String basePaymentUrl = kWebPaymentUrl;
+  Uri initPaymentUrl;
+  String basePaymentUrl = kWebPaymentBaseEndpoint;
 
   @override
   void initState() {
     userService.getPaymentToken().then((token) {
-      paymentWebToken = token;
-      setState(() {});
+      initPaymentUrl = _getPaymentUrl(token);
+      setState(() {
+      });
     });
     if (Platform.isAndroid && kDebugMode) {
       AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
@@ -52,16 +53,15 @@ class _PaymentWebPage extends State<PaymentWebPage> {
     super.initState();
   }
 
-  Uri _getPaymentUrl(String baseEndpoint, String productId, String paymentToken,
-      String actionType, String redirectUrl) {
+  Uri _getPaymentUrl(String paymentToken) {
     final queryParameters = {
-      'productID': productId,
+      'productID': widget.planId,
       'paymentToken': paymentToken,
-      'action': actionType,
-      'redirectURL': redirectUrl,
+      'action': widget.actionType,
+      'redirectURL': kWebPaymentRedirectUrl,
     };
-    var tryParse = Uri.tryParse(baseEndpoint);
-    if (kDebugMode && baseEndpoint.startsWith("http://")) {
+    var tryParse = Uri.tryParse(kWebPaymentBaseEndpoint);
+    if (kDebugMode && kWebPaymentBaseEndpoint.startsWith("http://")) {
       return Uri.http(tryParse.authority, tryParse.path, queryParameters);
     } else {
       return Uri.https(tryParse.authority, tryParse.path, queryParameters);
@@ -70,12 +70,9 @@ class _PaymentWebPage extends State<PaymentWebPage> {
 
   @override
   Widget build(BuildContext context) {
-    if (paymentWebToken == null) {
+    if (initPaymentUrl == null) {
       return loadWidget;
     }
-    Uri paymentUri = _getPaymentUrl(basePaymentUrl, widget.planId,
-        paymentWebToken, widget.actionType, kMobilePaymentRedirect);
-    _logger.info("paymentUrl : $paymentUri");
     return WillPopScope(
         onWillPop: () async => _buildPageExitWidget(context),
         child: Scaffold(
@@ -89,7 +86,7 @@ class _PaymentWebPage extends State<PaymentWebPage> {
                   : Container(),
               Expanded(
                 child: InAppWebView(
-                  initialUrlRequest: URLRequest(url: paymentUri),
+                  initialUrlRequest: URLRequest(url: initPaymentUrl),
                   onProgressChanged:
                       (InAppWebViewController controller, int progress) {
                     setState(() {
@@ -125,7 +122,11 @@ class _PaymentWebPage extends State<PaymentWebPage> {
                       await _dialog.hide();
                     }
                   },
+                  onLoadHttpError: (controller, navigationAction, code, msg) async {
+                    _logger.info("onHttpError with $code and msg = $msg");
+                  },
                   onLoadStop: (controller, navigationAction) async {
+                    _logger.info("loadStart" + navigationAction.toString());
                     if (_dialog.isShowing()) {
                       await _dialog.hide();
                     }
@@ -160,7 +161,7 @@ class _PaymentWebPage extends State<PaymentWebPage> {
   }
 
   bool _isPaymentActionComplete(Uri loadingUri) {
-    return loadingUri.toString().startsWith(kMobilePaymentRedirect);
+    return loadingUri.toString().startsWith(kWebPaymentRedirectUrl);
   }
 
   Future<void> handlePaymentResponse(Uri uri) async {

+ 26 - 25
lib/ui/payment/stripe_subscription_page.dart

@@ -20,8 +20,6 @@ import 'package:photos/ui/payment/skip_subscription_widget.dart';
 import 'package:photos/ui/payment/subscription_common_widgets.dart';
 import 'package:photos/ui/payment/subscription_plan_widget.dart';
 import 'package:photos/ui/progress_dialog.dart';
-import 'package:photos/utils/data_util.dart';
-import 'package:photos/utils/date_time_util.dart';
 import 'package:photos/utils/dialog_util.dart';
 import 'package:photos/utils/toast_util.dart';
 import 'package:url_launcher/url_launcher.dart';
@@ -57,7 +55,6 @@ class _StripeSubscriptionPageState extends State<StripeSubscriptionPage> {
 
   @override
   void initState() {
-    _billingService.setIsOnSubscriptionPage(true);
     _fetchSub();
     _dialog = createProgressDialog(context, "please wait...");
     super.initState();
@@ -152,7 +149,7 @@ class _StripeSubscriptionPageState extends State<StripeSubscriptionPage> {
       widgets.add(ValidityWidget(currentSubscription: _currentSubscription));
     }
 
-    if ( _currentSubscription.productID == kFreeProductID) {
+    if (_currentSubscription.productID == kFreeProductID) {
       if (widget.isOnboarding) {
         widgets.add(SkipSubscriptionWidget(freePlan: _freePlan));
       }
@@ -170,17 +167,22 @@ class _StripeSubscriptionPageState extends State<StripeSubscriptionPage> {
           alignment: Alignment.center,
           child: GestureDetector(
             onTap: () async {
-              if (_isActiveStripeSubscriber) {
+              switch (_currentSubscription.paymentProvider) {
+                case kStripe:
                   await _launchStripePortal();
-                  return;
-              }
-              if (Platform.isAndroid) {
-                launch(
-                    "https://play.google.com/store/account/subscriptions?sku=" +
-                        _currentSubscription.productID +
-                        "&package=io.ente.photos");
-              } else {
-                launch("https://apps.apple.com/account/billing");
+                  break;
+                case kPlayStore:
+                  launch(
+                      "https://play.google.com/store/account/subscriptions?sku=" +
+                          _currentSubscription.productID +
+                          "&package=io.ente.photos");
+                  break;
+                case kAppStore:
+                  launch("https://apps.apple.com/account/billing");
+                  break;
+                default:
+                  _logger.severe(
+                      "unexpected payment provider ", _currentSubscription);
               }
             },
             child: Container(
@@ -189,13 +191,13 @@ class _StripeSubscriptionPageState extends State<StripeSubscriptionPage> {
                 children: [
                   RichText(
                     text: TextSpan(
-                      text: _isActiveStripeSubscriber
-                          ? "visit web.ente.io to manage your subscription"
+                      text: !_isActiveStripeSubscriber
+                          ? "visit ${_currentSubscription.paymentProvider} to manage your subscription"
                           : "payment details",
                       style: TextStyle(
                         color: _isActiveStripeSubscriber
-                            ? Colors.white
-                            : Colors.blue,
+                            ? Colors.blue
+                            : Colors.white,
                         fontFamily: 'Ubuntu',
                         fontSize: 15,
                       ),
@@ -239,19 +241,18 @@ class _StripeSubscriptionPageState extends State<StripeSubscriptionPage> {
   Widget _stripeRenewOrCancelButton() {
     bool isRenewCancelled =
         _currentSubscription.attributes?.isCancelled ?? false;
+    String title =
+        isRenewCancelled ? "renew subscription" : "cancel subscription";
     return TextButton(
       child: Text(
-        isRenewCancelled ? "renew subscription" : "cancel subscription",
+        title,
         style: TextStyle(
-          color: isRenewCancelled ? Colors.greenAccent : Colors.redAccent,
+          color: (isRenewCancelled ? Colors.greenAccent : Colors.white)
+          .withOpacity(isRenewCancelled ? 1.0: 0.4),
         ),
       ),
       onPressed: () async {
-        var result = await showChoiceDialog(
-            context,
-            isRenewCancelled
-                ? 'subscription renewal'
-                : 'subscription cancellation',
+        var result = await showChoiceDialog(context, title,
             isRenewCancelled
                 ? 'are you sure you want to renew?'
                 : 'are you sure you want to cancel?',

+ 1 - 1
lib/ui/payment/subscription.dart

@@ -4,7 +4,7 @@ import 'package:photos/ui/payment/stripe_subscription_page.dart';
 import 'package:photos/ui/payment/subscription_page.dart';
 
 StatefulWidget getSubscriptionPage({bool isOnBoarding = false}) {
-  if (!UpdateService.instance.isIndependentFlavor()) {
+  if (UpdateService.instance.isIndependentFlavor()) {
     return StripeSubscriptionPage(isOnboarding: isOnBoarding);
   } else {
     return SubscriptionPage(isOnboarding: isOnBoarding);