moved revenuecat

pull/1476/head
Brord van Wierst 1 year ago committed by ggurdin
parent 925d7506ef
commit 8c4619dfdc
No known key found for this signature in database
GPG Key ID: A01CB41737CBB478

@ -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<void> setAppIds() async {
Future<void> setAppIds(String accessToken) async {
if (appIds != null) return;
appIds = await SubscriptionRepo.getAppIds();
appIds = await SubscriptionRepo.getAppIds(accessToken);
}
Future<void> setAllProducts() async {
Future<void> setAllProducts(String accessToken) async {
if (allProducts != null) return;
allProducts = await SubscriptionRepo.getAllProducts();
allProducts = await SubscriptionRepo.getAllProducts(accessToken);
}
bool get isNewUserTrial =>

@ -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) {

@ -8,8 +8,9 @@ class WebSubscriptionInfo extends SubscriptionInfo {
@override
Future<void> configure() async {
await setAppIds();
await setAllProducts();
const accessToken = "";
await setAppIds(accessToken);
await setAllProducts(accessToken);
await setCustomerInfo();
if (allProducts == null || appIds == null) {

@ -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";

@ -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<SubscriptionAppIds?> getAppIds() async {
static Future<SubscriptionAppIds?> 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<List<SubscriptionDetails>?> getAllProducts() async {
static Future<List<SubscriptionDetails>?> 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<String, dynamic> json = jsonDecode(res.body);
final RCProductsResponseModel resp =
@ -89,26 +100,18 @@ class RCProductsResponseModel {
Map<String, dynamic> json,
) {
final List<dynamic> offerings = json["items"] as List<dynamic>;
final offering = offerings.firstWhereOrNull(
Environment.isStaging
? (offering) => !(offering['is_current'] as bool)
: (offering) => offering['is_current'] as bool,
);
final Map<String, dynamic> metadata = offering['metadata'];
final List<SubscriptionDetails> allProducts = [];
for (final packageDetails in offering['packages']['items']) {
final String packageId = packageDetails['id'];
final List<SubscriptionDetails> 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<SubscriptionDetails>();
return RCProductsResponseModel(allProducts: res);
}
static List<SubscriptionDetails> productsFromPackageDetails(

@ -22,24 +22,11 @@ class SubscriptionAppIds {
return null;
}
factory SubscriptionAppIds.fromJson(json) {
final SubscriptionAppIds appIds = SubscriptionAppIds();
for (final appInfo in (json['items'] as List<dynamic>)) {
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<String, dynamic> json) {
return SubscriptionAppIds()
..stripeId = json['stripe_id']
..androidId = json['android_id']
..appleId = json['apple_id'];
}
}

Loading…
Cancel
Save