A flutter plugin for creating in app purchase in a super simple way.
Currently it doesn't supports subscription.
In App Purchase(IAP) is a very complicated thing to implement in any mobile app for many years. Generally, it takes 200+ lines of code just to implement in app purchase. But I have tried my best to make it as simple as possible using this plugin.
Please support me via Donation. Your donation seriously helps me to regularly update this plugin and do bug fixes fast.
First add this package in your pubspec.yaml
file using the following command in your terminal:
flutter pub add super_easy_in_app_purchase
Make sure you are in the root of the flutter project directory inside terminal at the time of running this command.
Create in app product in your Google Play Store and Apple App Store account. Follow the links for more detail steps:
Creating In App Product in Google Play Store
Creating In App Product in Apple App Store
Create a class level variable (e.g. inAppPurchase
) in your State class in stateful widget
import 'package:super_easy_in_app_purchase/super_easy_in_app_purchase.dart';
...
class _MyAppState extends State<MyApp> {
SuperEasyInAppPurchase inAppPurchase;
...
Initialise that variable in initState()
method
This is the most important and difficult step to understand.
@override
void initState() {
super.initState();
inAppPurchase = SuperEasyInAppPurchase(
inAppPurchaseItems: [
InAppPurchaseItem(
// This must be unique accross entire play/app store
productId: 'product1',
// This function will run when 'product1' is purchased successfully
// For simplicity, only a message is printed to console
// In real app, you should use shared preference to store related data
onPurchaseComplete: () => print('Product 1 purchased successfully !'),
// This function will run when 'product1' is refunded by google or removed intentionally by you using inAppPurchase.removeProduct('product1')
// These functions can also be an async
// In real app, you should use shared preference to store related data
onPurchaseRefunded: () => print('Product 1 disabled successfully !'),
),
InAppPurchaseItem(
productId: 'product2',
onPurchaseComplete: () => print('Product 2 purchased successfully !'),
onPurchaseRefunded: () => print('Product 2 disabled successfully !'),
// Setting this to true means you can later disable this product using inAppPurchase.removeProduct('product2')
isConsumable: true,
),
],
);
}
SuperEasyInAppPurchase()
constructor required a single named parameter inAppPurchaseItems
which is of type List<InAppPurchaseItem>
. Each InAppPurchaseItem
takes 3 required parameters and 1 optional parameter.
String productId
: It identifies the digital product. This id must be unique accross entire play/app store.Function onPurchaseComplete
: This function will run when its corresponding product is purchased successfully. The main purpose of this function is to activate the product (Generally using Shared Preferences).Function onPurchaseRefunded
: This function will run when its corresponding product is refunded/disabled intentionally by the developer. The main purpose of this function is to deactivate the product (Generally using shared preferences).bool isConsumable = false
: (Optional) Determines if the product is One-Time or Consumable. Setting this to true means you can later disable this product usinginAppPurchase.removeProduct(productId)
.
Note: Consumables are those products which needs to be purchased again and again, e.g. - The fuel of racing car. By default, isConsumable
parameter is set to false
.
Prevent memory leaks by calling stop()
method in your App State's dispose()
method:
@override
void dispose() {
inAppPurchase.stop();
super.dispose();
}
Start a purchase
Write this line of code in your button's onPressed function:
await inAppPurchase.startPurchase('product1');
Consume(disable) the purchase
In order to remove the purchase, use:
await inAppPurchase.removeProduct('product2');
When you consume a purchase, the user has to purchase it again in order to use its features.
Don't hesitate to email any issues or feature at [email protected].
Please support me via Donation. Your donation seriously motivates me to develop more useful packages like this.
This flutter plugin is developed by Rituraj Shakti. You can contact me at [email protected]