Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Apply a plan changed in Manage billing on return

rcourtman5c2b4de3136b
diff --git a/docs/release-control/v6/internal/subsystems/cloud-paid.md b/docs/release-control/v6/internal/subsystems/cloud-paid.md
index 858f3b5879..7a64165142 100644
--- a/docs/release-control/v6/internal/subsystems/cloud-paid.md
+++ b/docs/release-control/v6/internal/subsystems/cloud-paid.md
@@ -3454,14 +3454,20 @@ load. `msp_solo` (3 client workspaces) is the first paid step above the
In the portal, provider-hosted mode shows the `billing` section as **Plan**
(`internal/cloudcp/portal/frontend/src/provider_plan.ts`), never Pulse-hosted
-or self-hosted billing. The section shows the evaluation or paid plan, its
-expiry, how many client workspaces are in use, and, on an evaluation, the plans
-above the current cap with their monthly or annual price. Owners and admins
-get Buy, Manage billing and "Apply my purchase now"; read-only members are told
-who can buy. After Stripe returns to `?provider_msp_checkout=complete` the
-panel keeps refreshing the licence until the purchase lands. It then polls the
-plan until the restarted control plane serves the new plan, and it never
-reloads the page. Regression coverage:
+or self-hosted billing. The section shows the evaluation or paid plan, how
+many client workspaces are in use, the evaluation's expiry, and, on an
+evaluation, the plans above the current cap with their monthly or annual
+price. A paid plan shows no licence date while it renews; once the licence is
+inside its 14-day grace the panel says the subscription has not renewed and
+when clients lose the plan. Owners and admins get Buy, Manage billing and an
+immediate refresh ("Apply my purchase now", or "Apply it now" on a paid plan);
+read-only members are told who can buy. After Stripe returns to
+`?provider_msp_checkout=complete` the panel keeps refreshing the licence until
+the purchase lands. It then polls the plan until the restarted control plane
+serves the new plan, and it never reloads the page. Manage billing returns to
+`?provider_msp_checkout=billing`, and the panel checks the licence twice so a
+plan changed in the Stripe billing portal applies without a click. Regression
+coverage:
`TestLoadConfig_ProviderHostedMSPPrefersRenewedLicense` in
`internal/cloudcp/config_test.go`,
`TestProviderMSPLicenseRefreshInstallsThePaidLicenceAndRestarts` and
diff --git a/internal/cloudcp/portal/dist/build_manifest.json b/internal/cloudcp/portal/dist/build_manifest.json
index 5fc0a117d8..f90f4e95b4 100644
--- a/internal/cloudcp/portal/dist/build_manifest.json
+++ b/internal/cloudcp/portal/dist/build_manifest.json
@@ -1,5 +1,5 @@
{
- "source_hash": "8b4817955ab28d58e1356be2dd1e96a4c66619d2b802ff49aa56f4f013427a80",
+ "source_hash": "02b2a130bad03a89d6b916726206d5941bd0b23de5a2475f534aaf69419d7aba",
"build_inputs": [
"package.json",
"tsconfig.json",
diff --git a/internal/cloudcp/portal/dist/portal_app.js b/internal/cloudcp/portal/dist/portal_app.js
index aa610ad2c0..eeda6cf1bd 100644
--- a/internal/cloudcp/portal/dist/portal_app.js
+++ b/internal/cloudcp/portal/dist/portal_app.js
@@ -2722,7 +2722,13 @@
function clientWorkspaces(count) {
return count === 1 ? "1 client workspace" : String(count) + " client workspaces";
}
- function renderCurrentPlan(plan, canManage, busy, inUse) {
+ var PAID_GRACE_MS = 14 * 24 * 60 * 60 * 1e3;
+ function paidLicenceInGrace(plan, now) {
+ if (!plan.expires_at) return false;
+ var expiresAt = new Date(plan.expires_at).getTime();
+ return !isNaN(expiresAt) && expiresAt - now <= PAID_GRACE_MS;
+ }
+ function renderCurrentPlan(plan, canManage, busy, inUse, now) {
var expires = formatDate(plan.expires_at);
var title = providerPlanName(plan.plan_version);
var description;
@@ -2732,8 +2738,7 @@
description = expires ? "Your evaluation covers " + clientWorkspaces(plan.workspace_limit) + " until " + expires + ". Buy a plan to keep your clients monitored after that and to add more." : "Your evaluation covers " + clientWorkspaces(plan.workspace_limit) + ". Buy a plan to add more.";
if (expires) meta.push("Expires " + expires);
} else {
- description = "Up to " + clientWorkspaces(plan.workspace_limit) + ". Renews automatically while your subscription is active.";
- if (expires) meta.push("Licence valid to " + expires);
+ description = paidLicenceInGrace(plan, now) ? "Your subscription has not renewed. Your clients keep this plan until " + expires + ". Open Manage billing to renew or update your payment method." : "Up to " + clientWorkspaces(plan.workspace_limit) + ". Renews automatically while your subscription is active.";
if (canManage) {
cta = '<button class="btn-secondary billing-action-button" type="button" data-provider-plan-action="manage-billing"' + (busy ? " disabled" : "") + ">" + (busy === "manage-billing" ? "Opening\u2026" : "Manage billing") + "</button>";
}
@@ -2753,7 +2758,7 @@
var action = canManage ? '<button class="btn-primary billing-action-button" type="button" data-provider-plan-action="buy" data-provider-plan-version="' + escapeAttribute(option.plan_version) + '" data-provider-plan-cycle="' + escapeAttribute(option.billing_cycle) + '"' + (busy ? " disabled" : "") + ">" + (busy === "buy:" + option.plan_version ? "Opening checkout\u2026" : "Buy " + escapeText(name)) + "</button>" : "";
return '<article class="billing-action-row" data-provider-plan-offer="' + escapeAttribute(option.plan_version) + '"><div class="billing-action-main"><div class="billing-action-copy"><h3>' + escapeText(name) + "</h3><p>Up to " + escapeText(clientWorkspaces(option.workspace_limit)) + '.</p></div><div class="billing-action-meta">' + escapeText(formatProviderPlanPrice(option)) + "</div></div>" + (action ? '<div class="billing-action-cta">' + action + "</div>" : "") + "</article>";
}
- function renderProviderPlanHTML(view, canManage, inUse = -1) {
+ function renderProviderPlanHTML(view, canManage, inUse = -1, now = Date.now()) {
var parts = ['<div class="billing-section-intro"><h2>Plan</h2></div>'];
if (view.notice) {
parts.push('<p class="billing-action-meta" role="status">' + escapeText(view.notice) + "</p>");
@@ -2768,7 +2773,7 @@
}
var plan = view.plan;
if (!plan) return parts.join("");
- parts.push(renderCurrentPlan(plan, canManage, view.busy, inUse));
+ parts.push(renderCurrentPlan(plan, canManage, view.busy, inUse, now));
if (plan.evaluation) {
if (plan.plans_error) {
parts.push('<p class="billing-action-meta" role="alert">' + escapeText(plan.plans_error) + "</p>");
@@ -2794,7 +2799,7 @@
}
if (canManage && plan.license_id) {
parts.push(
- '<p class="billing-action-meta">Just paid? <button class="btn-secondary btn-compact" type="button" data-provider-plan-action="refresh"' + (view.busy ? " disabled" : "") + ">" + (view.busy === "refresh" ? "Checking\u2026" : "Apply my purchase now") + "</button></p>"
+ '<p class="billing-action-meta">' + (plan.evaluation ? "Just paid? " : "Changed your plan? ") + '<button class="btn-secondary btn-compact" type="button" data-provider-plan-action="refresh"' + (view.busy ? " disabled" : "") + ">" + (view.busy === "refresh" ? "Checking\u2026" : plan.evaluation ? "Apply my purchase now" : "Apply it now") + "</button></p>"
);
}
return parts.join("");
@@ -2809,6 +2814,7 @@
var CHECKOUT_RETURN_PARAM = "provider_msp_checkout";
var APPLY_POLL_MS = 5e3;
var APPLY_POLL_ATTEMPTS = 12;
+ var BILLING_RETURN_ATTEMPTS = 2;
var RESTART_SETTLE_MS = 8e3;
var RESTART_POLL_MS = 3e3;
var RESTART_POLL_ATTEMPTS = 20;
@@ -2914,6 +2920,14 @@
}, APPLY_POLL_MS);
});
}
+ function checkAfterBillingReturn(attempt) {
+ void refresh(false).then(function(applied) {
+ if (applied || attempt + 1 >= BILLING_RETURN_ATTEMPTS) return;
+ later(function() {
+ checkAfterBillingReturn(attempt + 1);
+ }, APPLY_POLL_MS);
+ });
+ }
document.addEventListener("click", function(event) {
var target = event.target instanceof HTMLElement ? event.target.closest("[data-provider-plan-action]") : null;
if (!target) return;
@@ -2982,6 +2996,8 @@
if (returned === "complete") {
view.notice = "Payment received. Applying your plan\u2026";
pollAfterCheckout(0);
+ } else if (returned === "billing") {
+ checkAfterBillingReturn(0);
} else {
view.notice = "Checkout was cancelled. Nothing was charged.";
}
diff --git a/internal/cloudcp/portal/frontend/src/provider_plan.test.ts b/internal/cloudcp/portal/frontend/src/provider_plan.test.ts
index 7686129440..0fd864d2d7 100644
--- a/internal/cloudcp/portal/frontend/src/provider_plan.test.ts
+++ b/internal/cloudcp/portal/frontend/src/provider_plan.test.ts
@@ -74,6 +74,26 @@ describe('renderProviderPlanHTML', () => {
expect(html).toContain('data-provider-plan-action="manage-billing"');
expect(html).not.toContain('data-provider-plan-action="buy"');
expect(html).toContain('Change plan, update your payment method or cancel renewal');
+ expect(html).toContain('Changed your plan?');
+ expect(html).not.toContain('Just paid?');
+ });
+
+ it('keeps the licence date out of a renewing plan and warns once renewal has stopped', () => {
+ const now = Date.parse('2026-10-01T00:00:00Z');
+ const renewing = renderProviderPlanHTML(view({
+ plan: evaluationPlan({ plan_version: 'msp_solo', evaluation: false, workspace_limit: 3, expires_at: '2026-11-06T00:00:00Z' }),
+ }), true, 2, now);
+ expect(renewing).toContain('Renews automatically');
+ expect(renewing).toContain('2 of 3 client workspaces in use');
+ expect(renewing).not.toContain('2026');
+ expect(renewing).not.toContain('Nov');
+
+ const lapsed = renderProviderPlanHTML(view({
+ plan: evaluationPlan({ plan_version: 'msp_solo', evaluation: false, workspace_limit: 3, expires_at: '2026-10-10T00:00:00Z' }),
+ }), true, 2, now);
+ expect(lapsed).toContain('Your subscription has not renewed.');
+ expect(lapsed).toContain('Open Manage billing to renew');
+ expect(lapsed).not.toContain('Renews automatically');
});
it('keeps the evaluation visible when plans cannot be loaded', () => {
@@ -205,6 +225,36 @@ describe('installProviderPlan', () => {
expect(document.getElementById(PROVIDER_PLAN_ROOT_ID)!.innerHTML).toContain('Manage billing');
});
+ it('applies a plan changed in Manage billing when the provider comes back', async () => {
+ api.fetchPlan = vi.fn().mockResolvedValue(evaluationPlan({ plan_version: 'msp_solo', evaluation: false, workspace_limit: 3 }));
+ api.refreshLicense = vi.fn()
+ .mockResolvedValueOnce({ status: 'active', changed: false })
+ .mockResolvedValueOnce({ status: 'active', changed: true, restart_scheduled: true });
+ const { store, controller } = install(providerBootstrap(), '?provider_msp_checkout=billing');
+ await controller.load();
+ expect(store.getShellState().activeSection).toBe('billing');
+ expect(controller.view().notice).toBe('');
+ await flush();
+ expect(api.refreshLicense).toHaveBeenCalledTimes(1);
+ timers.shift()!();
+ await flush();
+ expect(api.refreshLicense).toHaveBeenCalledTimes(2);
+ expect(controller.view().notice).toContain('being applied');
+ expect(toast).not.toHaveBeenCalled();
+ });
+
+ it('checks a Manage billing return only twice when nothing changed', async () => {
+ api.refreshLicense = vi.fn().mockResolvedValue({ status: 'active', changed: false });
+ const { controller } = install(providerBootstrap(), '?provider_msp_checkout=billing');
+ await controller.load();
+ await flush();
+ timers.shift()!();
+ await flush();
+ expect(api.refreshLicense).toHaveBeenCalledTimes(2);
+ expect(timers).toHaveLength(0);
+ expect(controller.view().notice).toBe('');
+ });
+
it('says nothing was charged when checkout is cancelled', () => {
const { controller } = install(providerBootstrap(), '?provider_msp_checkout=cancelled');
expect(controller.view().notice).toBe('Checkout was cancelled. Nothing was charged.');
diff --git a/internal/cloudcp/portal/frontend/src/provider_plan.ts b/internal/cloudcp/portal/frontend/src/provider_plan.ts
index 8c5d10e3fa..43d459edcf 100644
--- a/internal/cloudcp/portal/frontend/src/provider_plan.ts
+++ b/internal/cloudcp/portal/frontend/src/provider_plan.ts
@@ -69,7 +69,18 @@ function clientWorkspaces(count: number): string {
return count === 1 ? '1 client workspace' : String(count) + ' client workspaces';
}
-function renderCurrentPlan(plan: ProviderPlanState, canManage: boolean, busy: string, inUse: number): string {
+// A paid licence is signed to the paid period end plus 14 days of grace and
+// re-signed as the subscription renews, so one that ends inside that window
+// means the subscription has stopped renewing.
+const PAID_GRACE_MS = 14 * 24 * 60 * 60 * 1000;
+
+function paidLicenceInGrace(plan: ProviderPlanState, now: number): boolean {
+ if (!plan.expires_at) return false;
+ var expiresAt = new Date(plan.expires_at).getTime();
+ return !isNaN(expiresAt) && expiresAt - now <= PAID_GRACE_MS;
+}
+
+function renderCurrentPlan(plan: ProviderPlanState, canManage: boolean, busy: string, inUse: number, now: number): string {
var expires = formatDate(plan.expires_at);
var title = providerPlanName(plan.plan_version);
var description: string;
@@ -81,8 +92,11 @@ function renderCurrentPlan(plan: ProviderPlanState, canManage: boolean, busy: st
: 'Your evaluation covers ' + clientWorkspaces(plan.workspace_limit) + '. Buy a plan to add more.';
if (expires) meta.push('Expires ' + expires);
} else {
- description = 'Up to ' + clientWorkspaces(plan.workspace_limit) + '. Renews automatically while your subscription is active.';
- if (expires) meta.push('Licence valid to ' + expires);
+ // The renewal date lives in Manage billing; the licence date only
+ // matters, and only shows, once the subscription has stopped renewing.
+ description = paidLicenceInGrace(plan, now)
+ ? 'Your subscription has not renewed. Your clients keep this plan until ' + expires + '. Open Manage billing to renew or update your payment method.'
+ : 'Up to ' + clientWorkspaces(plan.workspace_limit) + '. Renews automatically while your subscription is active.';
if (canManage) {
cta = '<button class="btn-secondary billing-action-button" type="button" data-provider-plan-action="manage-billing"' +
(busy ? ' disabled' : '') + '>' + (busy === 'manage-billing' ? 'Opening…' : 'Manage billing') + '</button>';
@@ -131,7 +145,7 @@ function renderOffer(option: ProviderPlanOption, canManage: boolean, busy: strin
);
}
-export function renderProviderPlanHTML(view: ProviderPlanView, canManage: boolean, inUse = -1): string {
+export function renderProviderPlanHTML(view: ProviderPlanView, canManage: boolean, inUse = -1, now = Date.now()): string {
var parts: string[] = ['<div class="billing-section-intro"><h2>Plan</h2></div>'];
if (view.notice) {
parts.push('<p class="billing-action-meta" role="status">' + escapeText(view.notice) + '</p>');
@@ -146,7 +160,7 @@ export function renderProviderPlanHTML(view: ProviderPlanView, canManage: boolea
}
var plan = view.plan;
if (!plan) return parts.join('');
- parts.push(renderCurrentPlan(plan, canManage, view.busy, inUse));
+ parts.push(renderCurrentPlan(plan, canManage, view.busy, inUse, now));
if (plan.evaluation) {
if (plan.plans_error) {
@@ -170,9 +184,9 @@ export function renderProviderPlanHTML(view: ProviderPlanView, canManage: boolea
if (canManage && plan.license_id) {
parts.push(
- '<p class="billing-action-meta">Just paid? ' +
+ '<p class="billing-action-meta">' + (plan.evaluation ? 'Just paid? ' : 'Changed your plan? ') +
'<button class="btn-secondary btn-compact" type="button" data-provider-plan-action="refresh"' + (view.busy ? ' disabled' : '') + '>' +
- (view.busy === 'refresh' ? 'Checking…' : 'Apply my purchase now') +
+ (view.busy === 'refresh' ? 'Checking…' : (plan.evaluation ? 'Apply my purchase now' : 'Apply it now')) +
'</button>' +
'</p>'
);
@@ -214,6 +228,9 @@ function providerAccount(accounts: PortalAccountSummary[] | undefined): PortalAc
const CHECKOUT_RETURN_PARAM = 'provider_msp_checkout';
const APPLY_POLL_MS = 5000;
const APPLY_POLL_ATTEMPTS = 12;
+// Most visits to Manage billing change nothing, so a return checks twice (the
+// plan-change webhook can trail the redirect) rather than polling.
+const BILLING_RETURN_ATTEMPTS = 2;
// The control plane restarts to apply a changed licence; give it a moment,
// then poll the plan until it answers with the new one.
const RESTART_SETTLE_MS = 8000;
@@ -320,6 +337,13 @@ export function installProviderPlan(deps: ProviderPlanDeps): ProviderPlanControl
});
}
+ function checkAfterBillingReturn(attempt: number) {
+ void refresh(false).then(function(applied) {
+ if (applied || attempt + 1 >= BILLING_RETURN_ATTEMPTS) return;
+ later(function() { checkAfterBillingReturn(attempt + 1); }, APPLY_POLL_MS);
+ });
+ }
+
document.addEventListener('click', function(event) {
var target = event.target instanceof HTMLElement ? event.target.closest('[data-provider-plan-action]') as HTMLElement | null : null;
if (!target) return;
@@ -391,6 +415,8 @@ export function installProviderPlan(deps: ProviderPlanDeps): ProviderPlanControl
if (returned === 'complete') {
view.notice = 'Payment received. Applying your plan…';
pollAfterCheckout(0);
+ } else if (returned === 'billing') {
+ checkAfterBillingReturn(0);
} else {
view.notice = 'Checkout was cancelled. Nothing was charged.';
}
diff --git a/internal/cloudcp/provider_msp_license_refresh.go b/internal/cloudcp/provider_msp_license_refresh.go
index e467fb2623..79755fb271 100644
--- a/internal/cloudcp/provider_msp_license_refresh.go
+++ b/internal/cloudcp/provider_msp_license_refresh.go
@@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net/http"
+ "net/url"
"os"
"path/filepath"
"strconv"
@@ -357,7 +358,10 @@ func (r *ProviderMSPLicenseRefresher) Checkout(ctx context.Context, planVersion,
// BillingPortal opens the Stripe billing portal for this platform's
// subscription and returns its URL.
func (r *ProviderMSPLicenseRefresher) BillingPortal(ctx context.Context) (string, error) {
- request, err := r.signedRequest("billing-portal", buildCPURL(r.cfg.BaseURL, portal.PortalPagePath, nil))
+ // Come back flagged, so the portal applies a plan changed there instead
+ // of showing the old one until the next scheduled refresh.
+ returnURL := buildCPURL(r.cfg.BaseURL, portal.PortalPagePath, url.Values{"provider_msp_checkout": {"billing"}})
+ request, err := r.signedRequest("billing-portal", returnURL)
if err != nil {
return "", err
}
diff --git a/internal/cloudcp/provider_msp_license_refresh_test.go b/internal/cloudcp/provider_msp_license_refresh_test.go
index bde33c5c9e..ba19499a8b 100644
--- a/internal/cloudcp/provider_msp_license_refresh_test.go
+++ b/internal/cloudcp/provider_msp_license_refresh_test.go
@@ -116,8 +116,8 @@ func (f *fakeProviderMSPLicenseServer) ServeHTTP(w http.ResponseWriter, r *http.
}
case "/v1/provider-msp/billing-portal":
body := f.verify(r, "billing-portal")
- if body["return_url"] != "https://msp.example.com/portal" {
- f.t.Fatalf("billing portal return_url = %v", body["return_url"])
+ if body["return_url"] != "https://msp.example.com/portal?provider_msp_checkout=billing" {
+ f.t.Fatalf("billing portal return_url = %v, want the portal flagged to apply a plan change", body["return_url"])
}
w.WriteHeader(f.portalStatus)
_, _ = w.Write([]byte(`{"url":"https://billing.stripe.com/p/session/test"}`))