Sales modes and external enrollments
How a course is bought (Checkout, Request, External link, Invitation), price shown/hidden/on request, workspace default, checkout gates, enrollments API after an external sale, external payment on the course member, SSO, webhooks and migration.
Status: branch release/course-sales-mode (07/10/2026). Owner decision of 07/10 ("How this course is bought"). Guides: How you sell a course, Enrollments API. Builds on Requests, Guest checkout and Customer SSO. Paths relative to backend/ and web-next/.
1. Rules (one place)
libs/common/src/common/leads/course-sales.rules.ts, twins in web-next/lib/course-sales.ts (node --test):
| Function | Line | What it decides |
|---|---|---|
isSafeExternalUrl / isContactUrl | 63 | external link only https:// with a dotted host, no credentials, spaces or control characters, max 2048 (no javascript:, data:, http:, //host, relative); contact = that or mailto: with a valid email |
selectedSalesMode | 96 | course choice → workspace default (checkout/request/invite; external is never a default) → checkout |
offerModeOf | 133 | invite → invite; external with a safe link → external, else request; request → request (free and with Stripe too); checkout → free if free, checkout with a verified connect account and productId, else request (the usual fallback) |
guestCheckoutAllowed | 151 | offerModeOf === 'checkout' and a Standard seller |
onlineCheckoutAllowed | 160 | account checkout and "Enroll for free" only in checkout mode; the purchase link (invitation) always passes |
salesErrors / cleanSales | 169 | save validation: externalUrl required and safe only in external, contactUrl safe in invite or with no mode; elsewhere an invalid link is dropped |
enrollmentCycleOf | 258 | edition of an enrollment: on demand none; live the requested one if it belongs to the course, the only one if there is one; with several editions the API requires cycleId (strict), Requests take the next one not ended |
lead.rules.ts re-exports offerModeOf/guestCheckoutAllowed for existing callers.
2. Data model
| Where | Field | Notes |
|---|---|---|
courses.billing.sales (and revisions) | { mode?, price?, externalUrl?, externalLabel?, externalParams?, contactUrl?, contactLabel? } | libs/common/src/common/models/microservices/course/course-sales.model.ts:18, inside BillingDocument (billing-model.ts:49): it travels wherever the price travels (Billing entity, BillingRto, cards, showcase). Missing = workspace default and price shown: no backfill |
lead_settings.salesMode | checkout | request | invite | models/microservices/profile/lead-settings.model.ts:92; in LeadPublicSettingsRto (the offer uses it, the site does not receive it) |
course_members.enrollment | { source: 'external-payment' | 'sso', via: 'api' | 'lead' | 'sso', externalId?, leadId?, at } | models/microservices/course/course-member.model.ts:86. purchased stays false: no "buyers cannot be removed" lock, no COURSE_PURCHASED, no transactions/subscriptions; exposed as MemberGatewayRto.enrollmentSource for the members badge |
3. Saving from the builder
PATCH /courses/:id with billing.sales (DTO CourseSalesGatewayDto, shape). In the course service (apps/course/src/app/courses/service/courses-upsert.service.ts): validSales (line 370) → salesErrors (400 course-sales-invalid with fields) and cleanSales; the change goes into the version like the rest of billing and is written right away on the online course ($set billing.sales, line 326), like settings.autoRecord: the storefront changes without republishing or a new review.
4. Public offer and gates
GET /public/courses/:id/offer(apps/gateway/.../profile/leads/leads.service.ts:114): readsbilling.salesfrom the online course (getCourseById) falling back to the published version; answersmode,guestCheckout,price(show|hide|on-request),external { url, label, params }(only inexternal),invite { contactUrl, contactLabel }(safe contacts only). The workspace default is not exposed.course-sales.gateway.ts:salesContextOf(30) = online course choices + default;assertSoldOnline(71) = 409course-not-sold-onlineoutside checkout mode.- Gates: account checkout
CheckoutSessionsService(payment/checkout-sessions/checkout-sessions.service.ts:64, purchase links pass), "Enroll for free"POST /courses/:id/start(course/courses/courses.controller.ts:770), guest checkoutsellableCourse(payment/guest-checkout/guest-checkout.service.ts:91, 409direct-charge-not-available).
5. Course page, cards, SEO (web-next)
components/courses/player/purchase-box.tsx: price fromoffer.price(thenbilling.sales):hide= no price row,on-request= "Price on request"; no instalments or coupon outsideshow.InviteOnly(293): no button, contact (mailto or https). The external link button: icon andofferCtaLabel(lib/lead-form.ts) with the academy label.app/(course)/courses/[courseId]/page.tsx:418onBuy:external→buildExternalUrl(lib/course-sales.ts:99: chosen parameters, page UTMs, email only if chosen and signed in; an unsafe link never becomes a URL) andwindow.location.assign(same tab);invite→ nothing.CourseStructuredData(page.tsx:1096) +courseJsonLd(lib/course-sales.ts:119): schema.orgCourse,offerswith the price only withshow.- Cards and showcase:
coursePriceLabel(lib/course-price.ts:19) andCouponBadge(components/courses/course-card-pills.tsx); social cards (feed-modules,social-rail).
6. Builder and settings
components/courses/builder/sales-section.tsx:59SalesSection, underBillingSectionin the Pricing panel: five choices (workspace default + four), yellow fallback notice (checkoutFallsBack: connect not verified orproductIdmissing; readsGET /payment/connect-accountsand/leads/settings, silent without permission), external link fields (URL, label, parameters; privacy note on email), invitation contact, Show/Hide/On request.billing-section.tsxkeepssaleswhen switching Free/Paid.components/leads/sales-default-section.tsx:19: Settings › Requests and sales › "How courses are bought", saved on click (PUT /leads/settings { salesMode }, validated inapps/profile/src/app/leads/leads.service.ts:397).
7. Enrollment after an external sale
apps/gateway/src/gateway/controllers/integrations/enrollments/:
POST /integrations/enrollments(course-enrollments.controller.ts:27) andPOST /leads/:id/enroll:ScopeAbilityGuard(UPDATE, LEADS). With an API key (apikey) the user isUser.fromApiKey: workspace = the key's organization, privileges of its creator. 201 when something changes, 200 whenexisting.CourseEnrollmentsGatewayService.enroll(course-enrollments.service.ts:103):- valid email and
externalId; course fromgetCourseByIdin the workspace (else 404) and online (409); edition viaenrollmentCycleOf; - Murlock
enroll-<course>-<email>(30 s); account(191): with SSO Product only no new accounts (getProfileByEmail, else 403workspace-sso-requiredwithloginUrl); otherwiseAccountProvisioningService.provision(single entry point,source: integration|lead; blocked account = 409);- workspace Student via
WorkspaceSsoGatewayService.ensureMembership(staff stays staff → internal member); course-members.enroll-external→CourseMembersUpsertService.enrollExternal(apps/course/.../members/services/course-members-upsert.service.ts:1434): new member withenrollment(throughcreateMany, events and completions like direct adds),existingfor same person/course/edition,cycle-addedwith$addToSetof the cycle and completion, pending invitation → enrollment. Neverpurchased, neverCOURSE_PURCHASED;- if changed:
enrollment.createdwebhook (resourceEntityType.ENROLLMENT, owner = workspace) and theGUEST_PURCHASE_CONFIRMEDemail withorigin: 'external'("your enrollment is confirmed", set password or sign in; links on the workspace host fromworkspaceHosts), skipped withnotify: false.
- valid email and
enrollLead(156):leads.retrieve(newACTION.RETRIEVEin profile), same enrollment withvia: leadandleadId, thenleads.update→enrolled+profileId(lead.updatedwebhook).
SSO: enrollCourses marks enrollments from courses with source: 'sso' (workspace-sso-gateway.service.ts:628, students only). An "external payment" claim was considered and dropped: no enrollment without an Aladia purchase creates revenue; the useful distinction is who manages it (the product with replace_courses, staff for the API).
Reports: Aladia revenue only comes from payment-service transactions (checkout and instalments); these enrollments create none. numberOf.externalAttendants counts them like hand-added students. Seats (purchases, "seats left") are not consumed: with external sales the CRM governs seats.
8. Migration
scripts/migrations/2026-10-course-sales-mode.js: inserts the enrollment.created event in webhook_events (without it the webhook does not fire) and counts courses/workspaces/enrollments already touched. PHASE from process.env.PHASE or the global PHASE from mongosh.sh (const MODE, never const PHASE).
9. Tests
tests/unit/course-sales.spec.ts: Stripe yes/no × mode × free/paid matrix, workspace default, fallback without product, safe links (open redirect,javascript:, credentials, CRLF), public offer, checkout gate, API (leads.updateguard, idempotency, key workspace, SSO product only,notify, no payment calls, nocourse-purchased),enrollExternal(purchased false, existing, cycle-added, other workspace).web-next/lib/course-sales.test.mjs: links, parameters, notice, JSON-LD without price.- e2e (after release):
web-next/e2e/help/flows/leads/sales-modes.spec.mjs.