@cosmicdrift/kumiko-bundled-features 0.397.0 → 0.398.0

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.
@@ -5,6 +5,7 @@ export type CheckoutMode = "subscription" | "payment";
5
5
  export declare const consentPayloadSchema: z.ZodObject<{
6
6
  earlyPerformanceRequested: z.ZodBoolean;
7
7
  withdrawalLossAcknowledged: z.ZodBoolean;
8
+ documentsAcknowledged: z.ZodOptional<z.ZodBoolean>;
8
9
  consentTextVersion: z.ZodString;
9
10
  locale: z.ZodString;
10
11
  }, z.core.$strict>;
@@ -15,6 +16,7 @@ export type PreparedConsent = {
15
16
  readonly locale: string;
16
17
  readonly submitMessage: string;
17
18
  readonly consentTextVersion: string;
19
+ readonly documentsAcknowledged: boolean;
18
20
  readonly termsHash: string;
19
21
  readonly termsTemplateVersion: number;
20
22
  };
@@ -8,13 +8,14 @@ import * as z from "zod";
8
8
  import { requireTemplateResolver, TEXT_BLOCK_KIND, TemplateNotFoundError, } from "../../template-resolver/index.js";
9
9
  import { paymentAggregateId, subscriptionAggregateId } from "../aggregate-id.js";
10
10
  import { CHECKOUT_CONSENT_RECORDED_EVENT_QN, checkoutConsentRecordedPayloadSchema, PAYMENT_AGGREGATE_TYPE, SUBSCRIPTION_AGGREGATE_TYPE, } from "../events.js";
11
- import { consentTextVersion, resolveConsentLocale, submitMessageFor } from "./consent-text.js";
11
+ import { consentTextVersion, documentsAcknowledgementTextFor, resolveConsentLocale, submitMessageFor, } from "./consent-text.js";
12
12
  // Optional at the schema level so a missing/unchecked consent reaches
13
13
  // prepareConsent and fails as 422 consent_required, not as a zod 400.
14
14
  export const consentPayloadSchema = z
15
15
  .object({
16
16
  earlyPerformanceRequested: z.boolean(),
17
17
  withdrawalLossAcknowledged: z.boolean(),
18
+ documentsAcknowledged: z.boolean().optional(),
18
19
  consentTextVersion: z.string().min(1).max(64),
19
20
  locale: z.string().min(2).max(35),
20
21
  })
@@ -39,14 +40,18 @@ export function assertValidOrderItem(item, priceId) {
39
40
  }
40
41
  }
41
42
  export async function prepareConsent(ctx, options, consent, mode) {
42
- if (!consent?.earlyPerformanceRequested || !consent.withdrawalLossAcknowledged) {
43
+ const documentsRequired = options.documentsAcknowledgement === true;
44
+ if (!consent?.earlyPerformanceRequested ||
45
+ !consent.withdrawalLossAcknowledged ||
46
+ (documentsRequired && consent.documentsAcknowledged !== true)) {
43
47
  throw new UnprocessableError("consent_required", {
44
48
  i18nKey: "billing-foundation.errors.consentRequired",
45
- message: "billing-foundation: both consent flags must be true to start a checkout",
49
+ message: "billing-foundation: all consent flags must be true to start a checkout",
46
50
  });
47
51
  }
48
52
  const locale = resolveConsentLocale(consent.locale);
49
- if (consent.consentTextVersion !== consentTextVersion(locale)) {
53
+ const documentsText = documentsAcknowledgementTextFor(options, locale);
54
+ if (consent.consentTextVersion !== consentTextVersion(locale, documentsText)) {
50
55
  throw new UnprocessableError("consent_text_outdated", {
51
56
  i18nKey: "billing-foundation.errors.consentTextOutdated",
52
57
  message: `billing-foundation: consent text version "${consent.consentTextVersion}" is not current for locale "${locale}"`,
@@ -78,6 +83,7 @@ export async function prepareConsent(ctx, options, consent, mode) {
78
83
  locale,
79
84
  submitMessage: submitMessageFor(locale, mode),
80
85
  consentTextVersion: consent.consentTextVersion,
86
+ documentsAcknowledged: documentsRequired,
81
87
  termsHash: createHash("sha256").update(terms.content).digest("hex"),
82
88
  termsTemplateVersion: terms.version,
83
89
  };
@@ -100,6 +106,7 @@ export async function recordConsent(ctx, prepared, details) {
100
106
  interval: details.price?.interval ?? null,
101
107
  intervalCount: details.price?.intervalCount ?? null,
102
108
  consentTextVersion: prepared.consentTextVersion,
109
+ ...(prepared.documentsAcknowledged && { documentsAcknowledged: true }),
103
110
  termsHash: prepared.termsHash,
104
111
  termsTemplateVersion: prepared.termsTemplateVersion,
105
112
  locale: prepared.locale,
@@ -1,3 +1,4 @@
1
+ import { type ConsumerProtectionOptions, type LegalLinkSet } from "../types.ts";
1
2
  import type { ConsentLocale } from "../web/consumer-protection/consent-locale.ts";
2
3
  export { CONSENT_LOCALES, type ConsentLocale, FALLBACK_CONSENT_LOCALE, resolveConsentLocale, } from "../web/consumer-protection/consent-locale.ts";
3
4
  export type ConsentTexts = {
@@ -7,5 +8,9 @@ export type ConsentTexts = {
7
8
  readonly paymentSubmitMessage: string;
8
9
  };
9
10
  export declare const CONSENT_TEXTS: Readonly<Record<ConsentLocale, ConsentTexts>>;
10
- export declare function consentTextVersion(locale: ConsentLocale): string;
11
+ export declare function legalLinksPerLocale(legalLinks: ConsumerProtectionOptions["legalLinks"]): Readonly<Record<ConsentLocale, LegalLinkSet>>;
12
+ /** The documents checkbox text for a locale, or undefined when the app did not
13
+ * opt in. The variant follows whether that locale has a refund link. */
14
+ export declare function documentsAcknowledgementTextFor(options: ConsumerProtectionOptions, locale: ConsentLocale): string | undefined;
15
+ export declare function consentTextVersion(locale: ConsentLocale, documentsAcknowledgementText?: string): string;
11
16
  export declare function submitMessageFor(locale: ConsentLocale, mode: "subscription" | "payment"): string;
@@ -1,4 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
+ import { isLegalLinkSet } from "../types.js";
2
3
  export { CONSENT_LOCALES, FALLBACK_CONSENT_LOCALE, resolveConsentLocale, } from "../web/consumer-protection/consent-locale.js";
3
4
  export const CONSENT_TEXTS = {
4
5
  de: {
@@ -14,13 +15,37 @@ export const CONSENT_TEXTS = {
14
15
  paymentSubmitMessage: "Paid one-off payment: by clicking the button you commit to paying.",
15
16
  },
16
17
  };
18
+ const DOCUMENTS_ACKNOWLEDGEMENT_TEXTS = {
19
+ de: {
20
+ withRefund: "Ich habe die AGB, die Erstattungsrichtlinie und die Datenschutzerklärung gelesen. Mit den AGB und der Erstattungsrichtlinie bin ich einverstanden.",
21
+ withoutRefund: "Ich habe die AGB und die Datenschutzerklärung gelesen. Mit den AGB bin ich einverstanden.",
22
+ },
23
+ en: {
24
+ withRefund: "I have read the Terms, the Refund Policy and the Privacy Policy, and I agree to the Terms and the Refund Policy.",
25
+ withoutRefund: "I have read the Terms and the Privacy Policy, and I agree to the Terms.",
26
+ },
27
+ };
28
+ export function legalLinksPerLocale(legalLinks) {
29
+ return isLegalLinkSet(legalLinks) ? { de: legalLinks, en: legalLinks } : legalLinks;
30
+ }
31
+ /** The documents checkbox text for a locale, or undefined when the app did not
32
+ * opt in. The variant follows whether that locale has a refund link. */
33
+ export function documentsAcknowledgementTextFor(options, locale) {
34
+ if (!options.documentsAcknowledgement)
35
+ return undefined;
36
+ const texts = DOCUMENTS_ACKNOWLEDGEMENT_TEXTS[locale];
37
+ return legalLinksPerLocale(options.legalLinks)[locale].refund !== undefined
38
+ ? texts.withRefund
39
+ : texts.withoutRefund;
40
+ }
17
41
  const CONSENT_TEXT_VERSION_LENGTH = 16;
18
- export function consentTextVersion(locale) {
42
+ // The documents text joins the hash only when shown, so apps without the opt-in keep their version.
43
+ export function consentTextVersion(locale, documentsAcknowledgementText) {
19
44
  const { earlyPerformance, withdrawalLoss } = CONSENT_TEXTS[locale];
20
- return createHash("sha256")
21
- .update(`${earlyPerformance}\n${withdrawalLoss}`)
22
- .digest("hex")
23
- .slice(0, CONSENT_TEXT_VERSION_LENGTH);
45
+ const hash = createHash("sha256").update(`${earlyPerformance}\n${withdrawalLoss}`);
46
+ if (documentsAcknowledgementText !== undefined)
47
+ hash.update(`\n${documentsAcknowledgementText}`);
48
+ return hash.digest("hex").slice(0, CONSENT_TEXT_VERSION_LENGTH);
24
49
  }
25
50
  export function submitMessageFor(locale, mode) {
26
51
  const texts = CONSENT_TEXTS[locale];
@@ -63,6 +63,7 @@ export declare const checkoutConsentRecordedPayloadSchema: z.ZodObject<{
63
63
  interval: z.ZodNullable<z.ZodString>;
64
64
  intervalCount: z.ZodNullable<z.ZodNumber>;
65
65
  consentTextVersion: z.ZodString;
66
+ documentsAcknowledged: z.ZodOptional<z.ZodBoolean>;
66
67
  termsHash: z.ZodString;
67
68
  termsTemplateVersion: z.ZodNumber;
68
69
  locale: z.ZodString;
@@ -102,6 +102,7 @@ export const checkoutConsentRecordedPayloadSchema = z.object({
102
102
  interval: z.string().nullable(),
103
103
  intervalCount: z.number().nullable(),
104
104
  consentTextVersion: z.string().min(1).max(64),
105
+ documentsAcknowledged: z.boolean().optional(),
105
106
  termsHash: z.string().length(64),
106
107
  termsTemplateVersion: z.number(),
107
108
  locale: z.string().min(2).max(35),
@@ -1,3 +1,3 @@
1
1
  import type { QueryHandlerDef } from "@cosmicdrift/kumiko-framework/engine";
2
- import { type BillingPlanCatalog, type ResolvedBillingFoundationOptions } from "../types.ts";
2
+ import type { BillingPlanCatalog, ResolvedBillingFoundationOptions } from "../types.ts";
3
3
  export declare function createBillingPlansQuery(options: ResolvedBillingFoundationOptions, catalog: BillingPlanCatalog): QueryHandlerDef;
@@ -3,9 +3,8 @@
3
3
  // when `createBillingFoundationFeature` gets a `catalog`.
4
4
  import * as z from "zod";
5
5
  import { filterOfferedProviders, listCatalogProviders, } from "../checkout-core.js";
6
- import { CONSENT_TEXTS, consentTextVersion, } from "../consumer-protection/consent-text.js";
6
+ import { CONSENT_TEXTS, consentTextVersion, documentsAcknowledgementTextFor, legalLinksPerLocale, } from "../consumer-protection/consent-text.js";
7
7
  import { buildProviderPlanViews, loadBillingPlansBase, toBillingPlansResult, } from "../plan-catalog.js";
8
- import { isLegalLinkSet, } from "../types.js";
9
8
  const billingPlansSchema = z.object({}).strict();
10
9
  export function createBillingPlansQuery(options, catalog) {
11
10
  return {
@@ -23,8 +22,8 @@ export function createBillingPlansQuery(options, catalog) {
23
22
  ...result,
24
23
  consumerProtection: {
25
24
  consentTexts: {
26
- de: consentTextView("de"),
27
- en: consentTextView("en"),
25
+ de: consentTextView(consumerProtection, "de"),
26
+ en: consentTextView(consumerProtection, "en"),
28
27
  },
29
28
  legalLinks: legalLinksPerLocale(consumerProtection.legalLinks),
30
29
  },
@@ -62,10 +61,13 @@ async function providerView(ctx, catalog, base, provider, providerTexts, known)
62
61
  plans: views.plans,
63
62
  };
64
63
  }
65
- function legalLinksPerLocale(legalLinks) {
66
- return isLegalLinkSet(legalLinks) ? { de: legalLinks, en: legalLinks } : legalLinks;
67
- }
68
- function consentTextView(locale) {
64
+ function consentTextView(consumerProtection, locale) {
69
65
  const { earlyPerformance, withdrawalLoss } = CONSENT_TEXTS[locale];
70
- return { earlyPerformance, withdrawalLoss, consentTextVersion: consentTextVersion(locale) };
66
+ const documentsAcknowledgement = documentsAcknowledgementTextFor(consumerProtection, locale);
67
+ return {
68
+ earlyPerformance,
69
+ withdrawalLoss,
70
+ ...(documentsAcknowledgement !== undefined && { documentsAcknowledgement }),
71
+ consentTextVersion: consentTextVersion(locale, documentsAcknowledgement),
72
+ };
71
73
  }
@@ -78,6 +78,7 @@ export const BILLING_FOUNDATION_I18N = {
78
78
  "billing-foundation.consent.link.terms": { en: "Terms" },
79
79
  "billing-foundation.consent.link.withdrawal": { en: "Withdrawal policy" },
80
80
  "billing-foundation.consent.link.privacy": { en: "Privacy policy" },
81
+ "billing-foundation.consent.link.refund": { en: "Refund policy" },
81
82
  "billing-foundation.consent.order": { en: "Order with obligation to pay" },
82
83
  "billing-foundation.plans.priceFrom": { en: "from" },
83
84
  "billing-foundation.consent.chooseProvider": { en: "Payment provider" },
@@ -345,6 +345,8 @@ export type LegalLinkSet = {
345
345
  readonly terms: string;
346
346
  readonly withdrawal: string;
347
347
  readonly privacy: string;
348
+ /** Link to the refund policy; shown next to the other links when set. */
349
+ readonly refund?: string;
348
350
  };
349
351
  export declare function isLegalLinkSet(links: LegalLinkSet | Readonly<Record<ConsentLocale, LegalLinkSet>>): links is LegalLinkSet;
350
352
  /** Consumer-protection consent gate (§ 312j / § 356 BGB style). When set,
@@ -382,6 +384,9 @@ export type ConsumerProtectionOptions = {
382
384
  /** Root-relative paths or absolute https URLs. One set for every locale, or
383
385
  * one set per consent locale (`de` and `en` required). */
384
386
  readonly legalLinks: LegalLinkSet | Readonly<Record<ConsentLocale, LegalLinkSet>>;
387
+ /** Adds a required first checkbox "documents read and accepted" (terms, privacy,
388
+ * and the refund policy when `legalLinks.refund` is set). Off by default. */
389
+ readonly documentsAcknowledgement?: boolean;
385
390
  };
386
391
  export type OneOffCheckoutRequest = {
387
392
  readonly tenantId: TenantId;
@@ -476,6 +481,8 @@ export type BillingPlansResult = {
476
481
  readonly consentTexts: Readonly<Record<"de" | "en", {
477
482
  readonly earlyPerformance: string;
478
483
  readonly withdrawalLoss: string;
484
+ /** Present only when `documentsAcknowledgement` is on. */
485
+ readonly documentsAcknowledgement?: string;
479
486
  readonly consentTextVersion: string;
480
487
  }>>;
481
488
  readonly legalLinks: Readonly<Record<ConsentLocale, LegalLinkSet>>;
@@ -73,7 +73,10 @@ function validatePerLocaleLegalLinks(legalLinks) {
73
73
  }
74
74
  function validateLegalLinkSet(set, path) {
75
75
  for (const [key, link] of Object.entries(set)) {
76
- if (!isRootRelativeOrHttpsUrl(link)) {
76
+ // skip: refund is optional
77
+ if (key === "refund" && link === undefined)
78
+ continue;
79
+ if (typeof link !== "string" || !isRootRelativeOrHttpsUrl(link)) {
77
80
  throw new Error(`createBillingFoundationFeature: consumerProtection.${path}.${key} "${link}" must be a root-relative path ("/..." not "//") or an absolute https URL.`);
78
81
  }
79
82
  }
@@ -6,6 +6,7 @@ type ConsumerProtectionView = NonNullable<BillingPlansResult["consumerProtection
6
6
  export type CheckoutConsentPayload = {
7
7
  readonly earlyPerformanceRequested: true;
8
8
  readonly withdrawalLossAcknowledged: true;
9
+ readonly documentsAcknowledged?: true;
9
10
  readonly consentTextVersion: string;
10
11
  readonly locale: string;
11
12
  };
@@ -2,41 +2,33 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
2
2
  import { useLocale, usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
3
3
  import { ChoiceCards } from "@cosmicdrift/kumiko-renderer/web";
4
4
  import { useState } from "react";
5
+ import { ConsentCheckboxes, ConsentLinks, useConsentFieldState } from "./consent-fields.js";
5
6
  import { resolveConsentLocale } from "./consumer-protection/consent-locale.js";
6
7
  export const CONSENT_TEXT_OUTDATED_CODE = "consent_text_outdated";
7
8
  // kumiko-lint-ignore no-custom-primitives Uses usePrimitives().Modal internally, name is a billing domain action, not a primitive reimplementation
8
9
  export function CheckoutConsentDialog({ open, onOpenChange, planName, price, consumerProtection, providerChoice, note, onOrder, onConsentTextOutdated, }) {
9
10
  const t = useTranslation();
10
11
  const uiLocale = useLocale().locale();
11
- const { Modal, Text, Field, Input, Button, Link, Banner } = usePrimitives();
12
- const [earlyPerformance, setEarlyPerformance] = useState(false);
13
- const [withdrawalLoss, setWithdrawalLoss] = useState(false);
12
+ const { Modal, Text, Button, Banner } = usePrimitives();
14
13
  const [submitting, setSubmitting] = useState(false);
15
14
  const [errorText, setErrorText] = useState(null);
16
15
  const consentLocale = resolveConsentLocale(uiLocale);
17
16
  const texts = consumerProtection?.consentTexts[consentLocale];
18
17
  const legalLinks = consumerProtection?.legalLinks[consentLocale];
19
- const consentGiven = consumerProtection === undefined || (earlyPerformance && withdrawalLoss);
18
+ const consentFields = useConsentFieldState(texts);
19
+ const consentGiven = consumerProtection === undefined || consentFields.allGiven;
20
20
  async function order() {
21
21
  setSubmitting(true);
22
22
  setErrorText(null);
23
- const result = await onOrder(texts === undefined
24
- ? undefined
25
- : {
26
- earlyPerformanceRequested: true,
27
- withdrawalLossAcknowledged: true,
28
- consentTextVersion: texts.consentTextVersion,
29
- locale: consentLocale,
30
- });
23
+ const result = await onOrder(consentFields.buildPayload(consentLocale));
31
24
  if (result.isSuccess)
32
25
  return;
33
26
  setSubmitting(false);
34
27
  setErrorText(t(result.error.i18nKey, result.error.i18nParams));
35
28
  if (result.error.code === CONSENT_TEXT_OUTDATED_CODE) {
36
- setEarlyPerformance(false);
37
- setWithdrawalLoss(false);
29
+ consentFields.reset();
38
30
  onConsentTextOutdated();
39
31
  }
40
32
  }
41
- return (_jsx(Modal, { open: open, onOpenChange: onOpenChange, title: t("billing-foundation.consent.title"), testId: "checkout-consent-dialog", children: _jsxs("div", { className: "flex flex-col gap-4", children: [providerChoice !== undefined && (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Text, { variant: "muted", children: t("billing-foundation.consent.chooseProvider") }), _jsx(ChoiceCards, { value: providerChoice.value, options: providerChoice.options, onChange: providerChoice.onChange, ariaLabel: t("billing-foundation.consent.chooseProvider"), testId: "checkout-provider-choice" })] })), _jsxs("div", { className: "flex flex-col gap-1", "data-testid": "checkout-consent-summary", children: [_jsxs(Text, { children: [planName, " \u2014 ", price.amount] }), price.renewalKey !== undefined && (_jsxs(_Fragment, { children: [_jsx(Text, { variant: "muted", children: t(price.renewalKey, price.count !== undefined ? { count: price.count } : undefined) }), _jsx(Text, { variant: "muted", children: t("billing-foundation.consent.cancelAnytime") })] })), note !== undefined && (_jsx(Text, { variant: "muted", testId: "checkout-consent-note", children: note }))] }), legalLinks !== undefined && (_jsxs("div", { className: "flex flex-wrap gap-4", children: [_jsx(Link, { href: legalLinks.terms, target: "_blank", children: t("billing-foundation.consent.link.terms") }), _jsx(Link, { href: legalLinks.withdrawal, target: "_blank", children: t("billing-foundation.consent.link.withdrawal") }), _jsx(Link, { href: legalLinks.privacy, target: "_blank", children: t("billing-foundation.consent.link.privacy") })] })), texts !== undefined && (_jsxs(_Fragment, { children: [_jsx(Field, { id: "consent-early-performance", label: texts.earlyPerformance, layout: "inline", children: _jsx(Input, { kind: "boolean", id: "consent-early-performance", name: "consent-early-performance", value: earlyPerformance, onChange: setEarlyPerformance, disabled: submitting }) }), _jsx(Field, { id: "consent-withdrawal-loss", label: texts.withdrawalLoss, layout: "inline", children: _jsx(Input, { kind: "boolean", id: "consent-withdrawal-loss", name: "consent-withdrawal-loss", value: withdrawalLoss, onChange: setWithdrawalLoss, disabled: submitting }) })] })), errorText !== null && (_jsx(Banner, { variant: "error", testId: "checkout-consent-error", children: errorText })), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { variant: "secondary", disabled: submitting, onClick: () => onOpenChange(false), testId: "checkout-consent-cancel", children: t("billing-foundation.consent.back") }), _jsx(Button, { disabled: !consentGiven || submitting, loading: submitting, onClick: order, testId: "checkout-consent-order", children: t("billing-foundation.consent.order") })] })] }) }));
33
+ return (_jsx(Modal, { open: open, onOpenChange: onOpenChange, title: t("billing-foundation.consent.title"), testId: "checkout-consent-dialog", children: _jsxs("div", { className: "flex flex-col gap-4", children: [providerChoice !== undefined && (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Text, { variant: "muted", children: t("billing-foundation.consent.chooseProvider") }), _jsx(ChoiceCards, { value: providerChoice.value, options: providerChoice.options, onChange: providerChoice.onChange, ariaLabel: t("billing-foundation.consent.chooseProvider"), testId: "checkout-provider-choice" })] })), _jsxs("div", { className: "flex flex-col gap-1", "data-testid": "checkout-consent-summary", children: [_jsxs(Text, { children: [planName, " \u2014 ", price.amount] }), price.renewalKey !== undefined && (_jsxs(_Fragment, { children: [_jsx(Text, { variant: "muted", children: t(price.renewalKey, price.count !== undefined ? { count: price.count } : undefined) }), _jsx(Text, { variant: "muted", children: t("billing-foundation.consent.cancelAnytime") })] })), note !== undefined && (_jsx(Text, { variant: "muted", testId: "checkout-consent-note", children: note }))] }), legalLinks !== undefined && _jsx(ConsentLinks, { legalLinks: legalLinks }), texts !== undefined && (_jsx(ConsentCheckboxes, { idPrefix: "consent", texts: texts, state: consentFields, disabled: submitting })), errorText !== null && (_jsx(Banner, { variant: "error", testId: "checkout-consent-error", children: errorText })), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { variant: "secondary", disabled: submitting, onClick: () => onOpenChange(false), testId: "checkout-consent-cancel", children: t("billing-foundation.consent.back") }), _jsx(Button, { disabled: !consentGiven || submitting, loading: submitting, onClick: order, testId: "checkout-consent-order", children: t("billing-foundation.consent.order") })] })] }) }));
42
34
  }
@@ -0,0 +1,28 @@
1
+ import { type ReactNode } from "react";
2
+ import type { BillingPlansResult, LegalLinkSet } from "../types.ts";
3
+ import type { CheckoutConsentPayload } from "./checkout-consent-dialog.tsx";
4
+ type ConsumerProtectionView = NonNullable<BillingPlansResult["consumerProtection"]>;
5
+ export type ConsentTextView = ConsumerProtectionView["consentTexts"]["de"];
6
+ export type ConsentChecks = {
7
+ readonly documentsAcknowledged: boolean;
8
+ readonly earlyPerformance: boolean;
9
+ readonly withdrawalLoss: boolean;
10
+ };
11
+ export type ConsentFieldState = {
12
+ readonly checks: ConsentChecks;
13
+ readonly setCheck: (key: keyof ConsentChecks, value: boolean) => void;
14
+ readonly reset: () => void;
15
+ readonly allGiven: boolean;
16
+ readonly buildPayload: (locale: string) => CheckoutConsentPayload | undefined;
17
+ };
18
+ export declare function useConsentFieldState(texts: ConsentTextView | undefined): ConsentFieldState;
19
+ export declare function ConsentLinks({ legalLinks }: {
20
+ readonly legalLinks: LegalLinkSet;
21
+ }): ReactNode;
22
+ export declare function ConsentCheckboxes({ idPrefix, texts, state, disabled, }: {
23
+ readonly idPrefix: string;
24
+ readonly texts: ConsentTextView;
25
+ readonly state: ConsentFieldState;
26
+ readonly disabled: boolean;
27
+ }): ReactNode;
28
+ export {};
@@ -0,0 +1,43 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
3
+ import { useState } from "react";
4
+ const NOTHING_CHECKED = {
5
+ documentsAcknowledged: false,
6
+ earlyPerformance: false,
7
+ withdrawalLoss: false,
8
+ };
9
+ export function useConsentFieldState(texts) {
10
+ const [checks, setChecks] = useState(NOTHING_CHECKED);
11
+ const documentsRequired = texts?.documentsAcknowledgement !== undefined;
12
+ return {
13
+ checks,
14
+ setCheck: (key, value) => setChecks((current) => ({ ...current, [key]: value })),
15
+ reset: () => setChecks(NOTHING_CHECKED),
16
+ allGiven: checks.earlyPerformance &&
17
+ checks.withdrawalLoss &&
18
+ (!documentsRequired || checks.documentsAcknowledged),
19
+ buildPayload: (locale) => texts === undefined
20
+ ? undefined
21
+ : {
22
+ earlyPerformanceRequested: true,
23
+ withdrawalLossAcknowledged: true,
24
+ ...(documentsRequired && { documentsAcknowledged: true }),
25
+ consentTextVersion: texts.consentTextVersion,
26
+ locale,
27
+ },
28
+ };
29
+ }
30
+ export function ConsentLinks({ legalLinks }) {
31
+ const t = useTranslation();
32
+ const { Link } = usePrimitives();
33
+ return (_jsxs("div", { className: "flex flex-wrap gap-4", children: [_jsx(Link, { href: legalLinks.terms, target: "_blank", children: t("billing-foundation.consent.link.terms") }), _jsx(Link, { href: legalLinks.withdrawal, target: "_blank", children: t("billing-foundation.consent.link.withdrawal") }), _jsx(Link, { href: legalLinks.privacy, target: "_blank", children: t("billing-foundation.consent.link.privacy") }), legalLinks.refund !== undefined && (_jsx(Link, { href: legalLinks.refund, target: "_blank", children: t("billing-foundation.consent.link.refund") }))] }));
34
+ }
35
+ export function ConsentCheckboxes({ idPrefix, texts, state, disabled, }) {
36
+ const { Field, Input } = usePrimitives();
37
+ const boxes = [
38
+ { key: "documentsAcknowledged", slug: "documents", label: texts.documentsAcknowledgement },
39
+ { key: "earlyPerformance", slug: "early-performance", label: texts.earlyPerformance },
40
+ { key: "withdrawalLoss", slug: "withdrawal-loss", label: texts.withdrawalLoss },
41
+ ];
42
+ return (_jsx(_Fragment, { children: boxes.map(({ key, slug, label }) => label === undefined ? null : (_jsx(Field, { id: `${idPrefix}-${slug}`, label: label, layout: "inline", children: _jsx(Input, { kind: "boolean", id: `${idPrefix}-${slug}`, name: `${idPrefix}-${slug}`, value: state.checks[key], onChange: (value) => state.setCheck(key, value), disabled: disabled }) }, key))) }));
43
+ }
@@ -1,8 +1,9 @@
1
- import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useLocale, useMutation, usePrimitives, useQuery, useTranslation, } from "@cosmicdrift/kumiko-renderer";
3
3
  import { formatMoney } from "@cosmicdrift/kumiko-renderer/web";
4
4
  import { useState } from "react";
5
- import { CONSENT_TEXT_OUTDATED_CODE, } from "./checkout-consent-dialog.js";
5
+ import { CONSENT_TEXT_OUTDATED_CODE } from "./checkout-consent-dialog.js";
6
+ import { ConsentCheckboxes, ConsentLinks, useConsentFieldState } from "./consent-fields.js";
6
7
  import { SubscriptionFoundationHandlers, SubscriptionFoundationQueries } from "./constants.js";
7
8
  import { resolveConsentLocale } from "./consumer-protection/consent-locale.js";
8
9
  import { intervalKey } from "./plan-price.js";
@@ -11,28 +12,20 @@ import { recurringAmountsOf } from "./plan-switch-recurring.js";
11
12
  export function PlanSwitchConfirmDialog({ open, onOpenChange, tier, fromPlanName, toPlanName, consumerProtection, onApplied, onConsentTextOutdated, }) {
12
13
  const t = useTranslation();
13
14
  const locale = useLocale().locale();
14
- const { Modal, Text, Field, Input, Button, Link, Banner } = usePrimitives();
15
+ const { Modal, Text, Button, Banner } = usePrimitives();
15
16
  const previewQuery = useQuery(SubscriptionFoundationQueries.planSwitchPreview, { tier });
16
17
  const switchMutation = useMutation(SubscriptionFoundationHandlers.switchPlan);
17
- const [earlyPerformance, setEarlyPerformance] = useState(false);
18
- const [withdrawalLoss, setWithdrawalLoss] = useState(false);
19
18
  const [submitting, setSubmitting] = useState(false);
20
19
  const [errorText, setErrorText] = useState(null);
21
20
  const consentLocale = resolveConsentLocale(locale);
22
21
  const texts = consumerProtection?.consentTexts[consentLocale];
23
22
  const legalLinks = consumerProtection?.legalLinks[consentLocale];
24
- const consentGiven = consumerProtection === undefined || (earlyPerformance && withdrawalLoss);
23
+ const consentFields = useConsentFieldState(texts);
24
+ const consentGiven = consumerProtection === undefined || consentFields.allGiven;
25
25
  async function confirm() {
26
26
  setSubmitting(true);
27
27
  setErrorText(null);
28
- const consent = texts === undefined
29
- ? undefined
30
- : {
31
- earlyPerformanceRequested: true,
32
- withdrawalLossAcknowledged: true,
33
- consentTextVersion: texts.consentTextVersion,
34
- locale: consentLocale,
35
- };
28
+ const consent = consentFields.buildPayload(consentLocale);
36
29
  const result = await switchMutation.mutate({ tier, ...(consent && { consent }) });
37
30
  if (result.isSuccess) {
38
31
  onApplied();
@@ -41,8 +34,7 @@ export function PlanSwitchConfirmDialog({ open, onOpenChange, tier, fromPlanName
41
34
  setSubmitting(false);
42
35
  setErrorText(t(result.error.i18nKey, result.error.i18nParams));
43
36
  if (result.error.code === CONSENT_TEXT_OUTDATED_CODE) {
44
- setEarlyPerformance(false);
45
- setWithdrawalLoss(false);
37
+ consentFields.reset();
46
38
  onConsentTextOutdated();
47
39
  }
48
40
  }
@@ -66,5 +58,5 @@ export function PlanSwitchConfirmDialog({ open, onOpenChange, tier, fromPlanName
66
58
  amount: money(reply.preview.credit, reply.preview.currency),
67
59
  }) }), _jsx(Text, { variant: "muted", children: t("billing-foundation.planSwitch.recurring", {
68
60
  amount: recurringPrice,
69
- }) }), _jsx(Text, { variant: "muted", children: t("billing-foundation.planSwitch.effective") }), _jsx(Text, { variant: "muted", children: t("billing-foundation.planSwitch.pendingNote") })] })), legalLinks !== undefined && (_jsxs("div", { className: "flex flex-wrap gap-4", children: [_jsx(Link, { href: legalLinks.terms, target: "_blank", children: t("billing-foundation.consent.link.terms") }), _jsx(Link, { href: legalLinks.withdrawal, target: "_blank", children: t("billing-foundation.consent.link.withdrawal") }), _jsx(Link, { href: legalLinks.privacy, target: "_blank", children: t("billing-foundation.consent.link.privacy") })] })), texts !== undefined && (_jsxs(_Fragment, { children: [_jsx(Field, { id: "plan-switch-early-performance", label: texts.earlyPerformance, layout: "inline", children: _jsx(Input, { kind: "boolean", id: "plan-switch-early-performance", name: "plan-switch-early-performance", value: earlyPerformance, onChange: setEarlyPerformance, disabled: submitting }) }), _jsx(Field, { id: "plan-switch-withdrawal-loss", label: texts.withdrawalLoss, layout: "inline", children: _jsx(Input, { kind: "boolean", id: "plan-switch-withdrawal-loss", name: "plan-switch-withdrawal-loss", value: withdrawalLoss, onChange: setWithdrawalLoss, disabled: submitting }) })] })), errorText !== null && (_jsx(Banner, { variant: "error", testId: "plan-switch-confirm-error", children: errorText })), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { variant: "secondary", disabled: submitting, onClick: () => onOpenChange(false), testId: "plan-switch-confirm-cancel", children: t("billing-foundation.consent.back") }), _jsx(Button, { disabled: !consentGiven || reply === null || submitting, loading: submitting, onClick: confirm, testId: "plan-switch-confirm-order", children: t("billing-foundation.planSwitch.order") })] })] }) }));
61
+ }) }), _jsx(Text, { variant: "muted", children: t("billing-foundation.planSwitch.effective") }), _jsx(Text, { variant: "muted", children: t("billing-foundation.planSwitch.pendingNote") })] })), legalLinks !== undefined && _jsx(ConsentLinks, { legalLinks: legalLinks }), texts !== undefined && (_jsx(ConsentCheckboxes, { idPrefix: "plan-switch", texts: texts, state: consentFields, disabled: submitting })), errorText !== null && (_jsx(Banner, { variant: "error", testId: "plan-switch-confirm-error", children: errorText })), _jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { variant: "secondary", disabled: submitting, onClick: () => onOpenChange(false), testId: "plan-switch-confirm-cancel", children: t("billing-foundation.consent.back") }), _jsx(Button, { disabled: !consentGiven || reply === null || submitting, loading: submitting, onClick: confirm, testId: "plan-switch-confirm-order", children: t("billing-foundation.planSwitch.order") })] })] }) }));
70
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-bundled-features",
3
- "version": "0.397.0",
3
+ "version": "0.398.0",
4
4
  "description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
5
5
  "license": "MIT",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -566,9 +566,9 @@
566
566
  }
567
567
  },
568
568
  "dependencies": {
569
- "@cosmicdrift/kumiko-framework": "0.397.0",
570
- "@cosmicdrift/kumiko-renderer": "0.397.0",
571
- "@cosmicdrift/kumiko-types": "0.397.0",
569
+ "@cosmicdrift/kumiko-framework": "0.398.0",
570
+ "@cosmicdrift/kumiko-renderer": "0.398.0",
571
+ "@cosmicdrift/kumiko-types": "0.398.0",
572
572
  "@mollie/api-client": "^4.5.0",
573
573
  "@node-rs/argon2": "^2.0.2",
574
574
  "@tiptap/core": "^3.31.3",
@@ -584,10 +584,10 @@
584
584
  "@types/qrcode": "^1.5.5",
585
585
  "clsx": "^2.1.1",
586
586
  "happy-dom": "^21.0.4",
587
- "hono": "^4.13.1",
587
+ "hono": "^4.13.14",
588
588
  "imapflow": "^1.7.8",
589
589
  "ioredis": "^5.10.1",
590
- "jose": "^6.2.3",
590
+ "jose": "^6.2.13",
591
591
  "lucide-react": "^1.14.0",
592
592
  "mailparser": "^3.12.4",
593
593
  "marked": "^18.0.3",
@@ -1157,18 +1157,18 @@
1157
1157
  "LICENSE"
1158
1158
  ],
1159
1159
  "devDependencies": {
1160
- "@testing-library/user-event": "^14.6.1",
1161
- "@cosmicdrift/kumiko-locale-de": "0.397.0",
1162
- "@cosmicdrift/kumiko-locale-es": "0.397.0",
1163
- "@cosmicdrift/kumiko-locale-fr": "0.397.0",
1164
- "@cosmicdrift/kumiko-locale-it": "0.397.0",
1165
- "@cosmicdrift/kumiko-renderer-mail-html": "0.397.0",
1160
+ "@testing-library/user-event": "^14.6.8",
1161
+ "@cosmicdrift/kumiko-locale-de": "0.398.0",
1162
+ "@cosmicdrift/kumiko-locale-es": "0.398.0",
1163
+ "@cosmicdrift/kumiko-locale-fr": "0.398.0",
1164
+ "@cosmicdrift/kumiko-locale-it": "0.398.0",
1165
+ "@cosmicdrift/kumiko-renderer-mail-html": "0.398.0",
1166
1166
  "jsqr": "^1.4.0",
1167
1167
  "unpdf": "^1.8.1",
1168
- "@cosmicdrift/kumiko-testing": "0.397.0"
1168
+ "@cosmicdrift/kumiko-testing": "0.398.0"
1169
1169
  },
1170
1170
  "peerDependencies": {
1171
- "@cosmicdrift/kumiko-testing": "0.397.0"
1171
+ "@cosmicdrift/kumiko-testing": "0.398.0"
1172
1172
  },
1173
1173
  "peerDependenciesMeta": {
1174
1174
  "@cosmicdrift/kumiko-testing": {
@@ -1,4 +1,10 @@
1
1
  [
2
+ {
3
+ "version": "0.398.0",
4
+ "type": "improvement",
5
+ "title": "Consent box takes a refund policy link and an optional documents checkbox",
6
+ "detail": "`legalLinks.refund` is rendered next to the other links in the checkout and plan-switch dialogs. `consumerProtection.documentsAcknowledgement: true` adds a required first checkbox whose text depends on whether a refund link is set for the locale. The text joins `consentTextVersion` only when the option is on, `start-plan-checkout`, `create-checkout-session` and `switch-plan` then reject a consent without `documentsAcknowledged: true` with 422 `consent_required`, and the recorded consent stores the flag. Without the option nothing changes, including the version. New i18n key `billing-foundation.consent.link.refund`."
7
+ },
2
8
  {
3
9
  "version": "0.394.0",
4
10
  "type": "improvement",