Code Monkey home page Code Monkey logo

flutter_billing_plugin's Introduction

flutter_billing_plugin

This plugin supports in-app purchases for App Store (on iOS) or Google Play (on Android).

Simple steps to add in-app purchases in you flutter app.

Getting Started

Add below line to your pubspec.yaml file

dependencies:
  flutter_billing_plugin: ^0.0.9

Initialize Plugin

@override
  void initState() {
    super.initState();
    initBilling();
    initListeners();
    initPrice();
  }

  Future<void> initBilling() async {
    billingHelper = FlutterBillingPlugin();
    await billingHelper?.init(productIds);
  }

  Future<void> initPrice() async {
    await billingHelper?.getPrice(Platform.isAndroid ? 'android.test.purchased' : 'weekly');
  }

  void initListeners() {
    billingHelper?.setPurchaseUpdateHandler((status, value) async {
      switch (status) {
        case 'success':
          if (kDebugMode) {
            print('$status : $value');
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                duration: const Duration(seconds: 1),
                backgroundColor: Utils.getPrimaryColor(),
                content: Text(
                  'Purchase Sucessfully',
                  style: TextStyle(fontSize: 18.0, fontFamily: 'Sans', fontStyle: FontStyle.normal, fontWeight: FontWeight.w500, color: Utils.getWhiteColor()),
                )));
          }
          break;
        case 'cancelled':
          if (kDebugMode) {
            print('$status : $value');
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                duration: const Duration(seconds: 1),
                backgroundColor: Utils.getPrimaryColor(),
                content: Text(
                  'Purchase cancelled by you',
                  style: TextStyle(fontSize: 18.0, fontFamily: 'Sans', fontStyle: FontStyle.normal, fontWeight: FontWeight.w500, color: Utils.getWhiteColor()),
                )));
          }
          break;
        case 'error':
          if (kDebugMode) {
            print('$status : $value');
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                duration: const Duration(seconds: 1),
                backgroundColor: Utils.getPrimaryColor(),
                content: Text(
                  'Something went wrong, Please try again after sometime',
                  style: TextStyle(fontSize: 18.0, fontFamily: 'Sans', fontStyle: FontStyle.normal, fontWeight: FontWeight.w500, color: Utils.getWhiteColor()),
                )));
          }
          break;
        case 'restore':
          if (kDebugMode) {
            print('$status : $value');
            ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                duration: const Duration(seconds: 1),
                backgroundColor: Utils.getPrimaryColor(),
                content: Text(
                  value ? 'Restore Successfully' : 'You don\'t have purchase to restore',
                  style: TextStyle(fontSize: 18.0, fontFamily: 'Sans', fontStyle: FontStyle.normal, fontWeight: FontWeight.w500, color: Utils.getWhiteColor()),
                )));
          }
          break;
        default:
          if (kDebugMode) {
            print('$status : $value');
          }
      }
    });

    billingHelper!.setPriceUpdateHandler((price) {
      if (kDebugMode) {
        print("Price: $price");
        setState(() {
          this.price = price;
        });
      }
    });
  }

Purchase product by passing product id as String and get response as 'success', 'cancelled', 'error'.

billingHelper!.buyProduct(Platform.isAndroid ? 'android.test.purchased' : 'weekly');

Recover old purchaseed product using below line call and return callback to restore with bool value

billingHelper!.checkRestore();

Support

For support, email [email protected]

flutter_billing_plugin's People

Contributors

palyo avatar

Watchers

 avatar

Forkers

ravinaab06

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.