Billing

Show your app's products on a pricing page, take payments with Stripe.js, and read what each user bought, with @authoric/js.

authoric.billing sells your app's own products to its users, through your Stripe account. Set up Stripe and your products first: see Selling from your app.

A pricing page

catalog() works signed out, so a public pricing page can list what you sell:

const { mode, publishableKey, products } = await authoric.billing.catalog();

for (const product of products) {
  for (const price of product.prices) {
    // price: { id, unitAmount, currency, interval, recurring, nickname }
    render(product.name, product.description, price.unitAmount / 100, price.interval);
  }
}
FieldIs
modeYour app's billing mode, test or live. The catalog lists that mode's products.
publishableKeyYour Stripe account's publishable key, for Stripe.js. Public by design.
productsEach active product: uid, name, description, display, defaultPriceId and its prices.

The same catalog is GET /{appUid}/autocrud/products/actions/catalog over REST.

Taking a payment

subscribe starts a subscription to a recurring price, and order a one-time purchase. Each answers the clientSecret that Stripe's own payment form confirms. The card details go to Stripe, never through your server or Authoric.

import { loadStripe } from "@stripe/stripe-js";

const { publishableKey } = await authoric.billing.catalog();
const stripe = await loadStripe(publishableKey);

const { clientSecret } = await authoric.billing.subscribe({ priceId });
// or: await authoric.billing.order({ priceId, quantity: 2 })

const elements = stripe.elements({ clientSecret });
elements.create("payment").mount("#payment");

// When the user submits:
await stripe.confirmPayment({ elements, confirmParams: { return_url: "https://app.example.com/thanks" } });

Stripe reports the outcome to Authoric, which updates the purchase in your app's database. Read the status back with subscriptions() rather than trusting the browser's confirmation.

Subscriptions and entitlements

const subscriptions = await authoric.billing.subscriptions(); // the user's own
await authoric.billing.cancel(subscriptionUid); // at the end of the period
await authoric.billing.resume(subscriptionUid); // undo a pending cancellation

const { status, limits, features } = await authoric.billing.entitlements();
if (features.exports) showExportButton();

entitlements() merges what every active subscription the user holds grants. Show and hide things with it in your UI, and check it again on the server side of anything you charge for: in a handler or a custom action.

Purchases always belong to the signed-in user. Who may buy is set by your policies; see Who can buy.

On this page