TrackMe API Docs
Flow guides

Truck Subscriptions

How a truck owner subscribes one truck to a monthly Paymob plan — and why a 201 from subscribe means "pending checkout", not "subscribed".

This guide is for Flutter engineers building the owner subscription screens. A truck owner subscribes a single truck to a recurring monthly plan (billed through Paymob). It covers listing plans, subscribing/switching, the activation that happens via webhook, cancellation, reactivation, and the lifecycle states you must render.

All owner endpoints are under /api/v1/owner/* and require a Bearer token: Authorization: Bearer <oat_...>. The per-truck endpoints (show, subscribe, cancel, reactivate) additionally require an active owner membership and ownership of the truck (otherwise 403 FORBIDDEN). Send Accept-Language: ar for Arabic error copy. All wire timestamps are UTC ISO-8601 with a Z suffix. Amounts are in minor units (9900 = 99.00 SAR — divide by 100 for display).

A 201 from subscribe does not mean the truck is subscribed. POST /api/v1/owner/trucks/:id/subscription creates the row at status: "pending" and returns a Paymob checkout (paymentIframeUrl + clientSecret). The subscription is not live. It only flips to "active" after the owner completes payment in the Paymob webview and the HMAC-verified webhook POST /payments/webhook/paymob fires. Treat the 201 as checkout creation, then re-fetch the subscription until status === "active".

Activation is webhook-driven, not response-driven. After payment, Paymob redirects the webview to /payments/return — that page is display-only. The real pending → active transition happens out-of-band via the webhook. If the webhook is delayed, the subscription stays pending even though the owner saw a success page. Poll the subscription; do not rely on the redirect.

The flow at a glance

Step 1 — Owner lists available plans

GET /api/v1/owner/subscription-plans returns the active plans (cheapest first), each with id, code, name, monthlyAmountMinor, currencyCode, and a free-form features object.

Step 2 — Owner subscribes a specific truck

POST /api/v1/owner/trucks/:id/subscription with { planId }. The server upserts one truck_subscriptions row keyed by the truck, sets status: "pending", creates a Paymob recurring intention + a pending payment order (SUB-..., 30-min expiry), and returns 201 with subscription (pending), paymentOrder, paymentIframeUrl, and clientSecret. Not live yet.

Step 3 — App opens the Paymob checkout webview

Open paymentIframeUrl (Paymob unified checkout, driven by clientSecret + your public key) in a webview. The owner pays there.

Step 4 — Paymob activates the subscription via webhook

The owner finishes payment; Paymob fires POST /payments/webhook/paymob (HMAC-verified, not owner-callable). Because the paid order is a truck_subscription, the server flips status to "active", sets a 1-month period, and stamps nextBillingAt = now + 1 month.

Step 5 — App confirms by re-fetching

GET /api/v1/owner/trucks/:id/subscription (or GET /api/v1/owner/subscriptions) and check status === "active". Once active, paymentIframeUrl and clientSecret are null — capture them in Step 2 if you need them.

Step 6 — Monthly renewals run automatically

Each month Paymob auto-charges and posts POST /payments/webhook/paymob/subscriptions. Success keeps it active; a failed/suspended charge moves it to past_due.

Step 7 — Owner can cancel, then later reactivate

POST .../subscription/cancel cancels immediately (and at Paymob if a gateway sub exists). POST .../subscription/reactivate resumes a cancelled subscription — but only when a gateway subscription id exists.

Per-step detail

Step 1 — List plans

GET /api/v1/owner/subscription-plans — needs auth only (no active-owner check), so any authenticated user can read it.

GET /api/v1/owner/subscription-plans
Authorization: Bearer oat_xxx
{
  "data": {
    "items": [
      {
        "id": "subp_truck_pro_monthly",
        "code": "truck_pro_monthly",
        "name": "Truck Pro Monthly",
        "monthlyAmountMinor": 9900,
        "currencyCode": "SAR",
        "features": { "analytics": true, "prioritySupport": false }
      }
    ]
  },
  "meta": { "requestId": "req_01h..." }
}

Step 2 — Subscribe (or switch plan)

POST /api/v1/owner/trucks/:id/subscription. The :id is the truck id (trk_...), never the subscription id. Body is a single planId (string, length 5–40).

POST /api/v1/owner/trucks/trk_abc123/subscription
Authorization: Bearer oat_xxx
Content-Type: application/json

{ "planId": "subp_truck_pro_monthly" }

Returns 201. The subscription.status is "pending" — read it directly and do not infer the state.

{
  "data": {
    "subscription": {
      "id": "tsb_01h...",
      "truckId": "trk_abc123",
      "truckName": "Burger Bus",
      "plan": {
        "id": "subp_truck_pro_monthly",
        "code": "truck_pro_monthly",
        "name": "Truck Pro Monthly",
        "monthlyAmountMinor": 9900,
        "currencyCode": "SAR"
      },
      "status": "pending",
      "currentPeriodStart": null,
      "currentPeriodEnd": null,
      "nextBillingAt": null,
      "cancelAtPeriodEndAt": null,
      "cancelledAt": null,
      "paymentIframeUrl": "https://ksa.paymob.com/unifiedcheckout/?publicKey=...&clientSecret=...",
      "clientSecret": "<opaque-paymob-secret>"
    },
    "paymentOrder": {
      "id": "pay_01h...",
      "orderNumber": "SUB-20260621-AB12CD",
      "status": "pending",
      "totalAmountMinor": 9900,
      "currencyCode": "SAR",
      "paidAt": null
    },
    "paymentIframeUrl": "https://ksa.paymob.com/unifiedcheckout/?publicKey=...&clientSecret=...",
    "clientSecret": "<opaque-paymob-secret>"
  },
  "meta": { "requestId": "req_01h..." }
}

This endpoint upserts on the truck — there is exactly one subscription per truck. Calling it again (to switch plans or retry a failed payment) overwrites the same row: it resets status back to "pending" and clears the period, next-billing, and cancel/cancelled fields, then issues a brand-new checkout. So "switch plan" and "retry payment" both blow current state away. Do not call it casually on an active subscription.

An unknown or inactive planId returns 404 NOT_FOUND (not 422). 422 VALIDATION_ERROR is only for a malformed body (e.g. planId outside the 5–40 length).

Step 3 — Open the checkout

Open paymentIframeUrl in a webview using clientSecret and your Paymob public key. See Cart & Checkout for the shared checkout/webhook mechanics.

Step 4 — Webhook activates (server-side, not your call)

When payment succeeds, Paymob calls POST /payments/webhook/paymob. This path is outside /api/v1, is public (HMAC-verified), and is not owner-callable — your app never hits it. The server marks the payment order paid and, because its service type is truck_subscription, activates the subscription: status → "active", currentPeriodStart = now, currentPeriodEnd = now + 1 month, nextBillingAt = now + 1 month.

Step 5 — Confirm by re-fetching

GET /api/v1/owner/trucks/:id/subscription returns the live state. :id is the truck id; the payload is { subscription: <subscription | null> } (null if the truck was never subscribed).

GET /api/v1/owner/trucks/trk_abc123/subscription
Authorization: Bearer oat_xxx
{
  "data": {
    "subscription": {
      "id": "tsb_01h...",
      "truckId": "trk_abc123",
      "truckName": "Burger Bus",
      "plan": { "id": "subp_truck_pro_monthly", "code": "truck_pro_monthly", "name": "Truck Pro Monthly", "monthlyAmountMinor": 9900, "currencyCode": "SAR" },
      "status": "active",
      "currentPeriodStart": "2026-06-21T10:00:00.000Z",
      "currentPeriodEnd": "2026-07-21T10:00:00.000Z",
      "nextBillingAt": "2026-07-21T10:00:00.000Z",
      "cancelAtPeriodEndAt": null,
      "cancelledAt": null,
      "paymentIframeUrl": null,
      "clientSecret": null
    }
  },
  "meta": { "requestId": "req_01h..." }
}

GET /api/v1/owner/subscriptions lists every subscription across the caller's trucks (newest first), same per-row shape.

Step 6 — Renewals and failures (webhook-driven)

POST /payments/webhook/paymob/subscriptions (also outside /api/v1, not owner-callable) carries recurring-charge events. Gateway states map to local status: active|started|paid → active; past_due|failed|suspended → past_due; cancelled|canceled|ended → cancelled. There is no owner endpoint to enter or clear past_due — recovery happens when Paymob retries the charge and a success webhook flips it back to active. Surface past_due as a "payment failed, update your card at the gateway" state.

Step 7 — Cancel

POST /api/v1/owner/trucks/:id/subscription/cancel — no body. This is an immediate hard cancel: it sets status: "cancelled" and stamps both cancelAtPeriodEndAt and cancelledAt to now. (The cancel_at_period_end value exists in the enum but this endpoint never produces it — there is no grace period.) If the subscription was past pending and has a gateway subscription id, the server also cancels the recurring subscription at Paymob; if still pending, it cancels the pending initial payment order instead.

POST /api/v1/owner/trucks/trk_abc123/subscription/cancel
Authorization: Bearer oat_xxx

Returns 200 with { subscription } at status: "cancelled". 404 if the truck has no subscription.

Step 7b — Reactivate

POST /api/v1/owner/trucks/:id/subscription/reactivate — no body. Resumes at Paymob, then sets status: "active" and clears the cancel timestamps. Returns 200 with { subscription }.

Two 409 CONFLICT conditions require explicit handling:

  • The subscription is pending → "Pending subscriptions cannot be reactivated. Complete checkout first." A pending subscription cannot be reactivated — start a fresh checkout (Step 2) instead.
  • There is no gateway_subscription_id → "Subscription has no gateway subscription id. Start a new checkout."
POST /api/v1/owner/trucks/trk_abc123/subscription/reactivate
Authorization: Bearer oat_xxx

States

StateMeaningHow you got hereWhat's allowed next
pendingRow exists, checkout issued, not live.POST .../subscription (subscribe / switch / retry).Pay in webview → webhook activates; or cancel; or re-subscribe (resets to a new pending). Cannot reactivate.
activeSubscribed and paid; truck is live on the plan.Webhook POST /payments/webhook/paymob after payment, or a successful renewal.Renew (stays active); fail → past_due; owner cancel → cancelled.
past_dueA recurring charge failed/was suspended.POST /payments/webhook/paymob/subscriptions with `past_duefailed
cancel_at_period_endEnum value only — the API never sets it. Cancel is immediate.(Not produced by current endpoints.)n/a
cancelledRecurring subscription stopped.Owner cancel (immediate), or gateway `cancelledcanceled

Transitions

  • (none) → pending — Owner: POST /api/v1/owner/trucks/:id/subscription (returns checkout; not live yet).
  • pending → active — Paymob: POST /payments/webhook/paymob after the owner pays (sets 1-month period + nextBillingAt).
  • active → active (renewed) — Paymob: POST /payments/webhook/paymob/subscriptions (active|started|paid).
  • active → past_due — Paymob: POST /payments/webhook/paymob/subscriptions (past_due|failed|suspended).
  • active / past_due → cancelled — Owner: POST .../subscription/cancel (immediate; also cancels at Paymob if a gateway sub exists).
  • pending → cancelled — Owner: POST .../subscription/cancel (also cancels the pending initial payment order).
  • active / past_due → cancelled — Paymob: POST /payments/webhook/paymob/subscriptions (cancelled|canceled|ended).
  • cancelled → active — Owner: POST .../subscription/reactivate (requires a gateway subscription id; resumes at Paymob).
  • pending → pending (reset) — Owner: POST .../subscription again (upsert overwrites the row, issues a new checkout).

Implementation Notes

  • Treating the subscribe 201 as "subscribed". It returns status: "pending". → Open paymentIframeUrl, then re-fetch the subscription until status === "active".
  • Trusting the /payments/return redirect. It is display-only; activation is the webhook. → Poll the subscription after the webview closes; tolerate a short delay where it is still pending.
  • Expecting paymentIframeUrl / clientSecret later. They are serialized only while status === "pending"; every other status nulls them. → Capture them from the subscribe 201 (or a pending show response); they will not reappear once active.
  • Passing the subscription id in the path. :id is always the truck id (trk_...), never the subscription id (tsb_...). All owner subscription routes are keyed by truck.
  • Re-subscribing to "switch plan" on an active sub without warning the user. It overwrites the same row back to pending and clears period/billing/cancel fields, then forces a new checkout. → Confirm before re-subscribing on a live subscription.
  • Expecting 422 for an unavailable plan. An unknown/inactive planId returns 404 NOT_FOUND; 422 is only for a malformed body (length 5–40). → Handle a 404 from subscribe as "plan unavailable".
  • Treating cancel as "cancel at period end". Despite the field name cancelAtPeriodEndAt, cancel is immediate. → Do not promise the owner access until period end; access ends now.
  • Trying to reactivate a pending subscription. Returns 409 CONFLICT. → Start a fresh checkout instead. Also 409 if there is no gateway subscription id.
  • Documenting/calling the webhooks as client endpoints. /payments/webhook/paymob and /payments/webhook/paymob/subscriptions are outside /api/v1, public, and gateway-only. → The mobile app never calls them.
  • Building a "fix past_due" button. There is no owner endpoint to clear past_due. → Surface it as "payment failed — update card at the gateway"; recovery is a successful Paymob retry.
  • Rendering raw monthlyAmountMinor. 9900 is 99.00, not 9900. → Divide by 100 (and totalAmountMinor on the payment order) for display.

On this page