monetization 1.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

monetization

A wrapper around the Web Monetization API.

 

Offer extra content and features for users who stream micro-payments — including premium features, additional content, or digital goods.

API Reference

Usage #

A simple usage example that initializes the monetization to a specific payment pointer:

import 'package:monetization/monetization.dart';

main() {
  var monetization = Monetization('\$pay.tomasarias.me');
}

You can subscribe to Web Monetization events using a Stream:

monetization.onPending.listen((event) {
  // Prepare to serve the monetized content
});

monetization.onStart.listen((event) {
  // Show monetized content
});

monetization.onProgress.listen((event) {
  // Do something on each micro-payment
});

monetization.onStop.listen((event) {
  // Hide monetized content
});

You can also check if a user is paying without subscribing to the streams:

Future<bool> isPaying() async {
  // Prefer custom logic over this
  await Future.delayed(const Duration(seconds: 3));
  return monetization.isPaying;
}

Get information about the monetization #

monetization.isMonetized; // Returns if the user supports monetization
monetization.isPaying;    // Returns if the user is streaming payments
monetization.pointer;     // Returns the current payment pointer

Get the revenue from the current session #

monetization.getTotal(formatted: false); // 884389
monetization.getTotal(); // 0.000884389
monetization.assetCode;  // 'XRP'
monetization.assetScale; // 9

Enable/disable the monetization dynamically #

monetization.enabled;   // true
monetization.disable(); // Stops the monetization
monetization.enable();  // Start the monetization again with the same pointer

Probabilistic revenue sharing #

Sometimes you want to share revenue across different people, to do this pass a Map with the payment pointers and weights to the Monetization.probabilistic constructor.

This will choose one of the pointers for the entire session.

final pointers = {
  'pay.tomasarias.me/usd': 0.5,
  'pay.tomasarias.me/xrp': 0.2,
  'pay.tomasarias.me/ars': 0.3
};

var monetization = Monetization.probabilistic(pointers);

This will result on a 50% chance of choosing the first pointer, 20% chance of choosing the second and 30% chance of choosing the third one.

For more information on probabilistic revenue sharing, read this article by Ben Sharafian.

Verifying payments (using Vanilla) #

Monetization events can be manipulated, so you can't know for sure if a user is really paying. You can add an extra layer of security to your monetized content using Vanilla. To learn how does Vanilla works read this article.

You will need your API credentials, you can get them here.

// Vanilla API Credentials
final clientId = 'Your Client ID';
final clientSecret = 'Your Client Secret';

var monetization = Monetization.vanilla(clientId, clientSecret);

Now you can check if the payment stream is valid in different ways:

// With Vanilla, this will return true if the user is paying and Vanilla generates a proof of payment.
monetization.isPaying;
// With Vanilla, this will return the current payment rate per second, if the monetization is stopped this will be 0.
monetization.getVanillaRate()
// With Vanilla, this will return the total amount received from the current requestId.
monetization.getVanillaTotal();

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.2.0 #

  • Added support for Vanilla
  • Added enabled getter that returns if the meta tag is present
  • getTotal() now returns the formatted amount by default
  • Example now uses Vanilla
  • Improved docs

1.0.1 #

  • Format and pubspec changes.

1.0.0 #

  • Initial release.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter + Web Monetization',
      home: MyHomePage(title: 'Web Monetization Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Monetization monetization;
  double total;

  // Vanilla API Credentials
  final clientId = const String.fromEnvironment('CLIENT_ID');
  final clientSecret = const String.fromEnvironment('CLIENT_SECRET');

  @override
  void initState() {
    super.initState();
    setState(() {
      monetization = Monetization.vanilla(clientId, clientSecret, debug: true);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: true,
        backgroundColor: Color(0xFF6ADAAB),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Image(
                image: AssetImage('assets/wm.png'),
                width: 275,
                height: 275,
              ),
            ),
            StreamBuilder(
                stream: monetization.onProgress,
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    return Text(
                      'Monetization not started yet',
                      style: Theme.of(context).textTheme.headline4,
                    );
                  }

                  return Text(
                    '${monetization.getTotal()} ${monetization.assetCode}',
                    style: Theme.of(context).textTheme.headline4,
                  );
                }),
            Padding(
              padding: const EdgeInsets.all(24.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  RaisedButton(
                    onPressed: () => monetization.enable(),
                    child: Text(
                      'Start',
                      style: Theme.of(context).textTheme.headline6,
                    ),
                  ),
                  SizedBox(
                    width: 24,
                  ),
                  RaisedButton(
                    onPressed: () => monetization.disable(),
                    child: Text(
                      'Stop',
                      style: Theme.of(context).textTheme.headline6,
                    ),
                  ),
                ],
              ),
            ),
            Text(
              'Tip: Open the DevTools to see the monetization events.',
              style: Theme.of(context).textTheme.overline,
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  monetization: ^1.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:monetization/monetization.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
99
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Jul 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with runtime flutter-native on android

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Package not compatible with runtime flutter-native on ios

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Package not compatible with runtime flutter-native on linux

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Package not compatible with runtime flutter-native on macos

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Package not compatible with runtime flutter-native on windows

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Package not compatible with runtime native-aot

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Package not compatible with runtime native-jit

Because of the import of dart:js_util via the import chain package:monetization/monetization.dartpackage:monetization/src/monetization_base.dartpackage:monetization/src/interop.dartpackage:js/js_util.dartdart:js_util

Maintenance suggestions

The package description is too short. (-1 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
http ^0.12.1 0.12.1
js ^0.6.1+1 0.6.2
Transitive dependencies
charcode 1.1.3
collection 1.14.13
http_parser 3.1.4
meta 1.2.1
path 1.7.0
pedantic 1.9.1
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0
Dev dependencies
build_runner >=1.6.2 <2.0.0
build_web_compilers >=2.6.1 <3.0.0
lint ^0.3.0
test ^1.14.4