The Wayback Machine - https://web.archive.org/web/20230316013808/https://github.com/KNawm/monetization
Skip to content

KNawm/monetization

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 use WebMonetization.org's API for sharing the revenue across payment pointers.

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 50% of the revenue going to the first pointer, 20% of the revenue going to the second pointer and 30% of the revenue going to 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.

About

๐Ÿ’ธ A wrapper around the Web Monetization API to monetize apps.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published