Created
August 12, 2024 11:21
-
-
Save stevenosse/429ca452ee14a07db40696547d52564a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InAppPurchaseService { | |
static const premiumPermissionId = 'premium'; | |
static const androidLifetimeProductId = 'lifetime_access'; | |
static const androidMonthlyProductId = 'android_premium_monthly_3.99_30days_0:0'; | |
static const androidYearlyProductId = 'android_paper_premium_yearly_45_30days_0:0'; | |
static const iosLifetimeProductId = 'lifetime_access'; | |
static const iosMonthlyProductId = 'ios_premium_monthly_3.99_30days_0'; | |
static const iosYearlyProductId = 'ios_paper_premium_yearly_45_30days_0'; | |
static const androidProducts = [androidLifetimeProductId, androidMonthlyProductId, androidYearlyProductId]; | |
static const iosProducts = [iosLifetimeProductId, iosMonthlyProductId, iosYearlyProductId]; | |
static final subscriptionProducts = [ | |
if (defaultTargetPlatform == TargetPlatform.android) ...[ | |
androidMonthlyProductId, | |
androidYearlyProductId | |
] else ...[ | |
iosMonthlyProductId, | |
iosYearlyProductId | |
], | |
]; | |
static final lifetimeProductId = | |
defaultTargetPlatform == TargetPlatform.android ? androidLifetimeProductId : iosLifetimeProductId; | |
static final allProductIds = [ | |
...androidProducts, | |
...iosProducts, | |
lifetimeProductId, | |
]; | |
const InAppPurchaseService(); | |
Future<void> init() async { | |
try { | |
await Purchases.setLogLevel(LogLevel.debug); | |
late final PurchasesConfiguration purchasesConfiguration; | |
if (defaultTargetPlatform == TargetPlatform.android) { | |
purchasesConfiguration = PurchasesConfiguration(Environment.rcGooglePlayApiKey); | |
} else { | |
purchasesConfiguration = PurchasesConfiguration(Environment.rcAppStoreApiKey); | |
} | |
await Purchases.configure(purchasesConfiguration); | |
} catch (e, trace) { | |
log('Glassfy init failed', error: e); | |
Sentry.captureException(e, stackTrace: trace); | |
} | |
} | |
Future<Offerings?> getOfferings() async { | |
try { | |
final offerings = await Purchases.getOfferings(); | |
return offerings; | |
} catch (e, trace) { | |
Sentry.captureException(e, stackTrace: trace); | |
return null; | |
} | |
} | |
Future<String?> get currentSubscription async { | |
try { | |
CustomerInfo customerInfo = await Purchases.getCustomerInfo(); | |
return customerInfo.activeSubscriptions.firstOrNull; | |
} catch (e, trace) { | |
Sentry.captureException(e, stackTrace: trace); | |
rethrow; | |
} | |
} | |
Future<bool> hasActiveSubscription() async { | |
try { | |
final customerInfo = await Purchases.getCustomerInfo(); | |
return customerInfo.entitlements.all[premiumPermissionId]?.isActive == true; | |
} catch (e, trace) { | |
Sentry.captureException(e, stackTrace: trace); | |
rethrow; | |
} | |
} | |
Future<bool> paySubscription({required StoreProduct product}) async { | |
try { | |
CustomerInfo customerInfo = await Purchases.purchaseStoreProduct(product); | |
if (customerInfo.entitlements.all[premiumPermissionId]?.isActive == true) { | |
return true; | |
} | |
return false; | |
} on PlatformException catch (e) { | |
var errorCode = PurchasesErrorHelper.getErrorCode(e); | |
if (errorCode != PurchasesErrorCode.purchaseCancelledError) { | |
Sentry.captureException(e); | |
} | |
rethrow; | |
} | |
} | |
Future<bool> restore() async { | |
try { | |
final customerInfo = await Purchases.restorePurchases(); | |
return customerInfo.entitlements.all[premiumPermissionId]?.isActive == true; | |
} catch (e, trace) { | |
Sentry.captureException(e, stackTrace: trace); | |
rethrow; | |
} | |
} | |
Future<StoreProduct?> getProduct({String? id}) async { | |
try { | |
final products = await Purchases.getProducts(allProductIds); | |
return products.firstWhereOrNull((element) => element.identifier == id); | |
} catch (e, trace) { | |
Sentry.captureException(e, stackTrace: trace); | |
rethrow; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment