purchase method
Implementation
Future<void> purchase(ProductCommon product) async {
try {
final iap = FlutterInappPurchase.instance;
// 🎯 1. Handle In-App (one-time) products
if (product.type == ProductType.InApp) {
final requestProps = RequestPurchaseProps.inApp((
ios: RequestPurchaseIosProps(sku: product.id),
android: RequestPurchaseAndroidProps(
skus: [product.id],
obfuscatedAccountIdAndroid: "user_id", // optional
obfuscatedProfileIdAndroid: "profile_id", // optional
),
useAlternativeBilling: null,
));
await iap.requestPurchase(requestProps);
return;
}
// 💳 2. Handle Subscriptions
if (product.type == ProductType.Subs) {
if (Platform.isAndroid) {
final offers = _getAndroidOffers(product);
final requestProps = RequestPurchaseProps.subs((
ios: null,
android: RequestSubscriptionAndroidProps(
skus: [product.id],
subscriptionOffers: offers.isNotEmpty ? offers : null,
),
useAlternativeBilling: null,
));
await iap.requestPurchase(requestProps);
} else if (Platform.isIOS) {
final requestProps = RequestPurchaseProps.subs((
ios: RequestSubscriptionIosProps(
sku: product.id,
),
android: null,
useAlternativeBilling: null,
));
await iap.requestPurchase(requestProps);
}
}
} catch (e) {
if (kDebugMode) {
print("❌ Failed to purchase: $e");
}
}
}