How To Add In App Purchases (IAP) in Flutter
In App Purchases (IAP) are an essential part of many mobile applications. They are used to offer extra content or features to users in exchange for a payment. Flutter is a popular mobile app development framework that has many built-in features for creating IAPs. In this tutorial, we will show you how to add In App Purchases to your Flutter project.
Setting up the Flutter Project
Before you can add In App Purchases to your Flutter project, you will need to set up the project. This includes installing the Flutter SDK, setting up a Flutter project, and adding the necessary packages.
Installing the Flutter SDK
The first step is to install the Flutter SDK. You can find the installation instructions here.
Setting up a Flutter Project
Once you have installed the Flutter SDK, you can create a new project by running the following command in your terminal:
flutter create my_project
This will create a new Flutter project in the my_project
directory.
Adding the Necessary Packages
Next, you will need to add the necessary packages to your project. You can do this by running the following command in the my_project
directory:
flutter pub get
This will install all the necessary packages needed for your project.
Installing in_app_purchase
The next step is to install the in_app_purchase
package. This package provides all the necessary tools for adding In App Purchases to your Flutter project. You can install the package by running the following command in the my_project
directory:
flutter pub add in_app_purchase
Building the In App Purchases UI
Once you have installed the in_app_purchase
package, you can start building the In App Purchases UI. This includes creating the necessary screens and buttons for allowing users to purchase items.
You can create a PurchaseScreen
widget to display the list of items available for purchase. This widget should include a ListView
for displaying the items, and a RaisedButton
for allowing users to purchase the item.
class PurchaseScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('In App Purchases'),
),
body: ListView(
children: <Widget>[
ListTile(
title: Text('Item 1'),
trailing: RaisedButton(
child: Text('Purchase'),
onPressed: () {
// Handle purchase here
},
),
),
ListTile(
title: Text('Item 2'),
trailing: RaisedButton(
child: Text('Purchase'),
onPressed: () {
// Handle purchase here
},
),
),
],
),
);
}
}
Adding In App Purchases to the Flutter Project
Now that you have created the UI for the In App Purchases, you can start adding the necessary code for handling the purchases. This includes setting up the necessary callbacks and connecting to the payment gateway.
Setting up the Callbacks
The first step is to set up the necessary callbacks for handling the purchases. This includes the onPurchaseSuccess
and onPurchaseError
callbacks. These callbacks will be called when the purchase is successful or fails respectively.
void onPurchaseSuccess(PurchaseDetails purchaseDetails) {
// Handle successful purchase
}
void onPurchaseError(PurchaseError purchaseError) {
// Handle purchase error
}
Connecting to the Payment Gateway
The next step is to connect to the payment gateway. You can use the InAppPurchaseConnection
class to connect to the payment gateway. This class provides all the necessary methods for connecting and querying the payment gateway.
InAppPurchaseConnection _iap = InAppPurchaseConnection.instance;
Future<void> connectToPaymentGateway() async {
bool isAvailable = await _iap.isAvailable();
if (!isAvailable) {
throw PlatformException(
code: 'not_available',
message: 'The payment gateway is not available');
}
_iap.connect();
}
Handling Different Purchase Types in Flutter
Once you have connected to the payment gateway, you can start handling different types of purchases. This includes one-time purchases, subscription purchases, and consumable purchases.
One-Time Purchases
One-time purchases are purchases that are made once and do not require any further payments. To make a one-time purchase, you can use the buyNonConsumable
method. This method takes the product identifier as an argument and returns a PurchaseDetails
object.
Future<PurchaseDetails> buyNonConsumable(String productId) async {
PurchaseDetails purchaseDetails =
await _iap.buyNonConsumable(productId);
return purchaseDetails;
}
Subscription Purchases
Subscription purchases are purchases that require recurring payments. To make a subscription purchase, you can use the buySubscription
method. This method takes the product identifier and the subscription period as arguments and returns a PurchaseDetails
object.
Future<PurchaseDetails> buySubscription(
String productId, Duration period) async {
PurchaseDetails purchaseDetails =
await _iap.buySubscription(productId, period);
return purchaseDetails;
}
Consumable Purchases
Consumable purchases are purchases that can be used multiple times. To make a consumable purchase, you can use the buyConsumable
method. This method takes the product identifier and the number of items to purchase as arguments and returns a PurchaseDetails
object.
Future<PurchaseDetails> buyConsumable(
String productId, int quantity) async {
PurchaseDetails purchaseDetails =
await _iap.buyConsumable(productId, quantity);
return purchaseDetails;
}
Consuming In App Purchases in Flutter
Once a user has purchased an item, you will need to consume it. This is necessary for consumable purchases, as they can be used multiple times. To consume a purchase, you can use the consumePurchase
method. This method takes the purchase token as an argument and returns a PurchaseDetails
object.
Future<PurchaseDetails> consumePurchase(String purchaseToken) async {
PurchaseDetails purchaseDetails =
await _iap.consumePurchase(purchaseToken);
return purchaseDetails;
}
Restoring In App Purchases in Flutter
You may also need to restore In App Purchases if a user has deleted and reinstalled the app. To restore purchases, you can use the restorePurchases
method. This method takes no arguments and returns a list of PurchaseDetails
objects.
Future<List<PurchaseDetails>> restorePurchases() async {
List<PurchaseDetails> purchaseDetailsList =
await _iap.restorePurchases();
return purchaseDetailsList;
}
Running the Flutter Project
Once you have added In App Purchases to your Flutter project, you can run the project by running the following command in the my_project
directory:
flutter run
This will build the app and launch it on your device or emulator. You can then test the In App Purchases by making purchases in the app.
Conclusion
In this tutorial, we have shown you how to add In App Purchases to your Flutter project. We have covered setting up the project, installing the necessary packages, building the UI, and handling different types of purchases. We have also shown you how to consume and restore purchases in Flutter.