From 2223c4103e497212f9b8b3cb65ee53970d69a928 Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Fri, 29 Jan 2021 17:12:15 +0530 Subject: [PATCH] Check for updates to subscription if it is locally expired --- lib/services/billing_service.dart | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/services/billing_service.dart b/lib/services/billing_service.dart index 4e22a2bf0..e6a3ea9ef 100644 --- a/lib/services/billing_service.dart +++ b/lib/services/billing_service.dart @@ -45,6 +45,9 @@ class BillingService { } } }); + if (_config.hasConfiguredAccount() && !hasActiveSubscription()) { + fetchSubscription(); + } } Future> getBillingPlans() { @@ -86,7 +89,24 @@ class BillingService { } } - // TODO: Fetch new subscription once the current one has expired? + Future fetchSubscription() async { + try { + final response = await _dio.get( + _config.getHttpEndpoint() + "/billing/subscription", + options: Options( + headers: { + "X-Auth-Token": _config.getToken(), + }, + ), + ); + final subscription = Subscription.fromMap(response.data["subscription"]); + await setSubscription(subscription); + return subscription; + } catch (e) { + throw e; + } + } + Subscription getSubscription() { final jsonValue = _prefs.getString(subscriptionKey); if (jsonValue == null) {