monetization 1.2.0 copy "monetization: ^1.2.0" to clipboard
monetization: ^1.2.0 copied to clipboard

A wrapper around the Web Monetization API to monetize apps.

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');
}
copied to clipboard

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
});
copied to clipboard

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;
}
copied to clipboard

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
copied to clipboard

Get the revenue from the current session #

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

Enable/disable the monetization dynamically #

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

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);
copied to clipboard

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);
copied to clipboard

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();
copied to clipboard

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1
likes
40
points
37
downloads

Publisher

verified publishertomasarias.me

Weekly Downloads

2024.09.19 - 2025.04.03

A wrapper around the Web Monetization API to monetize apps.

Repository (GitHub)

License

MIT (license)

Dependencies

http, js

More

Packages that depend on monetization