This repository demonstrates how to integrate in-app subscriptions in a Flutter application using the in_app_purchase
package. The guide includes step-by-step instructions for setting up in-app subscriptions for both Google Play Store and Apple App Store, along with introductory offers.
- Subscription handling for both iOS and Android.
- Fetching products and subscription plans from stores.
- Managing purchases and handling billing.
- Handling introductory offers on both stores.
- Flutter SDK: Make sure Flutter is installed. Installation Guide
- Google Play Console: To configure in-app purchases for Android.
- App Store Connect: To configure in-app purchases for iOS.
In your pubspec.yaml
, add the following dependencies:
dependencies:
flutter:
sdk: flutter
in_app_purchase: ^3.0.6
- In Xcode, go to Signing & Capabilities and enable and create product In-App Subscription.
- Ensure that your app’s bundle ID matches the one registered in App Store Connect.
- Set up your In-App Purchase items in App Store Connect.
For local testing, you can enable StoreKit testing using Xcode:
- Open the
ios
project in Xcode. - In the Scheme Editor, select Run, then go to Options.
- Under StoreKit Configuration, select the StoreKit configuration file for testing.
-
Open
android/app/build.gradle
and ensure you have:def billing_version = "5.0.0"
-
Add the following permission in
AndroidManifest.xml
:<uses-permission android:name="com.android.vending.BILLING" />
-
Set up In-App Purchase products in the Google Play Console under Monetize > Products > Subscriptions.
Add the initialization code to your app:
import 'package:flutter/material.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
InAppPurchase.instance.isAvailable().then((isAvailable) {
if (isAvailable) {
// Proceed with fetching and displaying products
} else {
// Handle store unavailability
}
});
runApp(MyApp());
}
Future<void> fetchSubscriptions() async {
final bool available = await InAppPurchase.instance.isAvailable();
if (!available) {
// Handle store not available
return;
}
const Set<String> _kIds = <String>{'subscription_id_1', 'subscription_id_2'};
final ProductDetailsResponse response = await InAppPurchase.instance.queryProductDetails(_kIds);
if (response.notFoundIDs.isNotEmpty) {
// Handle missing products
}
final List<ProductDetails> products = response.productDetails;
// Process and display the available subscriptions
}
void handlePurchaseUpdates() {
final Stream<List<PurchaseDetails>> purchaseUpdates = InAppPurchase.instance.purchaseStream;
purchaseUpdates.listen((purchases) {
for (PurchaseDetails purchase in purchases) {
if (purchase.status == PurchaseStatus.pending) {
// Handle pending purchase
} else if (purchase.status == PurchaseStatus.purchased) {
// Verify purchase and grant entitlement
InAppPurchase.instance.completePurchase(purchase);
} else if (purchase.status == PurchaseStatus.error) {
// Handle purchase error
}
}
});
}
- Log in to App Store Connect.
- Navigate to My Apps > Your App > Features > In-App Purchases.
- Create a new subscription or select an existing one.
- Scroll down to Subscription Prices and click Add Introductory Offer.
- Choose the introductory offer type (free trial, pay-as-you-go, pay-up-front), and set the pricing details.
- Log in to the Google Play Console.
- Navigate to Monetize > Products > Subscriptions.
- Select your subscription product.
- In the Introductory pricing section, click Add.
- Set up the introductory pricing duration and price.
This project is licensed under the MIT License. See the LICENSE file for details.