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): allow abandoning checkouts before payment creation

Timothy Yindcc17b1e1e7b
diff --git a/apps/cloud/i18n/locales/en.json b/apps/cloud/i18n/locales/en.json
index 0ab54374..f7933af4 100644
--- a/apps/cloud/i18n/locales/en.json
+++ b/apps/cloud/i18n/locales/en.json
@@ -768,7 +768,7 @@
"cancelCheckout": "Cancel this payment",
"restartCheckout": "Start a new checkout",
"confirmRestartCheckout": "Cancel and start again",
- "cancelCheckoutHelp": "Cancel this unpaid checkout after verifying its current status. Existing entitlements and transaction history are preserved. Payments already processing or completed cannot be canceled here. Starting again creates a new checkout link.",
+ "cancelCheckoutHelp": "Cancel this unpaid checkout after verifying its current status. Existing entitlements and transaction history are preserved. A checkout still collecting details is only abandoned locally: its old link may remain usable until it expires. Do not pay through the old link. Payments already processing or completed cannot be canceled here. Starting again creates a new checkout link.",
"checkoutCannotCancel": "This payment cannot currently be canceled. It may have started processing or completed. Check the updated status before trying again.",
"checkoutCancelFailed": "Could not confirm cancellation. No new checkout was created. Please retry or contact support.",
"cancelCheckoutSummary": "Verify and cancel this unpaid checkout."
diff --git a/apps/cloud/i18n/locales/zh-Hans.json b/apps/cloud/i18n/locales/zh-Hans.json
index c9d2ca25..641681a4 100644
--- a/apps/cloud/i18n/locales/zh-Hans.json
+++ b/apps/cloud/i18n/locales/zh-Hans.json
@@ -768,7 +768,7 @@
"cancelCheckout": "取消本次支付",
"restartCheckout": "重新发起支付",
"confirmRestartCheckout": "取消并重新发起",
- "cancelCheckoutHelp": "核验当前状态后取消这次尚未付款的结账,保留已有权益及交易历史。已开始处理或已完成的付款无法在此取消。重新发起会创建新的结账链接。",
+ "cancelCheckoutHelp": "核验当前状态后取消这次尚未付款的结账,保留已有权益及交易历史。尚在填写资料的结账仅在本平台放弃,旧链接在过期前可能仍可用,请勿再通过旧链接付款。已开始处理或已完成的付款无法在此取消。重新发起会创建新的结账链接。",
"checkoutCannotCancel": "当前无法取消这笔付款,可能已开始处理或已完成。请先查看更新后的状态。",
"checkoutCancelFailed": "暂未确认取消成功,未创建新的结账。请重试或联系客服。",
"cancelCheckoutSummary": "核验并取消这次尚未付款的结账。"
diff --git a/packages/platform/src/dodo-payments.ts b/packages/platform/src/dodo-payments.ts
index 046bad0c..e3614dff 100644
--- a/packages/platform/src/dodo-payments.ts
+++ b/packages/platform/src/dodo-payments.ts
@@ -1342,6 +1342,25 @@ export async function refreshDodoCheckout(
key,
cancelInitial,
)
+ } else if (cancelInitial && session.payment_status == null) {
+ // Dodo has no session revocation endpoint. Abandon locally only after a fresh
+ // details-stage response; retain the provider identity for late payment events.
+ const closed = await db
+ .prepare(
+ `UPDATE platform_payment_checkouts SET state='expired',error='payment_checkout_abandoned',checkout_url=NULL,updated_at=? WHERE id=? AND state IN('open','uncertain') AND NOT EXISTS(SELECT 1 FROM platform_payment_subscriptions WHERE checkout_id=platform_payment_checkouts.id) AND NOT EXISTS(SELECT 1 FROM platform_payment_locks WHERE workspace_id=platform_payment_checkouts.workspace_id AND expires_at>?) AND ${activeFence}`,
+ )
+ .bind(now, row.id, now, key, revision)
+ .run()
+ if (!closed.meta.changes) {
+ // The handover reconciliation above may already have confirmed cancellation.
+ const canceled = await db
+ .prepare(
+ "SELECT 1 FROM platform_payment_subscriptions WHERE checkout_id=? AND status IN('canceled','expired','failed')",
+ )
+ .bind(row.id)
+ .first()
+ if (!canceled) fail('payment_cancellation_unavailable')
+ }
} else if (now - row.created_at > 86400000)
await db
.prepare(
diff --git a/packages/platform/src/payments.ts b/packages/platform/src/payments.ts
index e32205fe..243bb267 100644
--- a/packages/platform/src/payments.ts
+++ b/packages/platform/src/payments.ts
@@ -249,7 +249,7 @@ export async function paymentStatus(
settings.purchaseMethod === 'payment'
? await db
.prepare(
- "SELECT id,state,plan_id AS planId,interval,error,CASE WHEN user_id=? AND ?='admin' THEN 1 ELSE 0 END AS canRefresh,CASE WHEN error='payment_awaiting_payment' AND user_id=? AND ?='admin' THEN 1 ELSE 0 END AS canCancel,CASE WHEN state='open' AND (error IS NULL OR error<>'payment_awaiting_payment') AND user_id=? AND ?='admin' THEN checkout_url ELSE NULL END AS url FROM platform_payment_checkouts WHERE workspace_id=? AND provider='dodo' AND environment=? AND provider_key=? AND state IN('creating','open','uncertain') ORDER BY created_at DESC LIMIT 1",
+ "SELECT id,state,plan_id AS planId,interval,error,CASE WHEN user_id=? AND ?='admin' THEN 1 ELSE 0 END AS canRefresh,CASE WHEN provider_id IS NOT NULL AND state IN('open','uncertain') AND NOT EXISTS(SELECT 1 FROM platform_payment_subscriptions WHERE checkout_id=platform_payment_checkouts.id) AND user_id=? AND ?='admin' THEN 1 ELSE 0 END AS canCancel,CASE WHEN state='open' AND (error IS NULL OR error<>'payment_awaiting_payment') AND user_id=? AND ?='admin' THEN checkout_url ELSE NULL END AS url FROM platform_payment_checkouts WHERE workspace_id=? AND provider='dodo' AND environment=? AND provider_key=? AND state IN('creating','open','uncertain') ORDER BY created_at DESC LIMIT 1",
)
.bind(
userId,
diff --git a/tests/dodo-payments.test.ts b/tests/dodo-payments.test.ts
index 91998726..57321001 100644
--- a/tests/dodo-payments.test.ts
+++ b/tests/dodo-payments.test.ts
@@ -3343,7 +3343,7 @@ test('manual cancellation never cancels processing or successful payments and re
await f.mf.dispose()
}
})
-test('manual cancellation cannot locally discard a still-valid session with no bound payment', async () => {
+test('details-stage cancellation releases the local slot and preserves late payment recovery', async () => {
const f = await fixture()
try {
await syncDodoProducts(f.db, root, f.transport)
@@ -3358,19 +3358,40 @@ test('manual cancellation cannot locally discard a still-valid session with no b
)
const { cancelDodoCheckout } =
await import('../packages/platform/src/dodo-payments.ts')
- await assert.rejects(
- cancelDodoCheckout(
- f.db,
- root,
- 1,
- 1,
- checkout.id,
- f.transport,
- f.now + 1000,
- ),
- { code: 'payment_cancellation_unavailable' },
+ assert.equal((await paymentStatus(f.db, 1, 1)).pending.canCancel, 1)
+ await cancelDodoCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ checkout.id,
+ f.transport,
+ f.now + 1000,
)
- assert.ok((await paymentStatus(f.db, 1, 1)).pending)
+ assert.equal((await paymentStatus(f.db, 1, 1)).pending, null)
+ const abandoned = await f.db
+ .prepare('SELECT * FROM platform_payment_checkouts WHERE id=?')
+ .bind(checkout.id)
+ .first<any>()
+ assert.equal(abandoned.state, 'expired')
+ assert.equal(abandoned.error, 'payment_checkout_abandoned')
+ assert.equal(abandoned.checkout_url, null)
+ assert.ok(abandoned.provider_id)
+ // Retain reconciliation for a real payment through an old browser tab.
+ await f.paid(checkout)
+ await refreshDodoCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ checkout.id,
+ f.transport,
+ f.now + 2000,
+ )
+ const grant = await f.db
+ .prepare('SELECT * FROM platform_workspace_plans WHERE workspace_id=1')
+ .first<any>()
+ assert.equal(grant.source, 'payment')
} finally {
await f.mf.dispose()
}
@@ -3997,3 +4018,78 @@ for (const providerStatus of ['cancelled', 'expired'] as const) {
}
})
}
+
+test('details-stage abandonment requires a fresh empty provider session and no reconciliation lock', async () => {
+ const f = await fixture()
+ try {
+ await syncDodoProducts(f.db, root, f.transport)
+ const checkout = await createPaymentCheckout(
+ f.db,
+ root,
+ 1,
+ input(),
+ origin,
+ f.transport,
+ f.now,
+ )
+ const { cancelDodoCheckout } =
+ await import('../packages/platform/src/dodo-payments.ts')
+ const session = f.sessions.get(checkout.url.split('/').pop()!)!
+ session.payment_status = 'processing'
+ await assert.rejects(
+ cancelDodoCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ checkout.id,
+ f.transport,
+ f.now + 1000,
+ ),
+ { code: 'payment_cancellation_unavailable' },
+ )
+ session.payment_status = null
+ const unavailable: typeof fetch = async () =>
+ Response.json({}, { status: 503 })
+ await assert.rejects(
+ cancelDodoCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ checkout.id,
+ unavailable,
+ f.now + 1000,
+ ),
+ )
+ await f.db
+ .prepare(
+ 'INSERT INTO platform_payment_locks(workspace_id,token,expires_at) VALUES(1,?,?)',
+ )
+ .bind('in-flight', f.now + 60000)
+ .run()
+ await assert.rejects(
+ cancelDodoCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ checkout.id,
+ f.transport,
+ f.now + 1000,
+ ),
+ { code: 'payment_cancellation_unavailable' },
+ )
+ const row = await f.db
+ .prepare(
+ 'SELECT state,checkout_url FROM platform_payment_checkouts WHERE id=?',
+ )
+ .bind(checkout.id)
+ .first<any>()
+ assert.equal(row.state, 'open')
+ assert.equal(row.checkout_url, checkout.url)
+ assert.ok((await paymentStatus(f.db, 1, 1)).pending)
+ } finally {
+ await f.mf.dispose()
+ }
+})