Web Monetization is an API that allows websites to request small payments from users facilitated by the browser and the user's Web Monetization provider.

This document is based on the Web Monetization Draft and a prototype implementation in Firefox.

Browser API

Extensions to the {{ Navigator }} interface

        partial interface Navigator {
          readonly attribute Monetization monetization;
        };
        

Not sure if Monetization interface makes more sense on `Navigator` or `Window`.

`Monetization` interface

        [Exposed=Window, SecureContext]
        interface Monetization : EventTarget {
          attribute EventHandler onprogress;
        };
        

The {{onprogress}} attribute is an {{EventHandler}} for a {{MonetizationProgressEvent/MonetizationProgressEvent}} named "progress".

`MonetizationProgressEvent`

          [Exposed=Window, SecureContext]
          interface MonetizationProgressEvent : Event {
            readonly attribute USVString url;
            readonly attribute DOMString amount;
            readonly attribute DOMString assetCode;
            readonly attribute unsigned long assetScale;
            readonly attribute DOMString? receipt;
          };
        
url attribute
A [[URL]] {{ URL/href }} representing receiver payment pointer for which the payment is completed.
amount attribute
The destination amount received as specified in the ILP packet.
assetCode attribute
A three letter asset code describing the amount's units. Currencies that have [[?ISO4217]] codes should use those.
assetScale attribute

The scale of the amount.

{{assetScale}} should probably be {{ octet }} as per Interledger's DynamicConfigurationProtocol.

For example, USD would have an assetScale of 2 when denominated in cents.

receipt attribute
Base64-encoded [[[Receipt]]] issued by the Web Monetization receiver to the Web Monetization provider as proof of the total amount received in the stream.

Should we define either a `MonetizationCurrencyAmount` or use {{ PaymentCurrencyAmount }} to combine {{amount}}, {{assetCode}} and {{assetScale}}?

        navigator.monetization.addEventListener("progress", async event => {
          console.assert(event instanceof MonetizationProgressEvent);

          const { amount, assetScale, assetCode, receipt } = event;
          if (receipt) {
            const isValid = await verifyPayment(receipt);
          }
          const paymentPointer = UrlToPaymentPointer(event.url);
        });
      

Browser Extension API

        [Exposed=Window]
        interface BrowserMonetization {
          readonly attribute BrowserMonetizationStartEvent onStart;
          readonly attribute BrowserMonetizationGenericEvent onStop;
          readonly attribute BrowserMonetizationGenericEvent onPause;
          readonly attribute BrowserMonetizationGenericEvent onResume;

          undefined completePayment(
            DOMString sessionId,
            MonetizationAmount amount,
            DOMString? receipt
          );

          Promise<DOMString?> refresh(DOMString sessionId);
        };

        dictionary MonetizationAmount {
          DOMString value;
          DOMString assetCode;
          unsigned long assetScale;
        };
      
        [Exposed=Window]
        interface BrowserMonetizationStartEvent : BrowserExtEvent {
          undefined addListener(BrowserMonetizationStartCallback callback);
        };

        callback BrowserMonetizationStartCallback = undefined (
          DOMString    sessionId,
          SpspResponse spspResponse
        );

        dictionary SpspResponse {
          DOMString destinationAccount;
          DOMString sharedSecret;
          boolean   receiptsEnabled;
        };
      
        [Exposed=Window]
        interface BrowserMonetizationGenericEvent : BrowserExtEvent {
          undefined addListener(BrowserMonetizationGenericCallback callback);
        };

        callback BrowserMonetizationGenericCallback = undefined (DOMString sessionId);
      

Where BrowserExtEvent is defined in [[[browserext]]].

Browser Extension Example

          browser.monetization.onStart.addListener((sessionId, spspResponse) => {
            // Start monetization: decide whether to make payments,
            // check authentication, setup payments etc.

            // Some time later, notify the browser for a completed payment:
            browser.monetization.completePayment(sessionId, {
              value: '60',
              assetScale: 2,
              assetCode: 'USD',
            });
          });

          browser.monetization.onStop.addListener(sessionId => {
            // Stop monetization for given sessionId.
          });

          browser.monetization.onPause.addListener(sessionId => {
            // User might have navigated away, so pause the payments.
            //  This is an optimization to avoid setting up new payments when
            // the user switches between tabs, which can happen very often.
          });

          browser.monetization.onResume.addListener(sessionId => {
            // Resume payments, as the user is back.

            // If the spspResponse is no longer valid, request a new session:
            const newSessionId = await browser.monetization.refresh(sessionId);
          });