@antelopejs/dms-saas 0.7.1 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/routes/tenant/tenant-upcoming-invoice.d.ts +7 -0
- package/dist/routes/tenant/tenant-upcoming-invoice.js +73 -8
- package/dist/routes/tenant/tenant-upcoming-invoice.js.map +1 -1
- package/frontend-vue/app/build/PlanChangeReview.vue +65 -16
- package/frontend-vue/app/build/plan-change-heading.ts +19 -0
- package/frontend-vue/app/components/WorkspaceCreateModal.vue +23 -5
- package/frontend-vue/app/composables/usePlanChangeReview.ts +25 -0
- package/frontend-vue/app/composables/useSaasRegistration.ts +25 -6
- package/frontend-vue/app/composables/useStripePaymentElement.ts +28 -12
- package/frontend-vue/app/custom-pages/auth/no-workspace.vue +23 -4
- package/frontend-vue/i18n/locales/saas-tenant-billing-en-GB.json +439 -432
- package/frontend-vue/i18n/locales/saas-tenant-billing-fr-FR.json +439 -432
- package/package.json +1 -1
|
@@ -10,6 +10,12 @@ export interface NextInvoiceRows {
|
|
|
10
10
|
currency: string | null;
|
|
11
11
|
billingDate: string | null;
|
|
12
12
|
}
|
|
13
|
+
export interface PlanSwitch {
|
|
14
|
+
currentPlanName: string;
|
|
15
|
+
nextPlanName: string;
|
|
16
|
+
isNextPlanFree: boolean;
|
|
17
|
+
at: Date;
|
|
18
|
+
}
|
|
13
19
|
interface NextInvoiceContext {
|
|
14
20
|
paymentMethod: PaymentMethodSummary | null;
|
|
15
21
|
billingEmail: string | null;
|
|
@@ -17,6 +23,7 @@ interface NextInvoiceContext {
|
|
|
17
23
|
planNames: ReadonlyMap<string, string>;
|
|
18
24
|
}
|
|
19
25
|
export declare function toNextInvoiceRows(preview: AvailableUpcomingInvoicePreview, context: NextInvoiceContext): NextInvoiceRows;
|
|
26
|
+
export declare function withPlanSwitch(rows: NextInvoiceRows, planSwitch: PlanSwitch, context: NextInvoiceContext): NextInvoiceRows;
|
|
20
27
|
export declare function latestPaidInvoice(invoices: Invoice[]): Invoice | null;
|
|
21
28
|
export declare function planNamesByProduct(plans: Plan[]): Map<string, string>;
|
|
22
29
|
declare const SaasTenantUpcomingInvoiceController_base: import("@antelopejs/interface-api").ControllerClass<object>;
|
|
@@ -14,6 +14,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.SaasTenantUpcomingInvoiceController = void 0;
|
|
16
16
|
exports.toNextInvoiceRows = toNextInvoiceRows;
|
|
17
|
+
exports.withPlanSwitch = withPlanSwitch;
|
|
17
18
|
exports.latestPaidInvoice = latestPaidInvoice;
|
|
18
19
|
exports.planNamesByProduct = planNamesByProduct;
|
|
19
20
|
const interface_api_1 = require("@antelopejs/interface-api");
|
|
@@ -22,6 +23,7 @@ const guards_1 = require("@antelopejs/interface-dms/guards");
|
|
|
22
23
|
const request_tenant_1 = require("@antelopejs/interface-dms/request-tenant");
|
|
23
24
|
const billing_state_1 = require("../../billing-state");
|
|
24
25
|
const db_1 = require("../../db");
|
|
26
|
+
const free_plan_1 = require("../../workspaces/free-plan");
|
|
25
27
|
const billing_1 = require("../../pages/tenant/billing");
|
|
26
28
|
const composed_text_1 = require("../../i18n/composed-text");
|
|
27
29
|
const preview_1 = require("../../upcoming-invoice/preview");
|
|
@@ -33,6 +35,9 @@ const PERCENT = 100;
|
|
|
33
35
|
const MASKED_DIGITS = "••••";
|
|
34
36
|
const PAID_ON_KEY = "saas.tenant_billing.next_invoice.paid_on";
|
|
35
37
|
const SUBSCRIPTION_LINE_KEY = "saas.tenant_billing.next_invoice.subscription_line";
|
|
38
|
+
const PAID_UNTIL_KEY = "saas.tenant_billing.next_invoice.paid_until";
|
|
39
|
+
const SWITCH_ON_KEY = "saas.tenant_billing.next_invoice.switch_on";
|
|
40
|
+
const SWITCH_FREE_DETAIL_KEY = "saas.tenant_billing.next_invoice.switch_free_detail";
|
|
36
41
|
const NOTHING_TO_INVOICE = {
|
|
37
42
|
items: [],
|
|
38
43
|
totalMinorUnits: null,
|
|
@@ -142,6 +147,47 @@ function toNextInvoiceRows(preview, context) {
|
|
|
142
147
|
billingDate: preview.billingDate,
|
|
143
148
|
};
|
|
144
149
|
}
|
|
150
|
+
function switchRow(planSwitch) {
|
|
151
|
+
const plan = planSwitch.nextPlanName;
|
|
152
|
+
return {
|
|
153
|
+
label: (0, composed_text_1.composed)(SWITCH_ON_KEY, { plan }),
|
|
154
|
+
value: planSwitch.at.toISOString(),
|
|
155
|
+
type: "date",
|
|
156
|
+
tone: "warning",
|
|
157
|
+
detail: planSwitch.isNextPlanFree
|
|
158
|
+
? (0, composed_text_1.composed)(SWITCH_FREE_DETAIL_KEY, { plan })
|
|
159
|
+
: undefined,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function withPlanSwitch(rows, planSwitch, context) {
|
|
163
|
+
if (rows.items.length > 0) {
|
|
164
|
+
const contextStart = rows.items.findIndex((row) => row.label === `${KEY_PREFIX}.total`);
|
|
165
|
+
const at = contextStart < 0 ? rows.items.length : contextStart + 1;
|
|
166
|
+
return {
|
|
167
|
+
...rows,
|
|
168
|
+
items: [
|
|
169
|
+
...rows.items.slice(0, at),
|
|
170
|
+
switchRow(planSwitch),
|
|
171
|
+
...rows.items.slice(at),
|
|
172
|
+
],
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const at = planSwitch.at.toISOString();
|
|
176
|
+
return {
|
|
177
|
+
items: [
|
|
178
|
+
{
|
|
179
|
+
label: (0, composed_text_1.composed)(PAID_UNTIL_KEY, { plan: planSwitch.currentPlanName }),
|
|
180
|
+
value: at,
|
|
181
|
+
type: "date",
|
|
182
|
+
},
|
|
183
|
+
switchRow(planSwitch),
|
|
184
|
+
...contextRows(context),
|
|
185
|
+
],
|
|
186
|
+
totalMinorUnits: null,
|
|
187
|
+
currency: null,
|
|
188
|
+
billingDate: at,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
145
191
|
function latestPaidInvoice(invoices) {
|
|
146
192
|
return invoices
|
|
147
193
|
.filter((invoice) => invoice.status === PAID_STATUS && invoice.paidAt)
|
|
@@ -156,6 +202,21 @@ function planNamesByProduct(plans) {
|
|
|
156
202
|
return productId ? [[productId, plan.name]] : [];
|
|
157
203
|
}));
|
|
158
204
|
}
|
|
205
|
+
function scheduledSwitch(subscription, plans) {
|
|
206
|
+
const { planId, pendingPlanId, pendingPlanChangeAt } = subscription ?? {};
|
|
207
|
+
if (!pendingPlanId || !pendingPlanChangeAt)
|
|
208
|
+
return null;
|
|
209
|
+
const current = plans.find((plan) => plan._id === planId);
|
|
210
|
+
const next = plans.find((plan) => plan._id === pendingPlanId);
|
|
211
|
+
if (!current || !next)
|
|
212
|
+
return null;
|
|
213
|
+
return {
|
|
214
|
+
currentPlanName: current.name,
|
|
215
|
+
nextPlanName: next.name,
|
|
216
|
+
isNextPlanFree: (0, free_plan_1.isFreePlan)(next),
|
|
217
|
+
at: new Date(pendingPlanChangeAt),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
159
220
|
async function loadContext(tenantId) {
|
|
160
221
|
const [subscription, billingInfo, invoices, plans] = await Promise.all([
|
|
161
222
|
(0, interface_database_decorators_1.GetModel)(db_1.TenantSubscriptionModel, tenantId).findOne(),
|
|
@@ -167,23 +228,27 @@ async function loadContext(tenantId) {
|
|
|
167
228
|
? await (0, billing_state_1.fetchDefaultPaymentMethod)(subscription.stripeCustomerId)
|
|
168
229
|
: null;
|
|
169
230
|
return {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
231
|
+
context: {
|
|
232
|
+
paymentMethod,
|
|
233
|
+
billingEmail: billingInfo?.billingEmail ?? null,
|
|
234
|
+
lastPayment: latestPaidInvoice(invoices),
|
|
235
|
+
planNames: planNamesByProduct(plans),
|
|
236
|
+
},
|
|
237
|
+
planSwitch: scheduledSwitch(subscription, plans),
|
|
174
238
|
};
|
|
175
239
|
}
|
|
176
240
|
class SaasTenantUpcomingInvoiceController extends (0, interface_api_1.Controller)("/api/saas/tenant/upcoming-invoice") {
|
|
177
241
|
async getNextInvoice(_user, ctx) {
|
|
178
242
|
const tenantId = (0, request_tenant_1.getRequestTenantId)(ctx);
|
|
179
243
|
const preview = await (0, preview_1.getUpcomingInvoicePreview)(tenantId);
|
|
180
|
-
if (preview.status === "absent")
|
|
181
|
-
return NOTHING_TO_INVOICE;
|
|
182
244
|
if (preview.status === "unavailable") {
|
|
183
245
|
throw new interface_api_1.HTTPResult(HTTP_SERVICE_UNAVAILABLE, "saas.errors.billing.preview_unavailable");
|
|
184
246
|
}
|
|
185
|
-
const context = await loadContext(tenantId);
|
|
186
|
-
|
|
247
|
+
const { context, planSwitch } = await loadContext(tenantId);
|
|
248
|
+
const rows = preview.status === "absent"
|
|
249
|
+
? NOTHING_TO_INVOICE
|
|
250
|
+
: toNextInvoiceRows(preview, context);
|
|
251
|
+
return planSwitch ? withPlanSwitch(rows, planSwitch, context) : rows;
|
|
187
252
|
}
|
|
188
253
|
}
|
|
189
254
|
exports.SaasTenantUpcomingInvoiceController = SaasTenantUpcomingInvoiceController;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tenant-upcoming-invoice.js","sourceRoot":"","sources":["../../../src/routes/tenant/tenant-upcoming-invoice.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"tenant-upcoming-invoice.js","sourceRoot":"","sources":["../../../src/routes/tenant/tenant-upcoming-invoice.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AA8MA,8CA0BC;AAqBD,wCAkCC;AAGD,8CAWC;AAGD,gDAOC;AApTD,6DAMmC;AACnC,6FAAqE;AAUrE,6DAA0E;AAC1E,6EAA8E;AAE9E,uDAG6B;AAC7B,iCAQkB;AAClB,0DAAwD;AACxD,wDAAyD;AACzD,4DAKkC;AAClC,4DAA2E;AAC3E,6CAAmD;AAEnD,MAAM,wBAAwB,GAAG,GAAG,CAAC;AACrC,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GAAG,MAAM,IAAA,mCAAyB,EAAC,QAAQ,CAAC,CAAC;QAC1D,IAAI,OAAO,CAAC,MAAM,KAAK,aAAa,EAAE,CAAC;YACrC,MAAM,IAAI,0BAAU,CAClB,wBAAwB,EACxB,yCAAyC,CAC1C,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC,CAAC;QAC5D,MAAM,IAAI,GACR,OAAO,CAAC,MAAM,KAAK,QAAQ;YACzB,CAAC,CAAC,kBAAkB;YACpB,CAAC,CAAC,iBAAiB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1C,OAAO,UAAU,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvE,CAAC;CACF;AA1BD,kFA0BC;AAtBO;IADL,IAAA,mBAAG,EAAC,GAAG,CAAC;IAEN,WAAA,IAAA,+BAAsB,EAAC,qBAAW,EAAE;QACnC,sBAAsB,EAAE,IAAI;KAC7B,CAAC,CAAA;IAED,WAAA,IAAA,uBAAO,GAAE,CAAA;;;;yEAgBX"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
import { computed, onMounted, ref } from 'vue'
|
|
3
3
|
import { LABEL_DAY_FORMAT, SENTENCE_DAY_FORMAT } from './day-formats'
|
|
4
|
-
import { planChangeHeadingCase } from './plan-change-heading'
|
|
4
|
+
import { planChangeHeadingCase, targetPlanEyebrow } from './plan-change-heading'
|
|
5
5
|
|
|
6
6
|
interface SummaryRow {
|
|
7
7
|
key: string
|
|
@@ -37,6 +37,8 @@ const planIntervalLabel = usePlanIntervalLabel('saas.workspace.plan.interval')
|
|
|
37
37
|
const preview = ref<PlanChangePreview | null>(null)
|
|
38
38
|
const isLoading = ref(true)
|
|
39
39
|
const loadFailure = ref<string | null>(null)
|
|
40
|
+
/** What the workspace holds beyond the target plan: the change is refused until it is brought within. */
|
|
41
|
+
const incompatibilities = ref<PlanChangeIncompatibility[] | null>(null)
|
|
40
42
|
const isSubmitting = ref(false)
|
|
41
43
|
const submitFailure = ref<string | null>(null)
|
|
42
44
|
|
|
@@ -82,14 +84,25 @@ const dueToday = computed(() => {
|
|
|
82
84
|
})
|
|
83
85
|
|
|
84
86
|
const headingCase = computed(() =>
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
incompatibilities.value
|
|
88
|
+
? 'incompatible'
|
|
89
|
+
: planChangeHeadingCase(
|
|
90
|
+
preview.value,
|
|
91
|
+
props.currentPlan,
|
|
92
|
+
props.plan,
|
|
93
|
+
props.isResubscription,
|
|
94
|
+
),
|
|
91
95
|
)
|
|
92
96
|
|
|
97
|
+
const targetEyebrow = computed(() => {
|
|
98
|
+
const eyebrow = targetPlanEyebrow(preview.value, !!incompatibilities.value)
|
|
99
|
+
if (eyebrow === 'from_date')
|
|
100
|
+
return t(`${KEY_PREFIX}.from_date`, {
|
|
101
|
+
date: day(preview.value?.effectiveAt),
|
|
102
|
+
})
|
|
103
|
+
return eyebrow ? t(`${KEY_PREFIX}.${eyebrow}`) : null
|
|
104
|
+
})
|
|
105
|
+
|
|
93
106
|
const heading = computed(() => {
|
|
94
107
|
const params = {
|
|
95
108
|
plan: props.plan.name,
|
|
@@ -177,10 +190,15 @@ const canConfirm = computed(
|
|
|
177
190
|
async function loadPreview(): Promise<void> {
|
|
178
191
|
isLoading.value = true
|
|
179
192
|
loadFailure.value = null
|
|
193
|
+
incompatibilities.value = null
|
|
180
194
|
try {
|
|
181
195
|
preview.value = await previewChange(props.plan._id)
|
|
182
196
|
} catch (error) {
|
|
183
|
-
|
|
197
|
+
preview.value = null
|
|
198
|
+
incompatibilities.value = readPlanChangeIncompatibilities(error)
|
|
199
|
+
if (!incompatibilities.value) {
|
|
200
|
+
loadFailure.value = resolveApiError(error, `${KEY_PREFIX}.load_failed`)
|
|
201
|
+
}
|
|
184
202
|
} finally {
|
|
185
203
|
isLoading.value = false
|
|
186
204
|
}
|
|
@@ -206,6 +224,12 @@ async function confirm(): Promise<void> {
|
|
|
206
224
|
emit('done')
|
|
207
225
|
} catch (error) {
|
|
208
226
|
if (await offerPendingCheckout(error, props.plan._id)) return
|
|
227
|
+
// The inventory changed between the review and the confirmation.
|
|
228
|
+
incompatibilities.value = readPlanChangeIncompatibilities(error)
|
|
229
|
+
if (incompatibilities.value) {
|
|
230
|
+
preview.value = null
|
|
231
|
+
return
|
|
232
|
+
}
|
|
209
233
|
submitFailure.value = resolveApiError(
|
|
210
234
|
error,
|
|
211
235
|
'saas.workspace.plan.change_error',
|
|
@@ -240,14 +264,8 @@ onMounted(() => void loadPreview())
|
|
|
240
264
|
<p class="text-muted text-sm">{{ priceOf(currentPlan) }}</p>
|
|
241
265
|
</DmsCard>
|
|
242
266
|
<DmsCard :padded="true" selected>
|
|
243
|
-
<p class="text-muted text-xs">
|
|
244
|
-
{{
|
|
245
|
-
isDowngrade
|
|
246
|
-
? $t(`${KEY_PREFIX}.from_date`, {
|
|
247
|
-
date: day(preview?.effectiveAt),
|
|
248
|
-
})
|
|
249
|
-
: $t(`${KEY_PREFIX}.from_today`)
|
|
250
|
-
}}
|
|
267
|
+
<p v-if="targetEyebrow" class="text-muted text-xs">
|
|
268
|
+
{{ targetEyebrow }}
|
|
251
269
|
</p>
|
|
252
270
|
<p class="font-semibold">{{ plan.name }}</p>
|
|
253
271
|
<p class="text-muted text-sm">{{ priceOf(plan) }}</p>
|
|
@@ -310,6 +328,36 @@ onMounted(() => void loadPreview())
|
|
|
310
328
|
<USkeleton class="h-6 w-1/2" />
|
|
311
329
|
</div>
|
|
312
330
|
|
|
331
|
+
<UAlert
|
|
332
|
+
v-else-if="incompatibilities"
|
|
333
|
+
color="warning"
|
|
334
|
+
variant="subtle"
|
|
335
|
+
icon="i-ph-prohibit"
|
|
336
|
+
:title="$t(`${KEY_PREFIX}.incompatible_list`, { plan: plan.name })"
|
|
337
|
+
>
|
|
338
|
+
<template #description>
|
|
339
|
+
<ul class="flex list-disc flex-col gap-1 ps-4">
|
|
340
|
+
<li
|
|
341
|
+
v-for="(issue, index) in incompatibilities"
|
|
342
|
+
:key="`${issue.code}-${issue.resource ?? index}`"
|
|
343
|
+
>
|
|
344
|
+
{{ issue.message }}
|
|
345
|
+
</li>
|
|
346
|
+
</ul>
|
|
347
|
+
</template>
|
|
348
|
+
<template #actions>
|
|
349
|
+
<UButton
|
|
350
|
+
color="warning"
|
|
351
|
+
variant="soft"
|
|
352
|
+
size="xs"
|
|
353
|
+
icon="i-ph-arrow-clockwise"
|
|
354
|
+
@click="loadPreview"
|
|
355
|
+
>
|
|
356
|
+
{{ $t(`${KEY_PREFIX}.check_again`) }}
|
|
357
|
+
</UButton>
|
|
358
|
+
</template>
|
|
359
|
+
</UAlert>
|
|
360
|
+
|
|
313
361
|
<UAlert
|
|
314
362
|
v-else-if="loadFailure"
|
|
315
363
|
color="error"
|
|
@@ -399,6 +447,7 @@ onMounted(() => void loadPreview())
|
|
|
399
447
|
{{ $t(`${KEY_PREFIX}.back`) }}
|
|
400
448
|
</UButton>
|
|
401
449
|
<UButton
|
|
450
|
+
v-if="!incompatibilities"
|
|
402
451
|
:color="isDowngrade ? 'warning' : 'primary'"
|
|
403
452
|
:icon="isDowngrade ? 'i-ph-calendar-check' : 'i-ph-lock-simple'"
|
|
404
453
|
:disabled="!canConfirm"
|
|
@@ -79,3 +79,22 @@ export function checkoutHeadingCase(
|
|
|
79
79
|
if (isResubscription) return 'resubscribe'
|
|
80
80
|
return isTrial ? 'trial' : 'upgrade'
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
/** When the chosen plan starts, as its card in the review says it. */
|
|
84
|
+
export type TargetPlanEyebrow = 'from_today' | 'from_date'
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The eyebrow of the chosen plan's card: from today, or from the renewal for
|
|
88
|
+
* a downgrade; none until the change is priced, and none for a change the
|
|
89
|
+
* workspace does not fit, which starts on no date at all.
|
|
90
|
+
*
|
|
91
|
+
* @param change The priced change, null while it is priced or once refused
|
|
92
|
+
* @param isRefused The workspace holds more than the chosen plan allows
|
|
93
|
+
*/
|
|
94
|
+
export function targetPlanEyebrow(
|
|
95
|
+
change: PricedChange | null,
|
|
96
|
+
isRefused: boolean,
|
|
97
|
+
): TargetPlanEyebrow | null {
|
|
98
|
+
if (isRefused || !change) return null
|
|
99
|
+
return change.kind === 'downgrade' ? 'from_date' : 'from_today'
|
|
100
|
+
}
|
|
@@ -154,6 +154,13 @@ function selectPlan(plan: CreatePlanOption): void {
|
|
|
154
154
|
submitError.value = null
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
+
// Not a `useApiData` read: each call creates a SetupIntent, whose client
|
|
158
|
+
// secret is the card form's alone, never cached nor shared.
|
|
159
|
+
async function createSetupIntent(): Promise<string> {
|
|
160
|
+
const setup = await $authFetch<SetupIntent>(SETUP_INTENT_ENDPOINT)
|
|
161
|
+
return setup.clientSecret
|
|
162
|
+
}
|
|
163
|
+
|
|
157
164
|
async function initStripeElement(): Promise<void> {
|
|
158
165
|
if (!publishableKey.value) {
|
|
159
166
|
// A missing key would render an empty card box and fail every submit
|
|
@@ -161,12 +168,10 @@ async function initStripeElement(): Promise<void> {
|
|
|
161
168
|
cardError.value = t(`${KEYS}.error.card_setup`)
|
|
162
169
|
return
|
|
163
170
|
}
|
|
164
|
-
|
|
165
|
-
// secret is the card form's alone, never cached nor shared.
|
|
166
|
-
const setup = await $authFetch<SetupIntent>(SETUP_INTENT_ENDPOINT)
|
|
171
|
+
const clientSecret = await createSetupIntent()
|
|
167
172
|
stripeHandle.value = useStripePaymentElement({
|
|
168
173
|
publishableKey: publishableKey.value,
|
|
169
|
-
clientSecret
|
|
174
|
+
clientSecret,
|
|
170
175
|
containerId: PAYMENT_ELEMENT_ID,
|
|
171
176
|
billingEmail: () => user.value?.email ?? '',
|
|
172
177
|
})
|
|
@@ -240,13 +245,25 @@ function reportSubmitError(error: unknown): void {
|
|
|
240
245
|
submitError.value = message
|
|
241
246
|
}
|
|
242
247
|
|
|
248
|
+
// The card was confirmed, then the creation refused (another card is needed,
|
|
249
|
+
// a creation already running): its SetupIntent is spent, and confirming it
|
|
250
|
+
// again with the next card fails with a bare Stripe processing error.
|
|
251
|
+
async function renewCard(): Promise<void> {
|
|
252
|
+
try {
|
|
253
|
+
await stripeHandle.value?.renew(await createSetupIntent())
|
|
254
|
+
} catch (error) {
|
|
255
|
+
cardError.value = resolveApiError(error, `${KEYS}.error.card_setup`)
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
243
259
|
async function submit(): Promise<void> {
|
|
244
260
|
if (isSubmitting.value || !validate()) return
|
|
245
261
|
cardError.value = null
|
|
246
262
|
submitError.value = null
|
|
247
263
|
isSubmitting.value = true
|
|
264
|
+
let payment: ConfirmedPayment | null = null
|
|
248
265
|
try {
|
|
249
|
-
|
|
266
|
+
payment = await confirmCard()
|
|
250
267
|
if (!payment.isConfirmed) return
|
|
251
268
|
const name = workspaceName.value.trim()
|
|
252
269
|
const created = await $authFetch<CreatedWorkspace>(CREATE_ENDPOINT, {
|
|
@@ -260,6 +277,7 @@ async function submit(): Promise<void> {
|
|
|
260
277
|
props.onSuccessCallback?.(created.tenantId, name)
|
|
261
278
|
} catch (error) {
|
|
262
279
|
reportSubmitError(error)
|
|
280
|
+
if (payment?.paymentMethodId) await renewCard()
|
|
263
281
|
} finally {
|
|
264
282
|
isSubmitting.value = false
|
|
265
283
|
}
|
|
@@ -8,6 +8,8 @@ import { PLAN_UNLIMITED } from '../build/plan-catalog'
|
|
|
8
8
|
/** Query parameter naming the plan to review on arrival on the billing page. */
|
|
9
9
|
export const UPGRADE_QUERY_PARAM = 'upgrade'
|
|
10
10
|
|
|
11
|
+
const INCOMPATIBLE_CODE = 'saas.errors.plan.incompatible'
|
|
12
|
+
|
|
11
13
|
const UNLIMITED_RANK = Number.POSITIVE_INFINITY
|
|
12
14
|
|
|
13
15
|
/** One feature that differs between two plans. */
|
|
@@ -33,6 +35,29 @@ export interface OfferedPlans {
|
|
|
33
35
|
tooSmall: OfferedPlanView[]
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
/** Something the workspace holds that the target plan does not allow. */
|
|
39
|
+
export interface PlanChangeIncompatibility {
|
|
40
|
+
code: string
|
|
41
|
+
message: string
|
|
42
|
+
resource?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* What the workspace must bring within the target plan before it can move,
|
|
47
|
+
* read from the 409 a plan change preview or confirmation answers with; null
|
|
48
|
+
* for any other failure.
|
|
49
|
+
*/
|
|
50
|
+
export function readPlanChangeIncompatibilities(
|
|
51
|
+
error: unknown,
|
|
52
|
+
): PlanChangeIncompatibility[] | null {
|
|
53
|
+
const data = (error as { data?: unknown } | undefined)?.data as
|
|
54
|
+
| { code?: unknown; incompatibilities?: unknown }
|
|
55
|
+
| undefined
|
|
56
|
+
if (data?.code !== INCOMPATIBLE_CODE) return null
|
|
57
|
+
const list = data.incompatibilities
|
|
58
|
+
return Array.isArray(list) ? (list as PlanChangeIncompatibility[]) : null
|
|
59
|
+
}
|
|
60
|
+
|
|
36
61
|
/**
|
|
37
62
|
* The plan id an arrival link asks to review (`?upgrade=<planId>`), or null.
|
|
38
63
|
* A repeated parameter takes its first non-empty value.
|
|
@@ -460,16 +460,21 @@ export function useSaasRegistration(options: UseSaasRegistrationOptions = {}) {
|
|
|
460
460
|
serverFieldErrors.value = rest
|
|
461
461
|
}
|
|
462
462
|
|
|
463
|
+
// Not a `useApiData` read: each call creates a SetupIntent, whose client
|
|
464
|
+
// secret is the card form's alone, never cached nor shared.
|
|
465
|
+
async function createSetupIntent(): Promise<string | undefined> {
|
|
466
|
+
const setup = await $authFetch<SetupIntentResponse>(SETUP_INTENT_ENDPOINT)
|
|
467
|
+
return setup.clientSecret || undefined
|
|
468
|
+
}
|
|
469
|
+
|
|
463
470
|
async function mountPaymentElement(): Promise<void> {
|
|
464
471
|
const publishableKey = saasConfig?.stripePublishableKey
|
|
465
472
|
if (!publishableKey) return
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
const setup = await $authFetch<SetupIntentResponse>(SETUP_INTENT_ENDPOINT)
|
|
469
|
-
if (!setup.clientSecret) return
|
|
473
|
+
const clientSecret = await createSetupIntent()
|
|
474
|
+
if (!clientSecret) return
|
|
470
475
|
stripeHandle.value = useStripePaymentElement({
|
|
471
476
|
publishableKey,
|
|
472
|
-
clientSecret
|
|
477
|
+
clientSecret,
|
|
473
478
|
containerId: paymentElementId,
|
|
474
479
|
billingEmail: () => form.email,
|
|
475
480
|
})
|
|
@@ -505,6 +510,18 @@ export function useSaasRegistration(options: UseSaasRegistrationOptions = {}) {
|
|
|
505
510
|
}
|
|
506
511
|
}
|
|
507
512
|
|
|
513
|
+
// A refusal after the card was confirmed leaves its SetupIntent spent:
|
|
514
|
+
// the next card must go through a fresh one, or Stripe refuses it with a
|
|
515
|
+
// bare processing error.
|
|
516
|
+
async function renewCard(): Promise<void> {
|
|
517
|
+
try {
|
|
518
|
+
const clientSecret = await createSetupIntent()
|
|
519
|
+
if (clientSecret) await stripeHandle.value?.renew(clientSecret)
|
|
520
|
+
} catch (error) {
|
|
521
|
+
errorMessage.value = resolveApiError(error, LOAD_ERROR_KEY)
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
508
525
|
function reportFailure(error: unknown): void {
|
|
509
526
|
const field = registrationFieldOf(readApiErrorKey(error))
|
|
510
527
|
if (!field) {
|
|
@@ -549,11 +566,12 @@ export function useSaasRegistration(options: UseSaasRegistrationOptions = {}) {
|
|
|
549
566
|
|
|
550
567
|
isSubmitting.value = true
|
|
551
568
|
let tenantId: string | null = null
|
|
569
|
+
let paymentMethodId: string | null | undefined
|
|
552
570
|
try {
|
|
553
571
|
// Inside the guard: freezing the capture reads the consumer's object,
|
|
554
572
|
// and a consumer's object is never guaranteed to be serialisable.
|
|
555
573
|
const submission = captureSubmission()
|
|
556
|
-
|
|
574
|
+
paymentMethodId = isPaymentStepVisible.value
|
|
557
575
|
? await confirmPaymentMethod()
|
|
558
576
|
: undefined
|
|
559
577
|
if (paymentMethodId === null) return null
|
|
@@ -570,6 +588,7 @@ export function useSaasRegistration(options: UseSaasRegistrationOptions = {}) {
|
|
|
570
588
|
// through a flow that would create a second account.
|
|
571
589
|
if (tenantId) return tenantId
|
|
572
590
|
reportFailure(error)
|
|
591
|
+
if (paymentMethodId) await renewCard()
|
|
573
592
|
return null
|
|
574
593
|
} finally {
|
|
575
594
|
isSubmitting.value = false
|
|
@@ -54,6 +54,12 @@ interface SetupConfirmation {
|
|
|
54
54
|
|
|
55
55
|
interface StripePaymentElementHandle {
|
|
56
56
|
confirmAndGetPaymentMethod: () => Promise<ConfirmResult>;
|
|
57
|
+
/**
|
|
58
|
+
* Swap in a fresh SetupIntent. A confirmed SetupIntent is spent: Stripe
|
|
59
|
+
* refuses to confirm it again with another card, so a form that keeps the
|
|
60
|
+
* visitor after a confirmed card was refused must call this first.
|
|
61
|
+
*/
|
|
62
|
+
renew: (clientSecret: string) => Promise<void>;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
const UNKNOWN_STRIPE_ERROR = "Stripe could not confirm the card";
|
|
@@ -99,20 +105,15 @@ export function useStripePaymentElement(
|
|
|
99
105
|
): StripePaymentElementHandle {
|
|
100
106
|
let stripeInstance: any = null;
|
|
101
107
|
let elementsInstance: any = null;
|
|
108
|
+
let paymentElement: any = null;
|
|
102
109
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
stripeInstance = await loadStripe(options.publishableKey);
|
|
106
|
-
if (!stripeInstance) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
110
|
+
function mountPaymentElement(clientSecret: string): void {
|
|
111
|
+
paymentElement?.destroy();
|
|
109
112
|
// Payment method types are fixed server-side on the SetupIntent: Stripe
|
|
110
113
|
// rejects `paymentMethodTypes` next to a `clientSecret`, since it only
|
|
111
114
|
// applies to an Elements instance created in deferred-intent `mode`.
|
|
112
|
-
elementsInstance = stripeInstance.elements({
|
|
113
|
-
|
|
114
|
-
});
|
|
115
|
-
const paymentElement = elementsInstance.create("payment", {
|
|
115
|
+
elementsInstance = stripeInstance.elements({ clientSecret });
|
|
116
|
+
paymentElement = elementsInstance.create("payment", {
|
|
116
117
|
wallets: { applePay: "never", googlePay: "never" },
|
|
117
118
|
fields: {
|
|
118
119
|
billingDetails: { address: "auto", name: "auto", email: "never" },
|
|
@@ -121,6 +122,20 @@ export function useStripePaymentElement(
|
|
|
121
122
|
paymentElement.mount(`#${options.containerId}`);
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
async function init(): Promise<void> {
|
|
126
|
+
const { loadStripe } = await import("@stripe/stripe-js");
|
|
127
|
+
stripeInstance = await loadStripe(options.publishableKey);
|
|
128
|
+
if (!stripeInstance) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
mountPaymentElement(options.clientSecret);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function renew(clientSecret: string): Promise<void> {
|
|
135
|
+
await ready;
|
|
136
|
+
if (stripeInstance) mountPaymentElement(clientSecret);
|
|
137
|
+
}
|
|
138
|
+
|
|
124
139
|
async function confirmAndGetPaymentMethod(): Promise<ConfirmResult> {
|
|
125
140
|
if (!stripeInstance || !elementsInstance) {
|
|
126
141
|
return { error: { message: "Stripe not initialized" } };
|
|
@@ -139,11 +154,12 @@ export function useStripePaymentElement(
|
|
|
139
154
|
}
|
|
140
155
|
|
|
141
156
|
onUnmounted(() => {
|
|
157
|
+
paymentElement = null;
|
|
142
158
|
elementsInstance = null;
|
|
143
159
|
stripeInstance = null;
|
|
144
160
|
});
|
|
145
161
|
|
|
146
|
-
|
|
162
|
+
const ready = init();
|
|
147
163
|
|
|
148
|
-
return { confirmAndGetPaymentMethod };
|
|
164
|
+
return { confirmAndGetPaymentMethod, renew };
|
|
149
165
|
}
|
|
@@ -178,15 +178,20 @@ async function loadPendingRegistration(): Promise<void> {
|
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
+
async function createSetupIntent(): Promise<string | undefined> {
|
|
182
|
+
const setup = await publicFetch<SetupIntentResponse>(SETUP_INTENT_ENDPOINT)
|
|
183
|
+
return setup.clientSecret || undefined
|
|
184
|
+
}
|
|
185
|
+
|
|
181
186
|
async function mountPaymentElement(): Promise<void> {
|
|
182
187
|
const publishableKey = saasConfig?.stripePublishableKey
|
|
183
188
|
if (!publishableKey || paymentMethodPolicy === 'none') return
|
|
184
189
|
try {
|
|
185
|
-
const
|
|
186
|
-
if (!
|
|
190
|
+
const clientSecret = await createSetupIntent()
|
|
191
|
+
if (!clientSecret) return
|
|
187
192
|
stripeHandle.value = useStripePaymentElement({
|
|
188
193
|
publishableKey,
|
|
189
|
-
clientSecret
|
|
194
|
+
clientSecret,
|
|
190
195
|
containerId: PAYMENT_ELEMENT_ID,
|
|
191
196
|
billingEmail: () => pending.value?.email ?? '',
|
|
192
197
|
})
|
|
@@ -195,6 +200,18 @@ async function mountPaymentElement(): Promise<void> {
|
|
|
195
200
|
}
|
|
196
201
|
}
|
|
197
202
|
|
|
203
|
+
// A refusal after the card was confirmed leaves its SetupIntent spent: the
|
|
204
|
+
// next card must go through a fresh one, or Stripe refuses it with a bare
|
|
205
|
+
// processing error.
|
|
206
|
+
async function renewCard(): Promise<void> {
|
|
207
|
+
try {
|
|
208
|
+
const clientSecret = await createSetupIntent()
|
|
209
|
+
if (clientSecret) await stripeHandle.value?.renew(clientSecret)
|
|
210
|
+
} catch (error) {
|
|
211
|
+
cardError.value = resolveApiError(error, 'saas.public.register.error.load')
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
198
215
|
/** The confirmed card, undefined when none is collected, null on failure. */
|
|
199
216
|
async function confirmPaymentMethod(): Promise<string | undefined | null> {
|
|
200
217
|
if (!isPaymentStepVisible.value) return undefined
|
|
@@ -219,8 +236,9 @@ function validate(): boolean {
|
|
|
219
236
|
async function submit(): Promise<void> {
|
|
220
237
|
if (isSubmitting.value || !validate()) return
|
|
221
238
|
isSubmitting.value = true
|
|
239
|
+
let paymentMethodId: string | undefined | null
|
|
222
240
|
try {
|
|
223
|
-
|
|
241
|
+
paymentMethodId = await confirmPaymentMethod()
|
|
224
242
|
if (paymentMethodId === null) return
|
|
225
243
|
await $fetch(SESSION_ESTABLISH_ENDPOINT, {
|
|
226
244
|
method: 'POST',
|
|
@@ -241,6 +259,7 @@ async function submit(): Promise<void> {
|
|
|
241
259
|
error,
|
|
242
260
|
'saas.public.register.error.failed',
|
|
243
261
|
)
|
|
262
|
+
if (paymentMethodId) await renewCard()
|
|
244
263
|
} finally {
|
|
245
264
|
isSubmitting.value = false
|
|
246
265
|
}
|