Skip to content

9.21.0

Latest

Choose a tag to compare

@github-actions github-actions released this 03 Dec 13:54
· 318 commits to master since this release
1804b71

RESTHeart 9.21.0 changes how the Stripe plugin keeps the subscription state: it reads it from Stripe at each webhook event, instead of taking it from the event. It also adds three things for per-seat plans: licences granted by themselves, the seats changed from the application, and Stripe Managed Payments.

One change alters how the webhook works. Read "Behaviour changes" below before you upgrade.

Behaviour changes

  • The webhook calls the Stripe API (#853). At each customer.subscription.*, invoice.payment_succeeded, invoice.payment_failed and checkout.session.completed event, the plugin lists the customer's subscriptions on Stripe and stores the one in force. The secret key needs read access to subscriptions; a full secret key has it.
  • POST /stripe/webhook answers 500 when Stripe cannot be read (#853). Nothing is written, and Stripe delivers the event again. Before, an event was always answered 200.
  • subscription.last_applied_event_at is gone (#853). The stored subscription carries sync_rev, a revision, instead. The old field is removed from each document at its next sync. No migration is needed.
  • A custom SubscriptionOwnerProvider must be updated (#853). writeSubscription takes the revision instead of the event time, patchSubscription is removed, and readStoredSubscription and markTrialWillEndNotified are new.
  • The trial notice is sent once for each trial end (#853). A redelivered customer.subscription.trial_will_end sends nothing. The same holds for the payment-failed and the canceled emails, which now follow the change of the stored state.

Stripe: the subscription state is read from Stripe (#853)

Until 9.20 the plugin wrote the state from the payload of each event and ordered the events by their created time. Stripe delivers events in any order, dates them to the second, and says not to use created to order them. Two events of the same second could leave the wrong state. The case that showed it: a trial starts with a paid invoice of zero while the subscription is trialing; when invoice.payment_succeeded was processed after customer.subscription.created, the entity was stored as active until the trial ended.

Now an event only says that something changed for a customer. The plugin then reads the customer's subscriptions from Stripe and stores the state of the one in force: status, price, seats, trial end, period end. The order of the deliveries and their number no longer matter.

What this fixes:

  • a trial is stored as trialing, whichever of its events arrives last;
  • the seats are the ones Stripe has, also when an older customer.subscription.updated is delivered late;
  • the end of an old subscription does not send back to the default plan an entity that has started a new one: the subscription in force is the newest that is not canceled or incomplete_expired;
  • an invoice event no longer sets the status by itself. A paid invoice does not always make a subscription active, as Stripe's documentation says;
  • an event whose payload does not match the API version of the SDK is no longer dropped: only the customer id is read from it.

Two deliveries can be processed at the same time, and a read of Stripe is a picture of one moment. The write is therefore conditional: a sync reads the revision of the stored state, reads Stripe, and writes only if the revision has not changed. Otherwise it reads again. The newest read always wins, on any number of nodes.

The entity document changes as follows:

"subscription": {
  "plan": "pro",
  "status": "trialing",
  "seats": 3,
  "sync_rev": 4,
  "trial_will_end_notified_for": { "$date": 1798882000000 },
  "trial_will_end_notified_at": { "$date": 1798622800000 }
}

To correct an entity stored with a wrong state by an earlier version, resend any of its subscription events from the Stripe Dashboard: the sync writes what Stripe has now.

Not covered by this change: events that Stripe never manages to deliver. Stripe retries for up to three days; after that, list them with the Events API (delivery_success=false) and resend them.

Stripe: licences granted by themselves on a per-seat plan (#850)

A new option of a plan, seats.auto-license, default false:

plans:
  pro:
    price-id-monthly: price_...
    seats: { mode: per_seat, auto-license: true }

With true:

  • whoever joins the entity gets a licence at once when a seat is free: an invitation accepted, an activation, an OAuth sign-in that accepts an invitation, and the user who creates the entity;
  • when the subscription changes, the free seats go to the members without a licence, in the order they joined;
  • a member who is removed frees their seat;
  • when the seats go down below the licences held, the licences of the last members to join are revoked.

An owner can still grant and revoke by hand with /stripe/licenses. With no seat free, nothing is granted, and @subscription.seats.available shows it: a rule can allow invitations only while a seat is free.

Stripe: the seats changed from the application (#851)

POST /stripe/portal takes an optional seats. With it, the URL returned opens Stripe's confirmation page for the new quantity directly, instead of the Customer Portal home: the customer sees the amount and confirms.

POST /stripe/portal
{ "seats": 5 }

Before anything is created on Stripe the request is checked, and refused with a message that says why:

  • 400 when seats is not an integer, is below the seats in use or below one, is above seats.max, or is the current number;
  • 409 when the entity has no active subscription, or its plan is not per_seat.

Proration and the moment a decrease takes effect follow the configuration of the Customer Portal.

Stripe: Managed Payments (#852)

A new option of a plan, managed-payments, default false:

plans:
  pro:
    price-id-monthly: price_...
    managed-payments: true

With true, the subscription Checkout of that plan is created with managed_payments[enabled]: Stripe sells as merchant of record, calculates and collects the taxes of each country, and issues the invoices. It requires Managed Payments enabled in the Stripe account and an eligible tax code on the product; otherwise Stripe refuses the Checkout. Subscriptions that already exist are not affected.

On a multi-tenant deployment the option belongs to the tenant's plans, like the others.

The products Checkout (products mode) does not support it.