From 8c4619dfdcba22488bf208c0b55df16728172ca8 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Tue, 3 Sep 2024 12:53:05 +0200 Subject: [PATCH] moved revenuecat --- lib/pangea/models/base_subscription_info.dart | 8 +-- lib/pangea/models/mobile_subscriptions.dart | 5 +- lib/pangea/models/web_subscriptions.dart | 5 +- lib/pangea/network/urls.dart | 14 ++-- lib/pangea/repo/subscription_repo.dart | 71 ++++++++++--------- lib/pangea/utils/subscription_app_id.dart | 23 ++---- 6 files changed, 58 insertions(+), 68 deletions(-) diff --git a/lib/pangea/models/base_subscription_info.dart b/lib/pangea/models/base_subscription_info.dart index 469ac5786..8792284e6 100644 --- a/lib/pangea/models/base_subscription_info.dart +++ b/lib/pangea/models/base_subscription_info.dart @@ -36,14 +36,14 @@ class SubscriptionInfo { Fetch App Ids for each RC app (iOS, Android, and Stripe). Used to determine which app a user with an active subscription purchased that subscription. */ - Future setAppIds() async { + Future setAppIds(String accessToken) async { if (appIds != null) return; - appIds = await SubscriptionRepo.getAppIds(); + appIds = await SubscriptionRepo.getAppIds(accessToken); } - Future setAllProducts() async { + Future setAllProducts(String accessToken) async { if (allProducts != null) return; - allProducts = await SubscriptionRepo.getAllProducts(); + allProducts = await SubscriptionRepo.getAllProducts(accessToken); } bool get isNewUserTrial => diff --git a/lib/pangea/models/mobile_subscriptions.dart b/lib/pangea/models/mobile_subscriptions.dart index 00fc6e3fd..d3c58ec82 100644 --- a/lib/pangea/models/mobile_subscriptions.dart +++ b/lib/pangea/models/mobile_subscriptions.dart @@ -31,8 +31,9 @@ class MobileSubscriptionInfo extends SubscriptionInfo { ); return; } - await setAppIds(); - await setAllProducts(); + const accessToken = ""; + await setAppIds(accessToken); + await setAllProducts(accessToken); await setCustomerInfo(); await setMobilePackages(); if (allProducts != null && appIds != null) { diff --git a/lib/pangea/models/web_subscriptions.dart b/lib/pangea/models/web_subscriptions.dart index 1a6cc722a..d70d08c7c 100644 --- a/lib/pangea/models/web_subscriptions.dart +++ b/lib/pangea/models/web_subscriptions.dart @@ -8,8 +8,9 @@ class WebSubscriptionInfo extends SubscriptionInfo { @override Future configure() async { - await setAppIds(); - await setAllProducts(); + const accessToken = ""; + await setAppIds(accessToken); + await setAllProducts(accessToken); await setCustomerInfo(); if (allProducts == null || appIds == null) { diff --git a/lib/pangea/network/urls.dart b/lib/pangea/network/urls.dart index 9a8421d83..41fae28cd 100644 --- a/lib/pangea/network/urls.dart +++ b/lib/pangea/network/urls.dart @@ -12,7 +12,7 @@ class PApiUrls { static String baseAPI = Environment.baseAPI; /// ---------------------- Languages -------------------------------------- - static String getLanguages = "/languages"; + static String getLanguages = "${Environment.choreoApi}/choreo/languages"; /// ---------------------- Users -------------------------------------- static String createUser = "/account/create"; @@ -56,11 +56,9 @@ class PApiUrls { "${Environment.choreoApi}/practice/message"; ///-------------------------------- revenue cat -------------------------- - static String rcApiV1 = "https://api.revenuecat.com/v1"; - static String rcApiV2 = - "https://api.revenuecat.com/v2/projects/${Environment.rcProjectId}"; - static String rcApps = "$rcApiV2/apps"; - static String rcProducts = "$rcApiV2/offerings?expand=items.package.product"; - static String rcSubscribers = "$rcApiV1/subscribers"; -} + static String rcAppsChoreo = "${Environment.choreoApi}/revenue/app_ids"; + static String rcProductsChoreo = + "${Environment.choreoApi}/revenue/all_products"; + static String rcSubscriptionChoreo = + "${Environment.choreoApi}/revenue/subscription"; \ No newline at end of file diff --git a/lib/pangea/repo/subscription_repo.dart b/lib/pangea/repo/subscription_repo.dart index 16ea60c4d..f791ce8c3 100644 --- a/lib/pangea/repo/subscription_repo.dart +++ b/lib/pangea/repo/subscription_repo.dart @@ -1,14 +1,15 @@ import 'dart:convert'; -import 'package:flutter/material.dart'; - import 'package:collection/collection.dart'; -import 'package:http/http.dart' as http; - import 'package:fluffychat/pangea/config/environment.dart'; import 'package:fluffychat/pangea/controllers/subscription_controller.dart'; +import 'package:fluffychat/pangea/network/requests.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/subscription_app_id.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; + import '../network/urls.dart'; class SubscriptionRepo { @@ -18,14 +19,19 @@ class SubscriptionRepo { 'Authorization': 'Bearer ${Environment.rcKey}', }; - static Future getAppIds() async { + static Future getAppIds(String accessToken) async { try { - final http.Response res = await http.get( - Uri.parse(PApiUrls.rcApps), - headers: SubscriptionRepo.requestHeaders, + final Requests req = Requests( + choreoApiKey: Environment.choreoApiKey, + accessToken: accessToken, + ); + final http.Response res = await req.get( + url: PApiUrls.rcAppsChoreo, + ); + + return SubscriptionAppIds.fromJson( + jsonDecode(res.body), ); - final json = jsonDecode(res.body); - return SubscriptionAppIds.fromJson(json); } catch (err) { ErrorHandler.logError( m: "Failed to fetch app information for revenuecat API", @@ -35,11 +41,16 @@ class SubscriptionRepo { } } - static Future?> getAllProducts() async { + static Future?> getAllProducts( + String accessToken, + ) async { try { - final http.Response res = await http.get( - Uri.parse(PApiUrls.rcProducts), - headers: SubscriptionRepo.requestHeaders, + final Requests req = Requests( + choreoApiKey: Environment.choreoApiKey, + accessToken: accessToken, + ); + final http.Response res = await req.get( + url: PApiUrls.rcProductsChoreo, ); final Map json = jsonDecode(res.body); final RCProductsResponseModel resp = @@ -89,26 +100,18 @@ class RCProductsResponseModel { Map json, ) { final List offerings = json["items"] as List; - final offering = offerings.firstWhereOrNull( - Environment.isStaging - ? (offering) => !(offering['is_current'] as bool) - : (offering) => offering['is_current'] as bool, - ); - final Map metadata = offering['metadata']; - - final List allProducts = []; - for (final packageDetails in offering['packages']['items']) { - final String packageId = packageDetails['id']; - final List products = - RCProductsResponseModel.productsFromPackageDetails( - packageDetails, - packageId, - metadata, - ); - allProducts.addAll(products); - } - - return RCProductsResponseModel(allProducts: allProducts); + final res = offerings + .map( + (offering) => SubscriptionDetails( + price: offering['price'], + duration: offering['duration'], + id: offering['id'], + appId: offering['appId'], + ), + ) + .toList() + .cast(); + return RCProductsResponseModel(allProducts: res); } static List productsFromPackageDetails( diff --git a/lib/pangea/utils/subscription_app_id.dart b/lib/pangea/utils/subscription_app_id.dart index 20a69d4a2..362e0d16a 100644 --- a/lib/pangea/utils/subscription_app_id.dart +++ b/lib/pangea/utils/subscription_app_id.dart @@ -22,24 +22,11 @@ class SubscriptionAppIds { return null; } - factory SubscriptionAppIds.fromJson(json) { - final SubscriptionAppIds appIds = SubscriptionAppIds(); - for (final appInfo in (json['items'] as List)) { - final String platform = appInfo['type']; - final String appId = appInfo['id']; - switch (platform) { - case 'stripe': - appIds.stripeId = appId; - continue; - case 'app_store': - appIds.appleId = appId; - continue; - case 'play_store': - appIds.androidId = appId; - continue; - } - } - return appIds; + factory SubscriptionAppIds.fromJson(Map json) { + return SubscriptionAppIds() + ..stripeId = json['stripe_id'] + ..androidId = json['android_id'] + ..appleId = json['apple_id']; } }