Abo-Spiegel: Produkt und tatsächlicher Preis aus den Stripe-Positionen
Neues Appwrite-Feld subscriptions.productKey (Setup-Skript). Ohne bekannten Abo-Preis gilt ein Abo weiter als Wartungs-Abo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
54defb75c4
commit
d097704d49
4 files changed
+88
-2
No files matched your search
@@ -117,6 +117,8 @@ await ensureCollection(
|
|||||||
// Individuelles Angebot, mit dem das Abo abgeschlossen wurde (Nachweis der Preisabrede)
|
// Individuelles Angebot, mit dem das Abo abgeschlossen wurde (Nachweis der Preisabrede)
|
||||||
{ type: 'string', key: 'offerId', size: 64 },
|
{ type: 'string', key: 'offerId', size: 64 },
|
||||||
{ type: 'string', key: 'offerText', size: 300 },
|
{ type: 'string', key: 'offerText', size: 300 },
|
||||||
|
// Plan 1: Produkt des Abos (wartung | hosting)
|
||||||
|
{ type: 'string', key: 'productKey', size: 16 },
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
{ key: 'idx_customer', type: 'key', attributes: ['customerId'] },
|
{ key: 'idx_customer', type: 'key', attributes: ['customerId'] },
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
updateDocument,
|
updateDocument,
|
||||||
} from './appwriteAdmin.js'
|
} from './appwriteAdmin.js'
|
||||||
import { lateStartAt } from './deadlines.js'
|
import { lateStartAt } from './deadlines.js'
|
||||||
|
import { describeItems } from './products.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wartungs-Abo ueber Stripe Billing.
|
* Wartungs-Abo ueber Stripe Billing.
|
||||||
@@ -160,14 +161,21 @@ export async function syncSubscription(subscriptionId) {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const customer = typeof sub.customer === 'string' ? null : sub.customer
|
const customer = typeof sub.customer === 'string' ? null : sub.customer
|
||||||
|
// Abo-Posten (Hosting/Wartung) unabhängig von der Reihenfolge; ohne bekannten Preis gilt das Abo
|
||||||
|
// als Wartungs-Abo (Altbestand bzw. von Hand angelegt)
|
||||||
|
const items = describeItems(sub.items?.data)
|
||||||
|
if (!items.productKey) {
|
||||||
|
console.warn(`[stripe] Abo ${sub.id}: kein bekannter Abo-Preis – als Wartungs-Abo gespiegelt`)
|
||||||
|
}
|
||||||
const doc = await upsertSubscriptionDoc(sub.id, {
|
const doc = await upsertSubscriptionDoc(sub.id, {
|
||||||
customerId,
|
customerId,
|
||||||
email: meta.email || customer?.email || '',
|
email: meta.email || customer?.email || '',
|
||||||
stripeCustomerId: typeof sub.customer === 'string' ? sub.customer : sub.customer?.id,
|
stripeCustomerId: typeof sub.customer === 'string' ? sub.customer : sub.customer?.id,
|
||||||
stripeSubscriptionId: sub.id,
|
stripeSubscriptionId: sub.id,
|
||||||
status: sub.status,
|
status: sub.status,
|
||||||
planKey: config.stripe.priceLookupKey,
|
productKey: items.productKey || 'wartung',
|
||||||
priceAmount: sub.items?.data?.[0]?.price?.unit_amount ?? config.stripe.priceAmountCents,
|
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),
|
startedAt: toIso(sub.start_date),
|
||||||
currentPeriodEnd: toIso(periodEndOf(sub)),
|
currentPeriodEnd: toIso(periodEndOf(sub)),
|
||||||
cancelAtPeriodEnd: Boolean(sub.cancel_at_period_end || sub.cancel_at),
|
cancelAtPeriodEnd: Boolean(sub.cancel_at_period_end || sub.cancel_at),
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { test, after } from 'node:test'
|
||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import { startAppwriteMock } from './support/appwriteMock.js'
|
||||||
|
import { startFakeStripe } from './support/fakeStripe.js'
|
||||||
|
import { useTestEnv } from './support/testEnv.js'
|
||||||
|
import { schemaKeys } from './support/schema.js'
|
||||||
|
|
||||||
|
const aw = await startAppwriteMock({ schema: { subscriptions: schemaKeys('subscriptions') } })
|
||||||
|
const st = await startFakeStripe()
|
||||||
|
after(() => Promise.all([aw.close(), st.close()]))
|
||||||
|
useTestEnv({ appwriteUrl: aw.url, stripeUrl: st.url })
|
||||||
|
const { syncSubscription } = await import('../server/services/stripeBilling.js')
|
||||||
|
|
||||||
|
const sub = (id, items, extra = {}) => ({
|
||||||
|
id,
|
||||||
|
object: 'subscription',
|
||||||
|
status: 'active',
|
||||||
|
customer: 'cus_1',
|
||||||
|
start_date: 1790000000,
|
||||||
|
current_period_end: 1792600000,
|
||||||
|
trial_end: null,
|
||||||
|
cancel_at_period_end: false,
|
||||||
|
cancel_at: null,
|
||||||
|
canceled_at: null,
|
||||||
|
ended_at: null,
|
||||||
|
metadata: { webklarCustomerId: 'c1', email: 'k@example.invalid', termsVersion: '2026-09-25-v3' },
|
||||||
|
items: { object: 'list', data: items },
|
||||||
|
...extra,
|
||||||
|
})
|
||||||
|
const item = (id, lookup_key, unit_amount, quantity = 1) => ({
|
||||||
|
id: `si_${id}`,
|
||||||
|
object: 'subscription_item',
|
||||||
|
quantity,
|
||||||
|
price: { id: `price_${id}`, object: 'price', lookup_key, unit_amount },
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Setup-Skript legt productKey an', () => {
|
||||||
|
assert.ok(schemaKeys('subscriptions').includes('productKey'))
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Spiegel: Hosting-Abo, Zusatzspeicher steht vorne', async () => {
|
||||||
|
st.subscriptions.set(
|
||||||
|
'sub_h',
|
||||||
|
sub('sub_h', [item('s', 'webklar_mailspeicher_5gb_monatlich_100', 100, 2), item('h', 'webklar_hosting_monatlich_1500', 1500)])
|
||||||
|
)
|
||||||
|
const doc = await syncSubscription('sub_h')
|
||||||
|
assert.equal(doc.productKey, 'hosting')
|
||||||
|
assert.equal(doc.planKey, 'webklar_hosting_monatlich_1500')
|
||||||
|
assert.equal(doc.priceAmount, 1500)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Spiegel: Wartungs-Abo wie bisher', async () => {
|
||||||
|
st.subscriptions.set('sub_w', sub('sub_w', [item('w', 'webklar_wartung_monatlich_5000', 5000)]))
|
||||||
|
const doc = await syncSubscription('sub_w')
|
||||||
|
assert.deepEqual([doc.productKey, doc.planKey, doc.priceAmount], ['wartung', 'webklar_wartung_monatlich_5000', 5000])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Spiegel: unbekannter Preis gilt als Wartungs-Abo, Betrag vom ersten Posten', async () => {
|
||||||
|
st.subscriptions.set('sub_x', sub('sub_x', [item('x', null, 4900)]))
|
||||||
|
const doc = await syncSubscription('sub_x')
|
||||||
|
assert.deepEqual([doc.productKey, doc.planKey, doc.priceAmount], ['wartung', 'webklar_wartung_monatlich_5000', 4900])
|
||||||
|
})
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { readFileSync } from 'node:fs'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attribut-Schlüssel einer Collection, wie sie scripts/setup-abo-collections.mjs in Appwrite
|
||||||
|
* anlegt. Tests geben sie dem Appwrite-Nachbau als Schema: Schreibt der Code ein Feld, das das
|
||||||
|
* Setup-Skript nicht anlegt, scheitert der Test wie Appwrite in Produktion.
|
||||||
|
*/
|
||||||
|
export function schemaKeys(collection) {
|
||||||
|
const src = readFileSync(new URL('../../scripts/setup-abo-collections.mjs', import.meta.url), 'utf8')
|
||||||
|
const start = src.indexOf(`'${collection}',`)
|
||||||
|
if (start < 0) throw new Error(`Collection ${collection} fehlt im Setup-Skript`)
|
||||||
|
const block = src.slice(start, src.indexOf('\n ],', start))
|
||||||
|
return [...block.matchAll(/key: '(\w+)'/g)].map((m) => m[1])
|
||||||
|
}
|
||||||
Reference in new issue
Block a user