Consume In-App Purchase

consume_inapp_purchase is a Flutter plugin that provides an easy way to consume in-app purchases in your Flutter applications. It simplifies handling consumable purchases, ensuring they are properly consumed after being acknowledged.

Features

  • Consume consumable in-app purchases.
  • Supports both Android and iOS platforms.
  • Simple and intuitive API for Flutter developers.

Installation

Add the following dependency to your pubspec.yaml file:

dependencies:
  consume_inapp_purchase: ^1.0.0

Then, run the following command:

flutter pub get

Usage

Import the Plugin

import 'package:consume_inapp_purchase/consume_inapp_purchase.dart';

Consuming a Purchase

To consume a purchase, call the consumePurchase method with the purchase token.

Future<void> consumePurchase() async {
    String consumeStatus;
    try {
      consumeStatus = await _consumeInappPurchasePlugin.consumePurchase() ??
          'No purchase found.';
    } on PlatformException {
      consumeStatus = 'Failed to consume purchase.';
    }
    debugPrint(consumeStatus);
    if (!mounted) return;
}

Example

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:consume_inapp_purchase/consume_inapp_purchase.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _consumeInappPurchasePlugin = ConsumeInappPurchase();

  @override
  void initState() {
    super.initState();
  }

  Future<void> consumePurchase() async {
    String consumeStatus;
    try {
      consumeStatus = await _consumeInappPurchasePlugin.consumePurchase() ??
          'No purchase found.';
    } on PlatformException {
      consumeStatus = 'Failed to consume purchase.';
    }
    debugPrint(consumeStatus);
    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            ElevatedButton(
              onPressed: consumePurchase,
              child: const Text('Consume Purchase'),
            ),
          ],
        ),
      ),
    );
  }
}

Supported Platforms

  • Android
  • iOS

Getting Started

To get started with Flutter, visit the official documentation.

For more information about in-app purchases, refer to:

Contributing

Contributions are welcome! If you encounter issues or have suggestions, feel free to open an issue or submit a pull request.

License

consume_inapp_purchase is licensed under the MIT License. See the LICENSE file for more information.