@frigear-nu/subscriptions 0.0.0-stage → 0.0.2
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/README.md +127 -2
- package/dist/module.d.mts +155 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +120 -0
- package/dist/runtime/app/components/SubscriptionPlans.d.vue.ts +71 -0
- package/dist/runtime/app/components/SubscriptionPlans.vue +215 -0
- package/dist/runtime/app/components/SubscriptionPlans.vue.d.ts +71 -0
- package/dist/runtime/app/components/SubscriptionStatus.d.vue.ts +44 -0
- package/dist/runtime/app/components/SubscriptionStatus.vue +138 -0
- package/dist/runtime/app/components/SubscriptionStatus.vue.d.ts +44 -0
- package/dist/runtime/app/composables/index.d.ts +4 -0
- package/dist/runtime/app/composables/index.js +2 -0
- package/dist/runtime/app/composables/usePlans.d.ts +23 -0
- package/dist/runtime/app/composables/usePlans.js +10 -0
- package/dist/runtime/app/composables/useSubscription.d.ts +27 -0
- package/dist/runtime/app/composables/useSubscription.js +73 -0
- package/dist/runtime/app/middleware/subscription.d.ts +2 -0
- package/dist/runtime/app/middleware/subscription.js +14 -0
- package/dist/runtime/app/pages/cancel.d.vue.ts +3 -0
- package/dist/runtime/app/pages/cancel.vue +23 -0
- package/dist/runtime/app/pages/cancel.vue.d.ts +3 -0
- package/dist/runtime/app/pages/membership.d.vue.ts +3 -0
- package/dist/runtime/app/pages/membership.vue +18 -0
- package/dist/runtime/app/pages/membership.vue.d.ts +3 -0
- package/dist/runtime/app/pages/success.d.vue.ts +3 -0
- package/dist/runtime/app/pages/success.vue +33 -0
- package/dist/runtime/app/pages/success.vue.d.ts +3 -0
- package/dist/runtime/i18n/lang/da.json +55 -0
- package/dist/runtime/i18n/lang/en.json +55 -0
- package/dist/runtime/menus.d.ts +2 -0
- package/dist/runtime/menus.js +16 -0
- package/dist/runtime/server/db/schema.sqlite.d.ts +231 -0
- package/dist/runtime/server/db/schema.sqlite.js +20 -0
- package/dist/runtime/server/routes/subscriptions/checkout.post.d.ts +2 -0
- package/dist/runtime/server/routes/subscriptions/checkout.post.js +125 -0
- package/dist/runtime/server/routes/subscriptions/me.get.d.ts +2 -0
- package/dist/runtime/server/routes/subscriptions/me.get.js +22 -0
- package/dist/runtime/server/routes/subscriptions/plans.get.d.ts +2 -0
- package/dist/runtime/server/routes/subscriptions/plans.get.js +3 -0
- package/dist/runtime/server/routes/subscriptions/portal.post.d.ts +2 -0
- package/dist/runtime/server/routes/subscriptions/portal.post.js +17 -0
- package/dist/runtime/server/stripe/subscription-events.d.ts +2 -0
- package/dist/runtime/server/stripe/subscription-events.js +36 -0
- package/dist/runtime/server/utils/plans.d.ts +6 -0
- package/dist/runtime/server/utils/plans.js +64 -0
- package/dist/runtime/server/utils/subscriptions.d.ts +13 -0
- package/dist/runtime/server/utils/subscriptions.js +16 -0
- package/dist/runtime/utils/index.d.ts +4 -0
- package/dist/runtime/utils/index.js +2 -0
- package/dist/runtime/utils/plans.d.ts +32 -0
- package/dist/runtime/utils/plans.js +33 -0
- package/dist/types.d.mts +3 -0
- package/package.json +88 -3
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { usePlans } from "../composables/usePlans";
|
|
3
|
+
import { useSubscription } from "../composables/useSubscription";
|
|
4
|
+
defineOptions({ name: "SubscriptionPlans" });
|
|
5
|
+
const props = defineProps({
|
|
6
|
+
locale: { type: String, required: false },
|
|
7
|
+
currency: { type: String, required: false },
|
|
8
|
+
interval: { type: String, required: false },
|
|
9
|
+
plans: { type: Array, required: false },
|
|
10
|
+
confirmChange: { type: Boolean, required: false, default: false }
|
|
11
|
+
});
|
|
12
|
+
const emit = defineEmits(["select"]);
|
|
13
|
+
const t = useModuleT("subscriptions.plans");
|
|
14
|
+
const tp = useModuleT("subscriptions.catalog");
|
|
15
|
+
const ti = useModuleT("subscriptions.interval");
|
|
16
|
+
const { plans: allPlans, status } = usePlans();
|
|
17
|
+
const { plan: currentPlan, isActive, pending, subscribe, error } = useSubscription();
|
|
18
|
+
const visible = computed(() => allPlans.value.filter((p) => !props.plans || props.plans.includes(p.key)));
|
|
19
|
+
const currentRank = computed(() => allPlans.value.find((p) => p.key === currentPlan.value)?.rank ?? null);
|
|
20
|
+
function priceFor(plan) {
|
|
21
|
+
return plan.prices.find((p) => p.interval === props.interval) ?? plan.prices[0];
|
|
22
|
+
}
|
|
23
|
+
function formatPrice(price) {
|
|
24
|
+
if (price.amount === null) return "";
|
|
25
|
+
return new Intl.NumberFormat(props.locale, {
|
|
26
|
+
style: "currency",
|
|
27
|
+
currency: (props.currency ?? price.currency).toUpperCase()
|
|
28
|
+
}).format(price.amount / 100);
|
|
29
|
+
}
|
|
30
|
+
function intervalLabel(price) {
|
|
31
|
+
const singular = { day: "day", week: "week", month: "month", year: "year" };
|
|
32
|
+
const unit = singular[price.interval];
|
|
33
|
+
if (price.intervalCount <= 1) return ti(unit, unit);
|
|
34
|
+
return `${price.intervalCount} ${ti(`${unit}s`, `${unit}s`)}`;
|
|
35
|
+
}
|
|
36
|
+
const labelOf = (plan) => tp(`${plan.key}.label`, plan.label);
|
|
37
|
+
const descriptionOf = (plan) => plan.description ? tp(`${plan.key}.description`, plan.description) : "";
|
|
38
|
+
function stateOf(plan) {
|
|
39
|
+
if (!isActive.value) return "subscribe";
|
|
40
|
+
if (plan.key === currentPlan.value) return "current";
|
|
41
|
+
if (currentRank.value === null) return "subscribe";
|
|
42
|
+
return plan.rank > currentRank.value ? "upgrade" : "downgrade";
|
|
43
|
+
}
|
|
44
|
+
const stateLabel = (state) => t(state, { current: "Current plan", subscribe: "Subscribe", upgrade: "Upgrade", downgrade: "Downgrade" }[state]);
|
|
45
|
+
const confirming = ref(null);
|
|
46
|
+
const confirmOpen = computed({
|
|
47
|
+
get: () => confirming.value !== null,
|
|
48
|
+
set: (value) => {
|
|
49
|
+
if (!value) confirming.value = null;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
async function go(plan, price) {
|
|
53
|
+
emit("select", plan, price);
|
|
54
|
+
confirming.value = null;
|
|
55
|
+
await subscribe(plan.key).catch(() => void 0);
|
|
56
|
+
}
|
|
57
|
+
function choose(plan) {
|
|
58
|
+
const price = priceFor(plan);
|
|
59
|
+
if (!price) return;
|
|
60
|
+
if (props.confirmChange && isActive.value) confirming.value = { plan, price };
|
|
61
|
+
else go(plan, price);
|
|
62
|
+
}
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<template>
|
|
66
|
+
<div class="space-y-4">
|
|
67
|
+
<slot
|
|
68
|
+
v-if="status === 'pending' && !visible.length"
|
|
69
|
+
name="loading"
|
|
70
|
+
>
|
|
71
|
+
<p class="text-center text-sm text-muted">
|
|
72
|
+
{{ t("loading", "Loading plans\u2026") }}
|
|
73
|
+
</p>
|
|
74
|
+
</slot>
|
|
75
|
+
|
|
76
|
+
<slot
|
|
77
|
+
v-else-if="!visible.length"
|
|
78
|
+
name="empty"
|
|
79
|
+
>
|
|
80
|
+
<p class="text-center text-sm text-muted">
|
|
81
|
+
{{ t("empty", "No plans are available right now.") }}
|
|
82
|
+
</p>
|
|
83
|
+
</slot>
|
|
84
|
+
|
|
85
|
+
<UAlert
|
|
86
|
+
v-if="error"
|
|
87
|
+
color="error"
|
|
88
|
+
variant="subtle"
|
|
89
|
+
:description="error"
|
|
90
|
+
/>
|
|
91
|
+
|
|
92
|
+
<div
|
|
93
|
+
v-if="visible.length"
|
|
94
|
+
class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3"
|
|
95
|
+
>
|
|
96
|
+
<UCard
|
|
97
|
+
v-for="plan in visible"
|
|
98
|
+
:key="plan.key"
|
|
99
|
+
:data-plan="plan.key"
|
|
100
|
+
>
|
|
101
|
+
<template #header>
|
|
102
|
+
<slot
|
|
103
|
+
name="header"
|
|
104
|
+
:plan="plan"
|
|
105
|
+
:state="stateOf(plan)"
|
|
106
|
+
>
|
|
107
|
+
<div class="flex items-center justify-between gap-2">
|
|
108
|
+
<h3 class="font-semibold">
|
|
109
|
+
{{ labelOf(plan) }}
|
|
110
|
+
</h3>
|
|
111
|
+
<slot
|
|
112
|
+
name="badge"
|
|
113
|
+
:plan="plan"
|
|
114
|
+
:state="stateOf(plan)"
|
|
115
|
+
>
|
|
116
|
+
<UBadge
|
|
117
|
+
v-if="stateOf(plan) === 'current'"
|
|
118
|
+
color="primary"
|
|
119
|
+
variant="subtle"
|
|
120
|
+
>
|
|
121
|
+
{{ t("current", "Current plan") }}
|
|
122
|
+
</UBadge>
|
|
123
|
+
</slot>
|
|
124
|
+
</div>
|
|
125
|
+
<p
|
|
126
|
+
v-if="descriptionOf(plan)"
|
|
127
|
+
class="mt-1 text-sm text-muted"
|
|
128
|
+
>
|
|
129
|
+
{{ descriptionOf(plan) }}
|
|
130
|
+
</p>
|
|
131
|
+
</slot>
|
|
132
|
+
</template>
|
|
133
|
+
|
|
134
|
+
<slot
|
|
135
|
+
name="price"
|
|
136
|
+
:plan="plan"
|
|
137
|
+
:price="priceFor(plan)"
|
|
138
|
+
:format-price="formatPrice"
|
|
139
|
+
:interval-label="intervalLabel"
|
|
140
|
+
>
|
|
141
|
+
<template v-if="priceFor(plan)">
|
|
142
|
+
<p class="text-2xl font-bold">
|
|
143
|
+
{{ formatPrice(priceFor(plan)) }}
|
|
144
|
+
<span class="text-sm font-normal text-muted">/ {{ intervalLabel(priceFor(plan)) }}</span>
|
|
145
|
+
</p>
|
|
146
|
+
<p
|
|
147
|
+
v-if="priceFor(plan).trialPeriodDays"
|
|
148
|
+
class="mt-1 text-sm text-muted"
|
|
149
|
+
>
|
|
150
|
+
{{ t("trial", "{days}-day free trial", { days: priceFor(plan).trialPeriodDays }) }}
|
|
151
|
+
</p>
|
|
152
|
+
</template>
|
|
153
|
+
</slot>
|
|
154
|
+
|
|
155
|
+
<slot
|
|
156
|
+
name="features"
|
|
157
|
+
:plan="plan"
|
|
158
|
+
/>
|
|
159
|
+
|
|
160
|
+
<template #footer>
|
|
161
|
+
<slot
|
|
162
|
+
name="action"
|
|
163
|
+
:plan="plan"
|
|
164
|
+
:state="stateOf(plan)"
|
|
165
|
+
:pending="pending"
|
|
166
|
+
:select="() => choose(plan)"
|
|
167
|
+
>
|
|
168
|
+
<UButton
|
|
169
|
+
block
|
|
170
|
+
:loading="pending"
|
|
171
|
+
:disabled="stateOf(plan) === 'current' || !priceFor(plan)"
|
|
172
|
+
:variant="stateOf(plan) === 'current' ? 'outline' : 'solid'"
|
|
173
|
+
:label="stateLabel(stateOf(plan))"
|
|
174
|
+
@click="choose(plan)"
|
|
175
|
+
/>
|
|
176
|
+
</slot>
|
|
177
|
+
</template>
|
|
178
|
+
</UCard>
|
|
179
|
+
</div>
|
|
180
|
+
|
|
181
|
+
<UModal
|
|
182
|
+
v-model:open="confirmOpen"
|
|
183
|
+
:title="t('confirmTitle', 'Change plan')"
|
|
184
|
+
>
|
|
185
|
+
<template #body>
|
|
186
|
+
<slot
|
|
187
|
+
name="confirm"
|
|
188
|
+
:plan="confirming?.plan"
|
|
189
|
+
:price="confirming?.price"
|
|
190
|
+
>
|
|
191
|
+
<p>{{ t("confirmBody", "Switch to {plan}? Your subscription will be updated.", { plan: confirming ? labelOf(confirming.plan) : "" }) }}</p>
|
|
192
|
+
</slot>
|
|
193
|
+
</template>
|
|
194
|
+
<template #footer>
|
|
195
|
+
<slot
|
|
196
|
+
name="confirm-actions"
|
|
197
|
+
:plan="confirming?.plan"
|
|
198
|
+
:price="confirming?.price"
|
|
199
|
+
:confirm="() => confirming && go(confirming.plan, confirming.price)"
|
|
200
|
+
>
|
|
201
|
+
<UButton
|
|
202
|
+
variant="outline"
|
|
203
|
+
:label="t('cancel', 'Cancel')"
|
|
204
|
+
@click="confirming = null"
|
|
205
|
+
/>
|
|
206
|
+
<UButton
|
|
207
|
+
:label="t('confirm', 'Confirm')"
|
|
208
|
+
:loading="pending"
|
|
209
|
+
@click="confirming && go(confirming.plan, confirming.price)"
|
|
210
|
+
/>
|
|
211
|
+
</slot>
|
|
212
|
+
</template>
|
|
213
|
+
</UModal>
|
|
214
|
+
</div>
|
|
215
|
+
</template>
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { SubscriptionPlan, SubscriptionPlanPrice } from '../composables/usePlans.js';
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
locale?: string;
|
|
4
|
+
/** Override the price currency formatting currency (defaults to the price's own currency). */
|
|
5
|
+
currency?: string;
|
|
6
|
+
interval?: SubscriptionPlanPrice['interval'];
|
|
7
|
+
/** Only show these plan keys. */
|
|
8
|
+
plans?: string[];
|
|
9
|
+
/** Ask for confirmation (modal) before changing an existing subscription. */
|
|
10
|
+
confirmChange?: boolean;
|
|
11
|
+
};
|
|
12
|
+
declare function formatPrice(price: SubscriptionPlanPrice): string;
|
|
13
|
+
declare function intervalLabel(price: SubscriptionPlanPrice): any;
|
|
14
|
+
declare var __VLS_1: {}, __VLS_3: {}, __VLS_17: {
|
|
15
|
+
plan: any;
|
|
16
|
+
state: "upgrade" | "downgrade" | "subscribe" | "current";
|
|
17
|
+
}, __VLS_19: {
|
|
18
|
+
plan: any;
|
|
19
|
+
state: "upgrade" | "downgrade" | "subscribe" | "current";
|
|
20
|
+
}, __VLS_27: {
|
|
21
|
+
plan: any;
|
|
22
|
+
price: SubscriptionPlanPrice | undefined;
|
|
23
|
+
formatPrice: typeof formatPrice;
|
|
24
|
+
intervalLabel: typeof intervalLabel;
|
|
25
|
+
}, __VLS_29: {
|
|
26
|
+
plan: any;
|
|
27
|
+
}, __VLS_32: {
|
|
28
|
+
plan: any;
|
|
29
|
+
state: "upgrade" | "downgrade" | "subscribe" | "current";
|
|
30
|
+
pending: any;
|
|
31
|
+
select: () => void;
|
|
32
|
+
}, __VLS_48: {
|
|
33
|
+
plan: any;
|
|
34
|
+
price: any;
|
|
35
|
+
}, __VLS_51: {
|
|
36
|
+
plan: any;
|
|
37
|
+
price: any;
|
|
38
|
+
confirm: () => any;
|
|
39
|
+
};
|
|
40
|
+
type __VLS_Slots = {} & {
|
|
41
|
+
loading?: (props: typeof __VLS_1) => any;
|
|
42
|
+
} & {
|
|
43
|
+
empty?: (props: typeof __VLS_3) => any;
|
|
44
|
+
} & {
|
|
45
|
+
header?: (props: typeof __VLS_17) => any;
|
|
46
|
+
} & {
|
|
47
|
+
badge?: (props: typeof __VLS_19) => any;
|
|
48
|
+
} & {
|
|
49
|
+
price?: (props: typeof __VLS_27) => any;
|
|
50
|
+
} & {
|
|
51
|
+
features?: (props: typeof __VLS_29) => any;
|
|
52
|
+
} & {
|
|
53
|
+
action?: (props: typeof __VLS_32) => any;
|
|
54
|
+
} & {
|
|
55
|
+
confirm?: (props: typeof __VLS_48) => any;
|
|
56
|
+
} & {
|
|
57
|
+
'confirm-actions'?: (props: typeof __VLS_51) => any;
|
|
58
|
+
};
|
|
59
|
+
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
60
|
+
select: (plan: SubscriptionPlan, price: SubscriptionPlanPrice) => any;
|
|
61
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
62
|
+
onSelect?: ((plan: SubscriptionPlan, price: SubscriptionPlanPrice) => any) | undefined;
|
|
63
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
64
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
65
|
+
declare const _default: typeof __VLS_export;
|
|
66
|
+
export default _default;
|
|
67
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
68
|
+
new (): {
|
|
69
|
+
$slots: S;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
type __VLS_Props = {
|
|
2
|
+
locale?: string;
|
|
3
|
+
};
|
|
4
|
+
declare var __VLS_9: {}, __VLS_11: {}, __VLS_13: {
|
|
5
|
+
plan: any;
|
|
6
|
+
label: any;
|
|
7
|
+
subscription: any;
|
|
8
|
+
}, __VLS_15: {
|
|
9
|
+
subscription: any;
|
|
10
|
+
badge: any;
|
|
11
|
+
}, __VLS_23: {
|
|
12
|
+
subscription: any;
|
|
13
|
+
cancelsAt: any;
|
|
14
|
+
dateText: any;
|
|
15
|
+
}, __VLS_25: {}, __VLS_33: {
|
|
16
|
+
manage: () => Promise<void | undefined>;
|
|
17
|
+
resubscribe: () => Promise<any>;
|
|
18
|
+
cancelling: boolean;
|
|
19
|
+
pending: any;
|
|
20
|
+
};
|
|
21
|
+
type __VLS_Slots = {} & {
|
|
22
|
+
header?: (props: typeof __VLS_9) => any;
|
|
23
|
+
} & {
|
|
24
|
+
loading?: (props: typeof __VLS_11) => any;
|
|
25
|
+
} & {
|
|
26
|
+
plan?: (props: typeof __VLS_13) => any;
|
|
27
|
+
} & {
|
|
28
|
+
badge?: (props: typeof __VLS_15) => any;
|
|
29
|
+
} & {
|
|
30
|
+
dates?: (props: typeof __VLS_23) => any;
|
|
31
|
+
} & {
|
|
32
|
+
empty?: (props: typeof __VLS_25) => any;
|
|
33
|
+
} & {
|
|
34
|
+
actions?: (props: typeof __VLS_33) => any;
|
|
35
|
+
};
|
|
36
|
+
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
37
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
38
|
+
declare const _default: typeof __VLS_export;
|
|
39
|
+
export default _default;
|
|
40
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
41
|
+
new (): {
|
|
42
|
+
$slots: S;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { usePlans } from "../composables/usePlans";
|
|
3
|
+
import { useSubscription } from "../composables/useSubscription";
|
|
4
|
+
defineOptions({ name: "SubscriptionStatus" });
|
|
5
|
+
const props = defineProps({
|
|
6
|
+
locale: { type: String, required: false }
|
|
7
|
+
});
|
|
8
|
+
const t = useModuleT("subscriptions.status");
|
|
9
|
+
const tp = useModuleT("subscriptions.catalog");
|
|
10
|
+
const { plans } = usePlans();
|
|
11
|
+
const { subscription, plan, status, isActive, cancelsAt, pending, error, subscribe, openPortal } = useSubscription();
|
|
12
|
+
const planLabel = computed(() => {
|
|
13
|
+
const found = plans.value.find((p) => p.key === plan.value);
|
|
14
|
+
return plan.value ? tp(`${plan.value}.label`, found?.label ?? plan.value) : plan.value;
|
|
15
|
+
});
|
|
16
|
+
const badge = computed(() => {
|
|
17
|
+
const value = subscription.value?.status;
|
|
18
|
+
if (value === "active") return { label: t("active", "Active"), color: "success" };
|
|
19
|
+
if (value === "trialing") return { label: t("trialing", "Trial"), color: "info" };
|
|
20
|
+
if (value === "past_due" || value === "unpaid") return { label: t("pastDue", "Past due"), color: "warning" };
|
|
21
|
+
if (value === "canceled") return { label: t("canceled", "Canceled"), color: "neutral" };
|
|
22
|
+
return { label: t("other", "Inactive"), color: "neutral" };
|
|
23
|
+
});
|
|
24
|
+
function formatDate(value) {
|
|
25
|
+
return value ? new Intl.DateTimeFormat(props.locale, { dateStyle: "long" }).format(new Date(value)) : "";
|
|
26
|
+
}
|
|
27
|
+
const dateText = computed(() => {
|
|
28
|
+
if (cancelsAt.value) return t("cancels", "Ends on {date}", { date: formatDate(cancelsAt.value) });
|
|
29
|
+
const end = subscription.value?.currentPeriodEnd;
|
|
30
|
+
return end ? t("renews", "Renews on {date}", { date: formatDate(end) }) : "";
|
|
31
|
+
});
|
|
32
|
+
const resubscribe = () => subscription.value?.planKey ? subscribe(subscription.value.planKey).catch(() => void 0) : subscribe(subscription.value?.priceId ?? "").catch(() => void 0);
|
|
33
|
+
const manage = () => openPortal().catch(() => void 0);
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<template>
|
|
37
|
+
<UCard>
|
|
38
|
+
<template #header>
|
|
39
|
+
<slot name="header">
|
|
40
|
+
<h3 class="font-semibold">
|
|
41
|
+
{{ t("title", "Your subscription") }}
|
|
42
|
+
</h3>
|
|
43
|
+
</slot>
|
|
44
|
+
</template>
|
|
45
|
+
|
|
46
|
+
<slot
|
|
47
|
+
v-if="status === 'pending' && !subscription"
|
|
48
|
+
name="loading"
|
|
49
|
+
>
|
|
50
|
+
<p class="text-sm text-muted">
|
|
51
|
+
{{ t("loading", "Loading subscription\u2026") }}
|
|
52
|
+
</p>
|
|
53
|
+
</slot>
|
|
54
|
+
|
|
55
|
+
<template v-else-if="isActive && subscription">
|
|
56
|
+
<div class="flex flex-wrap items-center gap-2">
|
|
57
|
+
<slot
|
|
58
|
+
name="plan"
|
|
59
|
+
:plan="plan"
|
|
60
|
+
:label="planLabel"
|
|
61
|
+
:subscription="subscription"
|
|
62
|
+
>
|
|
63
|
+
<span class="font-medium">{{ planLabel }}</span>
|
|
64
|
+
</slot>
|
|
65
|
+
<slot
|
|
66
|
+
name="badge"
|
|
67
|
+
:subscription="subscription"
|
|
68
|
+
:badge="badge"
|
|
69
|
+
>
|
|
70
|
+
<UBadge
|
|
71
|
+
:color="badge.color"
|
|
72
|
+
variant="subtle"
|
|
73
|
+
>
|
|
74
|
+
{{ badge.label }}
|
|
75
|
+
</UBadge>
|
|
76
|
+
</slot>
|
|
77
|
+
</div>
|
|
78
|
+
<slot
|
|
79
|
+
name="dates"
|
|
80
|
+
:subscription="subscription"
|
|
81
|
+
:cancels-at="cancelsAt"
|
|
82
|
+
:date-text="dateText"
|
|
83
|
+
>
|
|
84
|
+
<p
|
|
85
|
+
v-if="dateText"
|
|
86
|
+
class="mt-2 text-sm text-muted"
|
|
87
|
+
>
|
|
88
|
+
{{ dateText }}
|
|
89
|
+
</p>
|
|
90
|
+
</slot>
|
|
91
|
+
</template>
|
|
92
|
+
|
|
93
|
+
<slot
|
|
94
|
+
v-else
|
|
95
|
+
name="empty"
|
|
96
|
+
>
|
|
97
|
+
<p class="text-sm text-muted">
|
|
98
|
+
{{ t("none", "You do not have an active subscription.") }}
|
|
99
|
+
</p>
|
|
100
|
+
</slot>
|
|
101
|
+
|
|
102
|
+
<UAlert
|
|
103
|
+
v-if="error"
|
|
104
|
+
class="mt-3"
|
|
105
|
+
color="error"
|
|
106
|
+
variant="subtle"
|
|
107
|
+
:description="error"
|
|
108
|
+
/>
|
|
109
|
+
|
|
110
|
+
<template
|
|
111
|
+
v-if="isActive"
|
|
112
|
+
#footer
|
|
113
|
+
>
|
|
114
|
+
<slot
|
|
115
|
+
name="actions"
|
|
116
|
+
:manage="manage"
|
|
117
|
+
:resubscribe="resubscribe"
|
|
118
|
+
:cancelling="!!cancelsAt"
|
|
119
|
+
:pending="pending"
|
|
120
|
+
>
|
|
121
|
+
<div class="flex flex-wrap gap-2">
|
|
122
|
+
<UButton
|
|
123
|
+
:loading="pending"
|
|
124
|
+
:label="t('manage', 'Manage billing')"
|
|
125
|
+
@click="manage"
|
|
126
|
+
/>
|
|
127
|
+
<UButton
|
|
128
|
+
v-if="cancelsAt"
|
|
129
|
+
variant="outline"
|
|
130
|
+
:loading="pending"
|
|
131
|
+
:label="t('resubscribe', 'Resubscribe')"
|
|
132
|
+
@click="resubscribe"
|
|
133
|
+
/>
|
|
134
|
+
</div>
|
|
135
|
+
</slot>
|
|
136
|
+
</template>
|
|
137
|
+
</UCard>
|
|
138
|
+
</template>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
type __VLS_Props = {
|
|
2
|
+
locale?: string;
|
|
3
|
+
};
|
|
4
|
+
declare var __VLS_9: {}, __VLS_11: {}, __VLS_13: {
|
|
5
|
+
plan: any;
|
|
6
|
+
label: any;
|
|
7
|
+
subscription: any;
|
|
8
|
+
}, __VLS_15: {
|
|
9
|
+
subscription: any;
|
|
10
|
+
badge: any;
|
|
11
|
+
}, __VLS_23: {
|
|
12
|
+
subscription: any;
|
|
13
|
+
cancelsAt: any;
|
|
14
|
+
dateText: any;
|
|
15
|
+
}, __VLS_25: {}, __VLS_33: {
|
|
16
|
+
manage: () => Promise<void | undefined>;
|
|
17
|
+
resubscribe: () => Promise<any>;
|
|
18
|
+
cancelling: boolean;
|
|
19
|
+
pending: any;
|
|
20
|
+
};
|
|
21
|
+
type __VLS_Slots = {} & {
|
|
22
|
+
header?: (props: typeof __VLS_9) => any;
|
|
23
|
+
} & {
|
|
24
|
+
loading?: (props: typeof __VLS_11) => any;
|
|
25
|
+
} & {
|
|
26
|
+
plan?: (props: typeof __VLS_13) => any;
|
|
27
|
+
} & {
|
|
28
|
+
badge?: (props: typeof __VLS_15) => any;
|
|
29
|
+
} & {
|
|
30
|
+
dates?: (props: typeof __VLS_23) => any;
|
|
31
|
+
} & {
|
|
32
|
+
empty?: (props: typeof __VLS_25) => any;
|
|
33
|
+
} & {
|
|
34
|
+
actions?: (props: typeof __VLS_33) => any;
|
|
35
|
+
};
|
|
36
|
+
declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
37
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
38
|
+
declare const _default: typeof __VLS_export;
|
|
39
|
+
export default _default;
|
|
40
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
41
|
+
new (): {
|
|
42
|
+
$slots: S;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface SubscriptionPlanPrice {
|
|
2
|
+
id: string;
|
|
3
|
+
/** Minor units (e.g. cents). */
|
|
4
|
+
amount: number | null;
|
|
5
|
+
currency: string;
|
|
6
|
+
interval: 'day' | 'week' | 'month' | 'year';
|
|
7
|
+
intervalCount: number;
|
|
8
|
+
trialPeriodDays: number | null;
|
|
9
|
+
lookupKey: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface SubscriptionPlan {
|
|
12
|
+
key: string;
|
|
13
|
+
label: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
rank: number;
|
|
16
|
+
prices: SubscriptionPlanPrice[];
|
|
17
|
+
}
|
|
18
|
+
export declare const usePlans: () => {
|
|
19
|
+
plans: any;
|
|
20
|
+
status: any;
|
|
21
|
+
error: any;
|
|
22
|
+
refresh: any;
|
|
23
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const usePlans = () => {
|
|
2
|
+
const { data, status, error, refresh } = useFetch("/api/subscriptions/plans", {
|
|
3
|
+
key: "subscriptions:plans",
|
|
4
|
+
default: () => ({ plans: [] })
|
|
5
|
+
});
|
|
6
|
+
const plans = computed(
|
|
7
|
+
() => [...data.value?.plans ?? []].sort((a, b) => a.rank - b.rank)
|
|
8
|
+
);
|
|
9
|
+
return { plans, status, error, refresh };
|
|
10
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter } from 'vue';
|
|
2
|
+
export interface UserSubscription {
|
|
3
|
+
id: string;
|
|
4
|
+
planKey: string | null;
|
|
5
|
+
priceId: string;
|
|
6
|
+
status: string;
|
|
7
|
+
cancelAtPeriodEnd: boolean;
|
|
8
|
+
currentPeriodStart: string | null;
|
|
9
|
+
currentPeriodEnd: string | null;
|
|
10
|
+
endedAt: string | null;
|
|
11
|
+
}
|
|
12
|
+
export declare const useSubscription: () => {
|
|
13
|
+
subscription: any;
|
|
14
|
+
plan: any;
|
|
15
|
+
status: any;
|
|
16
|
+
isActive: any;
|
|
17
|
+
cancelsAt: any;
|
|
18
|
+
has: (planKey: MaybeRefOrGetter<string>) => any;
|
|
19
|
+
pending: any;
|
|
20
|
+
error: any;
|
|
21
|
+
refresh: any;
|
|
22
|
+
subscribe: (planKeyOrPriceId: string, options?: {
|
|
23
|
+
successUrl?: string;
|
|
24
|
+
cancelUrl?: string;
|
|
25
|
+
}) => Promise<any>;
|
|
26
|
+
openPortal: (returnUrl?: string) => Promise<void>;
|
|
27
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const ACTIVE_STATUSES = ["active", "trialing"];
|
|
2
|
+
export const useSubscription = () => {
|
|
3
|
+
const { loggedIn } = useUserSession();
|
|
4
|
+
const t = useModuleT("subscriptions");
|
|
5
|
+
const pending = ref(false);
|
|
6
|
+
const error = ref(null);
|
|
7
|
+
const { data, status, refresh } = useFetch("/api/subscriptions/me", {
|
|
8
|
+
key: "subscriptions:me",
|
|
9
|
+
default: () => ({ subscriptions: [], activePlan: null }),
|
|
10
|
+
immediate: loggedIn.value,
|
|
11
|
+
watch: false
|
|
12
|
+
});
|
|
13
|
+
watch(loggedIn, (value) => {
|
|
14
|
+
if (value) refresh();
|
|
15
|
+
else data.value = { subscriptions: [], activePlan: null };
|
|
16
|
+
});
|
|
17
|
+
const subscription = computed(() => {
|
|
18
|
+
const list = data.value?.subscriptions ?? [];
|
|
19
|
+
return list.find((item) => ACTIVE_STATUSES.includes(item.status)) ?? null;
|
|
20
|
+
});
|
|
21
|
+
const plan = computed(() => data.value?.activePlan ?? subscription.value?.planKey ?? null);
|
|
22
|
+
const isActive = computed(() => subscription.value !== null);
|
|
23
|
+
const cancelsAt = computed(
|
|
24
|
+
() => subscription.value?.cancelAtPeriodEnd ? subscription.value.currentPeriodEnd : null
|
|
25
|
+
);
|
|
26
|
+
function has(planKey) {
|
|
27
|
+
return isActive.value && plan.value === toValue(planKey);
|
|
28
|
+
}
|
|
29
|
+
async function run(request, fallback) {
|
|
30
|
+
pending.value = true;
|
|
31
|
+
error.value = null;
|
|
32
|
+
try {
|
|
33
|
+
return await request();
|
|
34
|
+
} catch (err) {
|
|
35
|
+
error.value = err instanceof Error ? err.message : fallback;
|
|
36
|
+
throw err;
|
|
37
|
+
} finally {
|
|
38
|
+
pending.value = false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function subscribe(planKeyOrPriceId, options = {}) {
|
|
42
|
+
const isPrice = planKeyOrPriceId.startsWith("price_");
|
|
43
|
+
const config = useRuntimeConfig().public.subscriptions;
|
|
44
|
+
const origin = import.meta.client ? window.location.origin : "";
|
|
45
|
+
const absolute = (path) => path ? `${origin}${path}` : void 0;
|
|
46
|
+
return run(async () => {
|
|
47
|
+
const result = await $fetch("/api/subscriptions/checkout", {
|
|
48
|
+
method: "POST",
|
|
49
|
+
body: {
|
|
50
|
+
...isPrice ? { priceId: planKeyOrPriceId } : { planKey: planKeyOrPriceId },
|
|
51
|
+
successUrl: options.successUrl ?? absolute(config?.pages?.success),
|
|
52
|
+
cancelUrl: options.cancelUrl ?? absolute(config?.pages?.cancel)
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
if ("url" in result) {
|
|
56
|
+
await navigateTo(result.url, { external: true });
|
|
57
|
+
} else {
|
|
58
|
+
await refresh();
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}, t("errors.checkout", "Failed to start checkout."));
|
|
62
|
+
}
|
|
63
|
+
function openPortal(returnUrl) {
|
|
64
|
+
return run(async () => {
|
|
65
|
+
const { url } = await $fetch("/api/subscriptions/portal", {
|
|
66
|
+
method: "POST",
|
|
67
|
+
body: { returnUrl: returnUrl ?? (import.meta.client ? window.location.href : void 0) }
|
|
68
|
+
});
|
|
69
|
+
await navigateTo(url, { external: true });
|
|
70
|
+
}, t("errors.portal", "Failed to open the billing portal."));
|
|
71
|
+
}
|
|
72
|
+
return { subscription, plan, status, isActive, cancelsAt, has, pending, error, refresh, subscribe, openPortal };
|
|
73
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default defineNuxtRouteMiddleware(async (to) => {
|
|
2
|
+
const meta = to.meta.subscription;
|
|
3
|
+
const config = useRuntimeConfig().public.subscriptions;
|
|
4
|
+
const target = meta?.redirectTo ?? config?.pages?.membership;
|
|
5
|
+
if (!target || to.path === target) return;
|
|
6
|
+
const { loggedIn } = useUserSession();
|
|
7
|
+
let activePlan = null;
|
|
8
|
+
if (loggedIn.value) {
|
|
9
|
+
const me = await useRequestFetch()("/api/subscriptions/me").catch(() => null);
|
|
10
|
+
activePlan = me?.activePlan ?? null;
|
|
11
|
+
}
|
|
12
|
+
const allowed = activePlan !== null && (!meta?.plans?.length || meta.plans.includes(activePlan));
|
|
13
|
+
if (!allowed) return navigateTo({ path: target, query: { redirect: to.fullPath } });
|
|
14
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
declare const __VLS_export: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
2
|
+
declare const _default: typeof __VLS_export;
|
|
3
|
+
export default _default;
|