Stripe: Preise je Produkt, Testrabatt Hosting, Kundenportal „Abo verwalten“, Mindestlaufzeit ab termStartAt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7799d7c4de
commit
e5cc57cefd
7 files changed
+402
-35
No files matched your search
+15
-1
@@ -31,12 +31,20 @@ const products = {
|
||||
name: 'WEBklar Wartungs-Abo',
|
||||
lookupKey: process.env.STRIPE_ABO_PRICE_LOOKUP_KEY || 'webklar_wartung_monatlich_5000',
|
||||
amountCents: 5000,
|
||||
// Version 4 (Rechtsrunde 28.09.2026): beliebig viele Postfächer statt 1 E-Mail-Postfach (F07)
|
||||
description:
|
||||
'Betrieb und Pflege Ihrer Website bzw. Web-Anwendung auf unserem Server in Deutschland (Sicherheitsupdates, SSL, tägliche Datensicherung, Überwachung der Erreichbarkeit), 1 Domain inklusive Verwaltung, 1 E-Mail-Postfach (5 GB) und 1 Stunde Arbeitszeit pro Monat (12 Monate nutzbar). Mindestlaufzeit 12 Monate, danach monatlich kündbar. Gemäß § 19 UStG ohne Umsatzsteuer.',
|
||||
'Betrieb und Pflege Ihrer Website bzw. Web-Anwendung auf unserem Server in Deutschland (Sicherheitsupdates, SSL, tägliche Datensicherung, Überwachung der Erreichbarkeit), 1 Domain inklusive Verwaltung, beliebig viele Postfächer (zusammen 5 GB) und 1 Stunde Arbeitszeit pro Monat (12 Monate nutzbar). Mindestlaufzeit 12 Monate, danach monatlich kündbar. Gemäß § 19 UStG ohne Umsatzsteuer.',
|
||||
statementDescriptor: 'WEBKLAR WARTUNG',
|
||||
priceNickname: 'Wartungs-Abo monatlich',
|
||||
minTermMonths: 12,
|
||||
minutesPerMonth: 60,
|
||||
// Leistungen fuers Kundenportal (Vergleich, Bestellseite): [Titel, Detail], Spec 3.1 / Fassung 4
|
||||
features: [
|
||||
['Betrieb & Pflege Ihrer Website bzw. Web-Anwendung', 'Server in Deutschland, Updates, Sicherheit, tägliche Datensicherung, Überwachung'],
|
||||
['1 Domain inklusive', 'Registrierung auf Ihren Namen, DNS- und Zertifikatsverwaltung'],
|
||||
['beliebig viele Postfächer', 'zusammen 5 GB Speicher'],
|
||||
['1 Stunde Arbeitszeit pro Monat', 'nicht genutzte Zeit bleibt 12 Monate verfügbar'],
|
||||
],
|
||||
},
|
||||
// Noch nicht bestellbar: Die Beschreibung geht erst mit den Vertragsbedingungen Version 4 an Stripe
|
||||
hosting: {
|
||||
@@ -50,6 +58,12 @@ const products = {
|
||||
priceNickname: 'Hosting-Abo monatlich',
|
||||
minTermMonths: 12,
|
||||
minutesPerMonth: 0,
|
||||
features: [
|
||||
['Bereitstellung Ihrer statischen Website', 'Server in Deutschland, Updates, Sicherheit, tägliche Datensicherung, Überwachung'],
|
||||
['1 Domain inklusive', 'Registrierung auf Ihren Namen, DNS- und Zertifikatsverwaltung'],
|
||||
['1 E-Mail-Postfach', 'mit Ihrer eigenen Domain, 5 GB Speicher'],
|
||||
['Änderungen nur nach Freigabe', '100 € pro Stunde'],
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,30 @@ export function getProduct(key) {
|
||||
return product
|
||||
}
|
||||
|
||||
/**
|
||||
* Vertragsunterlagen zur zugestimmten Fassung: Version 4 (Suffix "-v4") zeigt die aktuellen
|
||||
* Bedingungen samt PDF, jede aeltere Version (auch unbekannt/keine) das Archiv der Version 3.
|
||||
*/
|
||||
export function termsDocFor(termsVersion) {
|
||||
if (typeof termsVersion === 'string' && termsVersion.endsWith('-v4')) {
|
||||
return { url: '/abo-bedingungen.html', pdfFile: 'WEBklar-Abo-Vertragsunterlagen.pdf' }
|
||||
}
|
||||
return { url: '/vertragsunterlagen/v3/abo-bedingungen.html', pdfFile: 'WEBklar-Wartungs-Abo-Vertragsunterlagen.pdf' }
|
||||
}
|
||||
|
||||
/** Ansicht eines Produkts fuers Kundenportal (Vergleich, Bestellseite): Preis und Leistungen. */
|
||||
export function productView(key) {
|
||||
const product = getProduct(key)
|
||||
return {
|
||||
key: product.key,
|
||||
name: product.name,
|
||||
amountCents: product.amountCents,
|
||||
minTermMonths: product.minTermMonths,
|
||||
minutesPerMonth: product.minutesPerMonth,
|
||||
features: product.features,
|
||||
}
|
||||
}
|
||||
|
||||
/** lookup_key eines Stripe-Preises -> 'wartung' | 'hosting' | 'mailStorage' | null */
|
||||
export function productKeyForLookupKey(lookupKey) {
|
||||
if (!lookupKey) return null
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
updateDocument,
|
||||
} from './appwriteAdmin.js'
|
||||
import { lateStartAt } from './deadlines.js'
|
||||
import { creditMinutesForInvoice, describeItems, priceIdsOfInvoice, productKeyForLookupKey } from './products.js'
|
||||
import { creditMinutesForInvoice, describeItems, getProduct, priceIdsOfInvoice, productKeyForLookupKey } from './products.js'
|
||||
|
||||
/**
|
||||
* Wartungs-Abo ueber Stripe Billing.
|
||||
@@ -50,40 +50,55 @@ export function isStripeConfigured() {
|
||||
return Boolean(config.stripe.secretKey)
|
||||
}
|
||||
|
||||
let cachedPriceId = null
|
||||
/** Preis per lookup_key holen oder (einmalig) samt Produkt anlegen. */
|
||||
export async function ensureAboPrice() {
|
||||
if (cachedPriceId) return cachedPriceId
|
||||
// Produkt-Metadaten "webklar" je Katalog-Schluessel (F09/Produces: ensurePrice)
|
||||
const PRODUCT_METADATA = { wartung: 'wartungs-abo', hosting: 'hosting-abo', mailStorage: 'mail-speicher' }
|
||||
|
||||
/** Katalogeintrag zum Schluessel: die beiden Abos aus products.js, der Zusatzspeicher aus config.mailStorage. */
|
||||
function catalogFor(key) {
|
||||
return key === 'mailStorage' ? config.mailStorage : getProduct(key)
|
||||
}
|
||||
|
||||
const priceIdCache = new Map()
|
||||
/**
|
||||
* Preis per lookup_key holen oder (einmalig) samt Produkt anlegen. key: 'wartung' | 'hosting' |
|
||||
* 'mailStorage'. Ersetzt das fruehere ensureAboPrice (nur Wartung); lookup_keys werden bei Stripe
|
||||
* nie uebertragen, ein neuer Preis bekaeme also ohnehin einen neuen lookup_key im Katalog.
|
||||
*/
|
||||
export async function ensurePrice(key) {
|
||||
if (priceIdCache.has(key)) return priceIdCache.get(key)
|
||||
const stripe = getStripe()
|
||||
const catalog = catalogFor(key)
|
||||
const { data } = await stripe.prices.list({
|
||||
lookup_keys: [config.stripe.priceLookupKey],
|
||||
lookup_keys: [catalog.lookupKey],
|
||||
active: true,
|
||||
limit: 1,
|
||||
})
|
||||
if (data[0]) {
|
||||
cachedPriceId = data[0].id
|
||||
return cachedPriceId
|
||||
priceIdCache.set(key, data[0].id)
|
||||
return data[0].id
|
||||
}
|
||||
const product = await stripe.products.create({
|
||||
name: config.stripe.productName,
|
||||
description: config.stripe.productDescription,
|
||||
statement_descriptor: config.stripe.statementDescriptor,
|
||||
images: [config.stripe.productImage],
|
||||
metadata: { webklar: 'wartungs-abo' },
|
||||
name: catalog.name,
|
||||
...(catalog.description ? { description: catalog.description } : {}),
|
||||
...(catalog.statementDescriptor ? { statement_descriptor: catalog.statementDescriptor } : {}),
|
||||
metadata: { webklar: PRODUCT_METADATA[key] },
|
||||
})
|
||||
const price = await stripe.prices.create({
|
||||
product: product.id,
|
||||
currency: 'eur',
|
||||
unit_amount: config.stripe.priceAmountCents,
|
||||
unit_amount: catalog.amountCents,
|
||||
recurring: { interval: 'month' },
|
||||
tax_behavior: 'inclusive',
|
||||
lookup_key: config.stripe.priceLookupKey,
|
||||
nickname: 'Wartungs-Abo monatlich',
|
||||
lookup_key: catalog.lookupKey,
|
||||
nickname: catalog.priceNickname,
|
||||
})
|
||||
cachedPriceId = price.id
|
||||
return cachedPriceId
|
||||
priceIdCache.set(key, price.id)
|
||||
return price.id
|
||||
}
|
||||
|
||||
// Beide Abos zusammen im Kundenportal, nicht mehr nur die Wartung (Hosting-Abo dazu)
|
||||
export const PORTAL_HEADLINE = 'WEBklar – Abo verwalten'
|
||||
|
||||
let cachedPortalConfigId = null
|
||||
/** Kundenportal-Konfiguration (Zahlungsmittel, Rechnungen, Kuendigung zum Periodenende). */
|
||||
export async function ensurePortalConfiguration() {
|
||||
@@ -93,11 +108,20 @@ export async function ensurePortalConfiguration() {
|
||||
const own = data.find((c) => c.metadata?.webklar === 'wartungs-abo')
|
||||
if (own) {
|
||||
cachedPortalConfigId = own.id
|
||||
// Aeltere Konfiguration (frueher nur Wartung) traegt noch die alte Ueberschrift -> anpassen
|
||||
if (own.business_profile?.headline !== PORTAL_HEADLINE) {
|
||||
await stripe.billingPortal.configurations.update(own.id, {
|
||||
business_profile: {
|
||||
headline: PORTAL_HEADLINE,
|
||||
terms_of_service_url: `${config.publicBaseUrl}/abo-bedingungen.html`,
|
||||
},
|
||||
})
|
||||
}
|
||||
return own.id
|
||||
}
|
||||
const created = await stripe.billingPortal.configurations.create({
|
||||
business_profile: {
|
||||
headline: 'Webklar – Wartungs-Abo verwalten',
|
||||
headline: PORTAL_HEADLINE,
|
||||
terms_of_service_url: `${config.publicBaseUrl}/abo-bedingungen.html`,
|
||||
},
|
||||
default_return_url: `${config.publicBaseUrl}/dashboard.html`,
|
||||
@@ -197,6 +221,9 @@ export async function syncSubscription(subscriptionId) {
|
||||
stripeSubscriptionId: sub.id,
|
||||
status: sub.status,
|
||||
productKey: items.productKey || 'wartung',
|
||||
// Plan 2: Wechseltag (Beginn der neuen Mindestlaufzeit) und bezahlter Speicherstand
|
||||
termStartAt: termStartOf(sub).toISOString(),
|
||||
storageUnits: items.storageUnits,
|
||||
planKey: items.lookupKey || config.products.wartung.lookupKey,
|
||||
priceAmount: items.priceAmount ?? sub.items?.data?.[0]?.price?.unit_amount ?? config.products.wartung.amountCents,
|
||||
startedAt: toIso(sub.start_date),
|
||||
@@ -265,7 +292,14 @@ export function minTermMonthsFor(termsVersion) {
|
||||
return ['2026-09-25', '2026-09-25-v2'].includes(termsVersion) ? 0 : config.abo.minTermMonths
|
||||
}
|
||||
|
||||
export function serviceStartOf(sub) {
|
||||
/**
|
||||
* Beginn der aktuellen Mindestlaufzeit: der Wechseltag aus den Metadaten (nach einem Wechsel ins
|
||||
* Wartungs-Abo), sonst der Leistungsbeginn (Ende der Probezeit bzw. Abo-Start). Ersetzt
|
||||
* serviceStartOf in cancelSubscriptionAt.
|
||||
*/
|
||||
export function termStartOf(sub) {
|
||||
const termStartAt = sub.metadata?.termStartAt
|
||||
if (termStartAt) return new Date(termStartAt)
|
||||
return new Date(((sub.trial_end || sub.start_date) || 0) * 1000)
|
||||
}
|
||||
|
||||
@@ -371,18 +405,28 @@ async function getOrCreateStripeCustomer(customer) {
|
||||
return created.id
|
||||
}
|
||||
|
||||
/** Rabatt fuer Testkonten: 49,50 € auf jede Abrechnung -> es werden nur 0,50 € abgebucht. */
|
||||
async function ensureTestCoupon() {
|
||||
// Testrabatt-IDs je Produkt: amount_off = amountCents - 50 -> es werden nur 0,50 € abgebucht
|
||||
const TEST_COUPON_IDS = { wartung: config.abo.testCouponId, hosting: 'WEBKLAR-TEST-050-HOSTING' }
|
||||
|
||||
/** Rabatt fuer Testkonten holen oder (einmalig) anlegen. productKey: 'wartung' | 'hosting'. */
|
||||
export async function ensureTestCoupon(productKey) {
|
||||
const id = TEST_COUPON_IDS[productKey]
|
||||
if (!id) {
|
||||
const err = new Error(`Kein Testrabatt fuer ${productKey}`)
|
||||
err.status = 400
|
||||
throw err
|
||||
}
|
||||
const stripe = getStripe()
|
||||
const existing = await stripe.coupons.retrieve(config.abo.testCouponId).catch(() => null)
|
||||
const existing = await stripe.coupons.retrieve(id).catch(() => null)
|
||||
if (existing) return existing.id
|
||||
const catalog = getProduct(productKey)
|
||||
const created = await stripe.coupons.create({
|
||||
id: config.abo.testCouponId,
|
||||
name: 'Testzugang WEBklar (0,50 €)',
|
||||
amount_off: config.stripe.priceAmountCents - 50,
|
||||
id,
|
||||
name: `Testzugang WEBklar (0,50 €) – ${catalog.name}`,
|
||||
amount_off: catalog.amountCents - 50,
|
||||
currency: 'eur',
|
||||
duration: 'forever',
|
||||
metadata: { webklar: 'wartungs-abo-test' },
|
||||
metadata: { webklar: `${productKey}-test` },
|
||||
})
|
||||
return created.id
|
||||
}
|
||||
@@ -404,13 +448,13 @@ export async function createAboCheckout(customer, consent, offer = null) {
|
||||
}
|
||||
const stripe = getStripe()
|
||||
const [priceId, stripeCustomerId] = await Promise.all([
|
||||
ensureAboPrice(),
|
||||
ensurePrice('wartung'),
|
||||
getOrCreateStripeCustomer(customer),
|
||||
])
|
||||
const lateStart = consent.startChoice === 'nach_frist'
|
||||
const serviceStartAt = lateStart ? consent.serviceStartAt || lateStartAt() : null
|
||||
const isTestAccount = config.abo.testEmails.includes(String(customer.email || '').toLowerCase())
|
||||
const testCoupon = isTestAccount ? await ensureTestCoupon() : null
|
||||
const testCoupon = isTestAccount ? await ensureTestCoupon('wartung') : null
|
||||
// Stripe-Metadaten: max. 500 Zeichen je Wert
|
||||
const meta = {
|
||||
webklarCustomerId: customer.$id,
|
||||
@@ -504,7 +548,7 @@ export async function cancelSubscriptionAt(subscriptionId, requestedDate = null,
|
||||
const stripe = getStripe()
|
||||
const sub = await stripe.subscriptions.retrieve(subscriptionId)
|
||||
const { effectiveAt, minTermEnd } = computeCancellationDate({
|
||||
serviceStart: serviceStartOf(sub),
|
||||
serviceStart: termStartOf(sub),
|
||||
receivedAt,
|
||||
requestedDate,
|
||||
kind,
|
||||
|
||||
Reference in new issue
Block a user