Skip to content

Entitlements and order events

The LMS owns access. A commerce provider only reports that an order was paid, refunded or cancelled; it never decides who may open a course, and a browser redirect after payment never grants anything.

An entitlement records one reason a user may open a course: subject (a course), status (active, revoked, expired), valid_from and valid_until (null = no end), source_type and a unique source_key. For an order line the key is <provider>:<channel>:order_item:<id>, so applying the same order twice changes nothing.

course_user stays the projection that policies, H5P and SCORM read. EntitlementService is the only writer for purchased access and goes through CourseAccessService. The source types are order_item, admin, compliance, lti, wellms_order and migration (written today) and seat, group_legacy and voucher_legacy (reserved):

  • granting creates the entitlement and the course_user row, with the latest valid_until as end_date;
  • revoking marks the entitlement and recomputes the row: another active entitlement keeps access, otherwise the row is removed. Progress is never deleted;
  • access that existed before the first purchase (an admin attached the course) is recorded as a migration entitlement first, so a refund cannot remove it;
  • expiry (ulams:entitlements:expire, hourly) marks entitlements whose end has passed as expired and keeps the course_user row with the last end date, so progress survives and a re-purchase restores access.

Everything else that attaches a course goes through CourseAccessService, which the entitlements package decorates: an admin attaching a user records an admin entitlement (with the admin as granted_by), an LTI launch an lti one and a compliance assignment a compliance one (AccessSourceContext::as('lti', ...)). Attaching again moves the end date of the same entitlement instead of adding rows, and attaching without an end date never extends existing access. Events and end-date rules are unchanged.

An explicit removal (the admin remove and set endpoints) revokes every entitlement of the user for the course with reason admin_removed: an admin who removes a learner expects the access to be gone. Refunds, cancellations and expiry are narrower and never remove access another source still grants. Group access is not a course_user row and is neither recorded nor compared.

ulams:entitlements:backfill gives every existing course_user row an entitlement without changing any access. --dry-run computes and reports without writing. A row explained by a paid Wellms order line becomes a wellms_order entitlement (wellms:order_item:<line>:course:<course>), any other row a migration one (migration:course_user:<course>:<user>), each with the row’s own end date. A Wellms subscription over a course the user has gets an entitlement ending with the subscription, never later than the row. Rows that already have an entitlement in force are left alone, so a refund can still revoke a purchase. It is idempotent by source_key.

ulams:entitlements:parity compares, for every user and course, the access in course_user with the access the entitlements in force give, using the date rule of Course::hasUser(). Differences are missing_entitlement, unprojected_entitlement and end_date_mismatch. Every run stores a report in entitlement_parity_reports (--latest prints the last one) and the command fails when any pair differs. The backfill runs the same check in its transaction: one difference, or a changed course_user/products_users hash, rolls everything back, so a failed run changes nothing. Parity runs nightly (03:30, per tenant) once a backfill has passed, for as long as shadow mode lasts, and must show zero differences before a tenant’s cutover (CM-10).

While the Wellms cart sells, a listener records each purchase as a wellms_order entitlement. On ProductBought it writes one entitlement per course in the product (open-ended, without touching course_user: the cart attaches the course itself, with its own events); on ProductAttached it aligns the end date with the one the cart wrote for a subscription. A failure is logged and never blocks the purchase; the parity check reports what was missed. Turn it off with COMMERCE_SHADOW_WELLMS=false for a tenant that never used the cart.

POST /api/commerce/webhooks/{provider} is public. The credentials are:

Check Refusal
Ulams-Signature: t=<unix>,v1=<hex>, HMAC-SHA256 over <t>.<raw body> with COMMERCE_WEBHOOK_SECRET (or COMMERCE_WEBHOOK_SECRET_PREVIOUS while rotating) 400
t within 300 s of the server clock, so a captured request cannot be replayed later 400
Ulams-Event-Id equals the payload id; payload matches schema v1 400
Payload channel_code equals COMMERCE_CHANNEL, so a valid event for another tenant is refused 400
Body at most 256 KB; rate limited 413, 429
Unknown provider, or no secret or channel configured 404

A valid event is stored once per (provider, event id) and answers 202; a redelivery answers 200 with duplicate: true and does nothing. Rejections are logged with the source address and never change state.

The queued job (ApplyOrderEvent, 10 tries with backoff) does not trust the payload. Under a lock on the order it reads the order back from the provider and acts on the provider’s current state: paid lines get entitlements, a refund or cancellation revokes, a line whose units were all refunded loses its entitlement, a goodwill amount refund keeps access, and an order the provider does not report as paid is rejected. An event whose per-order sequence is not above the last applied one is stale, so out-of-order delivery converges to the true state. Outcomes (applied, stale, rejected, failed) are kept in commerce_order_events.

ulams:commerce:reconcile reads the provider’s event feed from a stored cursor, stores every event the LMS has not seen and applies it through the same job, then queues events that were stored but never applied. A blocked webhook URL or a dead worker on the provider side is therefore repaired. --retry-failed queues events the job gave up on again. Set COMMERCE_RECONCILE=true to run it every five minutes from the scheduler (per tenant).

Variable Default Meaning
COMMERCE_CHANNEL none The provider channel of this tenant
COMMERCE_WEBHOOK_SECRET none Per-tenant HMAC secret (whsec_...)
COMMERCE_WEBHOOK_SECRET_PREVIOUS none Secret being rotated out, accepted as well
COMMERCE_RECONCILE false Schedule reconciliation every five minutes
COMMERCE_SHADOW_WELLMS true Record Wellms cart purchases as entitlements (shadow mode)
COMMERCE_NIGHTLY false Run the nightly order sweep and catalogue check from the scheduler
COMMERCE_SYLIUS_URL, COMMERCE_SYLIUS_ADMIN_EMAIL, COMMERCE_SYLIUS_ADMIN_PASSWORD none The Sylius service and its admin API user (platform env only)
ENTITLEMENTS_EXPIRE true Schedule ulams:entitlements:expire hourly
ENTITLEMENTS_PARITY true Schedule ulams:entitlements:parity nightly
  • Buying from the learner frontend (single login, cart, checkout, return page): CM-5. Until then orders reach the LMS only from a Sylius an operator or a test fills.
  • Seat pools are CM-9; the source types seat, group_legacy and voucher_legacy are reserved.
  • Unit refunds (a partial refund tied to order units) are not read yet: a full refund or a cancellation revokes, a goodwill amount keeps access.
  • The parity report is read with ulams:entitlements:parity --latest; showing it in the admin comes with the unified commerce admin (CM-8).
  • scripts/commerce-e2e.sh and the CI job that runs the flow against the compose profile are CM-7; until then the live suites in commerce-sylius are run by hand against the profile.