JustFlows

Bookings · Extend

Commerce and calendar contracts

Sell services from your own store, report payments and credits, or add a calendar provider.

Bookings keeps no prices, moves no money, and talks to no calendar provider by name. Other plugins take on that work through the contracts below. Shop is one commerce provider; your own store can be another.

Commerce providers

Add a provider with the justflows.bookings.commerce.providers filter. Without one, nothing is ever due: bookings confirm by their own rules. A provider that throws or answers available() === false is treated as absent, and a paid service then cannot be booked online (503 payments_unavailable).

Register a provider
ctx.hooks.filter("justflows.bookings.commerce.providers", (providers) => [
  ...providers,
  {
    id: "acme.store",
    label: "Acme Store",
    available: () => storeIsSetUp(),
    requiresPayment: (productRef) => priceOf(productRef) > 0,
    // Where the customer pays. Repeating it for the same booking must not start a second payment.
    startPayment: async (booking, { origin, session }) => {
      const order = await createOrder(booking);
      return { url: `${origin}/checkout/${order.id}`, ref: order.id, label: order.number };
    },
    // Optional: keep a product per service, and stop a payment a booking no longer needs.
    syncService: async (service) => ({ ref: await upsertProduct(service) }),
    cancelPayment: async (booking, reason) => cancelOrder(booking.paymentRef, reason),
  },
]);
MemberRequiredWhat it does
id, labelYesThe plugin id, stored on the services and bookings it handles, and a name for people.
available()YesWhether it can take payments right now.
requiresPayment(productRef)YesWhether bookings of this product must be paid before they confirm.
startPayment(booking, context)YesStarts payment and answers { url, ref?, label? }. Must be idempotent per booking.
startPayments(bookings, context)NoOne payment for several bookings, such as places for two children.
syncService(service, context)NoCreates or updates what sells a service, with the acting staff member's rights; answers { ref }.
syncMembership(membership, context)NoCreates a membership or class-pack product; answers the reference a credit plan is keyed by.
startPurchase(lines, context)NoPuts items (costumes, fees) in a cart, each line carrying a reference reported back with the paid order.
cancelPayment(booking, reason)NoThe booking no longer needs payment. Must not move money.
editPath(productRef)NoWhere staff edit the product in the admin.
revenue(refs, context)NoIts own recorded amounts for report totals, or null when the session may not see them.

Payment outcomes

Report what happened to a payment with POST v1/linked/payment through ctx.plugins.call. Only plugin calls reach the route, and a plugin reports only for itself: provider must be your plugin id, and the booking must be one you handle (you started its payment, or you sell its service). Each outcome is applied once per provider and eventKey, with the booking locked, so repeats, simultaneous deliveries, and outcomes arriving out of order all end in the same state.

Report a payment
ctx.hooks.action("acme.store.order.paid", async (order) => {
  const response = await ctx.plugins.call("justflows.bookings", {
    method: "POST",
    path: "v1/linked/payment",
    body: {
      provider: "acme.store",
      bookingId: order.bookingId,
      eventKey: `order-${order.id}-paid`,
      outcome: "paid", /** ordered | pay_later | partially_paid | paid | failed | cancelled | refunded | partially_refunded | disputed */
      ref: order.id,
      label: order.number,
    },
  });
  /** { result: "confirmed" | "paid_late" | "needs_refund" | "recorded" | "cancelled" | "ignored" | "duplicate" } */
});

A payment that arrives after the payment window closed confirms the booking when its time is still free (booking.paid_late), and otherwise marks it paid after release so staff refund it through the provider. disputed marks the payment state and leaves the booking as it is.

Credits

Memberships and packages report credits with POST v1/linked/credits. provider must be your plugin id, and balances are keyed by provider and sourceRef, so you only reach what you sold. A grant must draw on a credit plan your plugin owns: staff set the owner with provider when they save the plan, and a new plan takes the only commerce plugin when there is exactly one. Each message is applied once per provider and eventKey; a renewal grants each period once, and status changes are ordered by occurredAt.

Report credits
await ctx.plugins.call("justflows.bookings", {
  method: "POST",
  path: "v1/linked/credits",
  body: {
    provider: "acme.store",
    eventKey: `sub-${subscription.id}-${period}`,
    occurredAt: new Date().toISOString(),
    kind: "grant",                      /** grant | pause | resume | end | revoke */
    sourceRef: subscription.id,         /** the purchase: unique per provider */
    productRef: subscription.productId, /** grants: the product a credit plan is keyed by */
    periodKey: period,                  /** grants: one grant per period */
    quantity: 1,
    validUntil: subscription.periodEnd,
    customer: { userId: subscription.userId, email: null },
  },
});

Billing never cancels a booking, and cancelling a booking never cancels a subscription. Trials grant nothing; a paused membership keeps its credits but books nothing new; an ended one stays usable until its credits expire; revoke removes what is left.

Calendar adapters

Add a calendar provider, such as Google, Outlook, or CalDAV, with the justflows.bookings.calendar.adapters filter. The built-in ics adapter cannot be replaced. Adapters reach the network through the host’s ctx.outbound, which needs the network:outbound permission.

MemberRequiredWhat it does
id, labelYesA lowercase id such as acme.caldav, and a name for people.
fieldsYesWhat staff fill in: { name, label, kind: url | text | secret, required, hint? }. Secret values are never shown again.
pull(input)YesBusy times between two moments: { ok: true, intervals: [{ key, startMs, endMs }] }, or { ok: false, reason: auth | unavailable | invalid, message }. Keep key stable per occurrence.
push(input)NoWrite one booking to the calendar (upsert or delete). Idempotent per idempotencyKey. Write the event's uid as its iCalendar UID so it is never imported back.
verifyWebhook(input)NoWhether a change notification posted to /public/calendar-hooks/<connection> really comes from the provider.
describe, validateNoA non-secret hint for the admin, and a message when credentials cannot work.

Both pull and push may answer rotated credentials, such as a refreshed OAuth token, to store instead of the old ones. A calendar that keeps failing keeps its last busy times blocking, and retries with back-off.