Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(billing): recover payment views and isolate scheduled maintenance failures

Timothy Yin0ab4ba334c71
diff --git a/apps/cloud/app/components/CloudPricing.vue b/apps/cloud/app/components/CloudPricing.vue
index f50f3557..2f0fe84b 100644
--- a/apps/cloud/app/components/CloudPricing.vue
+++ b/apps/cloud/app/components/CloudPricing.vue
@@ -230,7 +230,13 @@ async function checkout(plan: any) {
}}
</p>
<CloudButton
- v-if="portal && !payment"
+ v-if="portal && !payment && payments?.error.value"
+ variant="secondary"
+ @click="payments?.refresh()"
+ >{{ $t('cloud.messages.retry') }}</CloudButton
+ >
+ <CloudButton
+ v-else-if="portal && !payment"
disabled
variant="secondary"
>{{ $t('cloud.billing.loading') }}</CloudButton
diff --git a/apps/cloud/app/composables/useWorkspaceBilling.ts b/apps/cloud/app/composables/useWorkspaceBilling.ts
index bd89db77..a92274b4 100644
--- a/apps/cloud/app/composables/useWorkspaceBilling.ts
+++ b/apps/cloud/app/composables/useWorkspaceBilling.ts
@@ -8,7 +8,10 @@ export function useWorkspaceBilling(adminOnly = false) {
const list = await fetcher<{ workspaces: Workspace[] }>(
'/api/billing/workspaces',
)
- const primary = list.workspaces.find((w) => w.owned) ?? list.workspaces[0]
+ const primary =
+ list.workspaces.find((w) => w.owned) ??
+ list.workspaces.find((w) => w.role === 'admin') ??
+ list.workspaces[0]
const id = primary?.id ?? 0
const plan = id
? await fetcher<any>('/api/billing/plan', {
diff --git a/apps/cloud/server/plugins/payments.ts b/apps/cloud/server/plugins/payments.ts
index 5c4f0455..1b9d57b2 100644
--- a/apps/cloud/server/plugins/payments.ts
+++ b/apps/cloud/server/plugins/payments.ts
@@ -9,8 +9,10 @@ export default defineNitroPlugin((app) => {
await syncPaymentProducts(
db,
String(env.NUXT_PLATFORM_STORAGE_SECRET || ''),
- ).catch(() => undefined)
- await expirePaymentEventPayloads(db)
+ ).catch(() => console.error('payment_product_sync_failed'))
+ await expirePaymentEventPayloads(db).catch(() =>
+ console.error('payment_payload_expiry_failed'),
+ )
await reconcilePayments(
db,
String(env.NUXT_PLATFORM_STORAGE_SECRET || ''),
diff --git a/packages/platform/src/payments.ts b/packages/platform/src/payments.ts
index 94a303f1..dd43beb7 100644
--- a/packages/platform/src/payments.ts
+++ b/packages/platform/src/payments.ts
@@ -748,12 +748,14 @@ async function synchronizePayment(
.first<any>()
subscriptionId = event.object_id
}
- if (
- !checkout ||
- !checkout.user_id ||
- checkout.error === 'payment_test_abandoned'
- )
+ if (!checkout || !checkout.user_id) {
+ await db
+ .prepare('DELETE FROM platform_payment_event_payloads WHERE event_id=?')
+ .bind(event.id)
+ .run()
return 'ignored'
+ }
+ if (checkout.error === 'payment_test_abandoned') return 'ignored'
if (!identifier(subscriptionId)) fail(409, 'payment_confirmation_pending')
const token = await acquireWorkspace(db, checkout.workspace_id, now)
try {
diff --git a/tests/platform-payments.test.ts b/tests/platform-payments.test.ts
index 070e47c3..2622b49e 100644
--- a/tests/platform-payments.test.ts
+++ b/tests/platform-payments.test.ts
@@ -968,6 +968,24 @@ test('Owned account erasure removes dependent payment rows only after cancellati
).n,
0,
)
+ await (
+ await f.event(
+ 'evt_after_erasure',
+ 'checkout.completed',
+ f.checkouts.get('chk_1'),
+ )
+ ).receive()
+ await processPaymentEvents(f.db, root, f.transport, f.now + 16 * 86400000)
+ assert.equal(
+ (
+ await f.db
+ .prepare(
+ "SELECT COUNT(*) n FROM platform_payment_event_payloads WHERE event_id='evt_after_erasure'",
+ )
+ .first<any>()
+ ).n,
+ 0,
+ )
} finally {
await f.mf.dispose()
}
@@ -1275,6 +1293,26 @@ test('Abandoned sandbox attempts cannot grant access through late callbacks or b
).n,
0,
)
+ assert.equal(
+ (await paymentEventPayload(f.db, root, 'evt_abandoned', f.now)).available,
+ true,
+ )
+ await f.db
+ .prepare('UPDATE platform_payment_checkouts SET user_id=NULL')
+ .run()
+ await (
+ await f.event(
+ 'evt_abandoned_no_payer',
+ 'checkout.completed',
+ f.checkouts.get('chk_1'),
+ )
+ ).receive()
+ await processPaymentEvents(f.db, root, f.transport, f.now)
+ assert.equal(
+ (await paymentEventPayload(f.db, root, 'evt_abandoned_no_payer', f.now))
+ .available,
+ false,
+ )
f.setUncertain(false)
const retry = await createPaymentCheckout(
f.db,
diff --git a/tests/portal-review-regressions.test.ts b/tests/portal-review-regressions.test.ts
index 99c63d1b..aa2b2c81 100644
--- a/tests/portal-review-regressions.test.ts
+++ b/tests/portal-review-regressions.test.ts
@@ -314,3 +314,84 @@ test('Overview data stays unknown before the first response and retains counts d
assert.equal(media.photos.value.length, 2)
assert.equal(media.albums.value.length, 1)
})
+
+test('Payment maintenance failures do not block authoritative reconciliation or expose raw errors', async () => {
+ let scheduled: any
+ const calls: string[] = [],
+ logs: string[] = []
+ const plugin = load(
+ 'apps/cloud/server/plugins/payments.ts',
+ {
+ defineNitroPlugin: (value: any) => value,
+ console: { error: (code: string) => logs.push(code) },
+ },
+ {
+ '../../../../packages/platform/src/payment-product-sync': {
+ syncPaymentProducts: async () => {
+ calls.push('sync')
+ throw new Error('sensitive fixture')
+ },
+ },
+ '../../../../packages/platform/src/payment-events': {
+ expirePaymentEventPayloads: async () => {
+ calls.push('expiry')
+ throw new Error('sensitive fixture')
+ },
+ },
+ '../../../../packages/platform/src/payments': {
+ reconcilePayments: async () => {
+ calls.push('reconcile')
+ },
+ },
+ },
+ )
+ plugin.default({
+ hooks: {
+ hook: (_name: string, callback: any) => {
+ scheduled = callback
+ },
+ },
+ })
+ await scheduled({ env: { DB: {}, NUXT_PLATFORM_STORAGE_SECRET: 'fixture' } })
+ assert.deepEqual(calls, ['sync', 'expiry', 'reconcile'])
+ assert.deepEqual(logs, [
+ 'payment_product_sync_failed',
+ 'payment_payload_expiry_failed',
+ ])
+})
+
+test('Primary billing workspace consistently prefers ownership then administrative membership', async () => {
+ let selected = 0
+ const list = [
+ { id: 1, name: 'Read only', role: 'member', owned: 0 },
+ { id: 2, name: 'Managed', role: 'admin', owned: 0 },
+ ]
+ const module = load('apps/cloud/app/composables/useWorkspaceBilling.ts', {
+ usePortalIdentity: () => ref({ id: 3 }),
+ useRequestFetch: () => async (url: string, options: any) => {
+ if (url.endsWith('/workspaces')) return { workspaces: list }
+ selected = options.query.workspaceId
+ return { workspace: { role: list.find((w) => w.id === selected)?.role } }
+ },
+ useAsyncData: (_key: string, run: any) => {
+ const data = ref<any>(),
+ ready = run().then((value: any) => {
+ data.value = value
+ })
+ return Object.assign(ready, {
+ data,
+ error: ref(null),
+ status: ref('success'),
+ })
+ },
+ onMounted: () => {},
+ })
+ let state = module.useWorkspaceBilling(true)
+ await state.ready
+ assert.equal(selected, 2)
+ assert.equal(state.selected.value, 2)
+ list[0].owned = 1
+ state = module.useWorkspaceBilling()
+ await state.ready
+ assert.equal(selected, 1)
+})