From f387dd99e126aefbaf089eeff72200be736548c1 Mon Sep 17 00:00:00 2001 From: Jigar-f Date: Tue, 12 Dec 2023 12:52:25 +0530 Subject: [PATCH] Added error handling. --- internalsdk/session_model.go | 3 +- lib/core/purchase/app_purchase.dart | 11 ++++ lib/plans/plan_details.dart | 89 +++++++++++++++++++---------- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/internalsdk/session_model.go b/internalsdk/session_model.go index 59c4db545..7c7728d6d 100644 --- a/internalsdk/session_model.go +++ b/internalsdk/session_model.go @@ -874,13 +874,12 @@ func redeemResellerCode(m *SessionModel, email string, resellerCode string) erro } func submitApplePayPayment(m *SessionModel, planId string, purchaseToken string) error { - log.Errorf("Submit Apple Pay Payment planId %v purchaseToken %v", planId, purchaseToken) + log.Debugf("Submit Apple Pay Payment planId %v purchaseToken %v", planId, purchaseToken) err, purchaseData := createPurchaseData(m, paymentProviderApplePay, "", purchaseToken, planId) if err != nil { log.Errorf("Error while creating purchase data %v", err) return err } - log.Errorf("Purchase data %v", purchaseData) deviecId, err := m.GetDeviceID() if err != nil { return err diff --git a/lib/core/purchase/app_purchase.dart b/lib/core/purchase/app_purchase.dart index a363b5af4..178ac2e13 100644 --- a/lib/core/purchase/app_purchase.dart +++ b/lib/core/purchase/app_purchase.dart @@ -44,6 +44,10 @@ class AppPurchase { } on PlatformException catch (e) { logger.e('Error while calling purchase api', error: e); Sentry.captureException(e); + _onError?.call(e); + } catch (e) { + logger.e('Payment failed', error: e); + _onError?.call(e); } } @@ -61,12 +65,18 @@ class AppPurchase { Future _onPurchaseUpdate( List purchaseDetailsList, ) async { + logger.i("_onPurchaseUpdate called with $purchaseDetailsList"); for (var purchaseDetails in purchaseDetailsList) { await _handlePurchase(purchaseDetails); } } Future _handlePurchase(PurchaseDetails purchaseDetails) async { + if (purchaseDetails.status == PurchaseStatus.canceled) { + // User has canceled the purchase + _onError?.call(purchaseDetails); + return; + } if (purchaseDetails.status == PurchaseStatus.purchased) { try { await sessionModel.submitApplePlay( @@ -77,6 +87,7 @@ class AppPurchase { } catch (e) { logger.e("purchase error", error: e); Sentry.captureException(e); + _onError?.call(e); } } if (purchaseDetails.pendingCompletePurchase) { diff --git a/lib/plans/plan_details.dart b/lib/plans/plan_details.dart index 47dbd97bf..16474a359 100644 --- a/lib/plans/plan_details.dart +++ b/lib/plans/plan_details.dart @@ -1,5 +1,6 @@ import 'package:lantern/common/common.dart'; import 'package:lantern/core/purchase/app_purchase.dart'; +import 'package:lantern/plans/utils.dart'; class PlanCard extends StatelessWidget { final Plan plan; @@ -122,39 +123,69 @@ class PlanCard extends StatelessWidget { Future onPlanTap(BuildContext context, String planName) async { if (Platform.isAndroid) { - final isPlayVersion = sessionModel.isPlayVersion.value ?? false; - final inRussia = sessionModel.country.value == 'RU'; - //If user is downloaded from Play store and !inRussia then - //Go with In App purchase - if (isPlayVersion && !inRussia) { - await sessionModel - .submitGooglePlay(planName) - .onError((error, stackTrace) { - // on failure - CDialog.showError( - context, - error: e, - stackTrace: stackTrace, - description: - (error as PlatformException).message ?? error.toString(), - ); - }); - } else { - // * Proceed to our own Checkout - await context.pushRoute( - Checkout( - plan: plan, - isPro: isPro, - ), + _proceedToAndroidCheckout(context, planName); + } else { + _proceedToCheckoutIOS(context); + } + } + + Future _proceedToAndroidCheckout( + BuildContext context, String planName) async { + final isPlayVersion = sessionModel.isPlayVersion.value ?? false; + final inRussia = sessionModel.country.value == 'RU'; + //If user is downloaded from Play store and !inRussia then + //Go with In App purchase + if (isPlayVersion && !inRussia) { + await sessionModel + .submitGooglePlay(planName) + .onError((error, stackTrace) { + // on failure + CDialog.showError( + context, + error: e, + stackTrace: stackTrace, + description: (error as PlatformException).message ?? error.toString(), ); - } + }); } else { - // * Proceed to IOSs - final appPurchase = sl(); + _proceedToCustomCheckout(context); + } + } + + Future _proceedToCustomCheckout(BuildContext context) async { + await context.pushRoute( + Checkout( + plan: plan, + isPro: isPro, + ), + ); + } + + void _proceedToCheckoutIOS(BuildContext context) { + final appPurchase = sl(); + try { + context.loaderOverlay.show(); appPurchase.startPurchase( plan.id, - onSuccess: () {}, - onFailure: (error) {}, + onSuccess: () { + context.loaderOverlay.hide(); + showSuccessDialog(context, isPro); + }, + onFailure: (error) { + context.loaderOverlay.hide(); + CDialog.showError( + context, + error: error, + description: error.toString(), + ); + }, + ); + } catch (e) { + context.loaderOverlay.hide(); + CDialog.showError( + context, + error: e, + description: e.toString(), ); } }