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): link retained callbacks and release expired checkouts

Timothy Yin0615d8316f13
diff --git a/packages/platform/src/payments.ts b/packages/platform/src/payments.ts
index d8fa2e66..95a8100e 100644
--- a/packages/platform/src/payments.ts
+++ b/packages/platform/src/payments.ts
@@ -668,7 +668,26 @@ export async function receivePaymentEvent(
)
const ref =
event.object.request_id || event.object.metadata?.chronoframe_checkout_id
- const payload = await sealPaymentEventPayload(rootKey, event.id, body)
+ let reference = identifier(ref) ? ref : null
+ let retainPayload = true
+ if (!supported) {
+ const sub = objectId(event.object.subscription)
+ const association = await db
+ .prepare(
+ 'SELECT c.id FROM platform_payment_checkouts c LEFT JOIN platform_payment_subscriptions s ON s.checkout_id=c.id WHERE c.user_id IS NOT NULL AND (c.id=? OR c.provider_id=? OR s.id=?) LIMIT 1',
+ )
+ .bind(
+ reference ?? '',
+ event.object.id,
+ identifier(sub) ? sub : event.object.id,
+ )
+ .first<{ id: string }>()
+ reference = association?.id ?? null
+ retainPayload = !!association
+ }
+ const payload = retainPayload
+ ? await sealPaymentEventPayload(rootKey, event.id, body)
+ : null
await db.batch([
db
.prepare(
@@ -678,16 +697,20 @@ export async function receivePaymentEvent(
event.id,
type,
event.object.id,
- identifier(ref) ? ref : null,
+ reference,
event.created_at,
supported ? 'pending' : 'ignored',
now,
),
- db
- .prepare(
- 'INSERT OR IGNORE INTO platform_payment_event_payloads(event_id,ciphertext,received_at) SELECT ?,?,? WHERE changes()=1',
- )
- .bind(event.id, payload, now),
+ ...(payload
+ ? [
+ db
+ .prepare(
+ 'INSERT OR IGNORE INTO platform_payment_event_payloads(event_id,ciphertext,received_at) SELECT ?,?,? WHERE changes()=1 AND (?=1 OR EXISTS(SELECT 1 FROM platform_payment_checkouts WHERE id=? AND user_id IS NOT NULL))',
+ )
+ .bind(event.id, payload, now, supported ? 1 : 0, reference),
+ ]
+ : []),
])
return { received: true }
}
@@ -1378,6 +1401,23 @@ export async function refreshPaymentCheckout(
objectId(checkout.product) !== row.product_id
)
fail(422, 'payment_binding_invalid')
+ if (checkout.status === 'expired') {
+ await db
+ .prepare(
+ "UPDATE platform_payment_checkouts SET state='expired',updated_at=?,error=NULL WHERE id=? AND state IN('open','uncertain')",
+ )
+ .bind(now, row.id)
+ .run()
+ const current = await db
+ .prepare('SELECT state FROM platform_payment_checkouts WHERE id=?')
+ .bind(row.id)
+ .first<any>()
+ return {
+ state: ['expired', 'completed'].includes(current?.state)
+ ? current.state
+ : 'pending',
+ }
+ }
if (checkout.status !== 'completed') return { state: 'pending' }
const eventId = 'return_' + row.id
await db
diff --git a/tests/platform-payments.test.ts b/tests/platform-payments.test.ts
index a3dc9bf2..44a0723b 100644
--- a/tests/platform-payments.test.ts
+++ b/tests/platform-payments.test.ts
@@ -848,6 +848,22 @@ test('Paid checkout snapshots survive price edits; actual purchasers qualify wit
).receive()
await processPaymentEvents(f.db, root, f.transport, f.now)
assert.equal((await resolveSubscription(f.db, 1, f.now)).id, 'pro')
+ const localCheckout = await f.db
+ .prepare('SELECT id FROM platform_payment_checkouts')
+ .first<any>()
+ await (
+ await f.event('evt_refund_associated', 'refund.created', {
+ id: 'refund_associated',
+ mode: 'test',
+ metadata: { chronoframe_checkout_id: localCheckout.id },
+ customer: { email: '[email protected]' },
+ })
+ ).receive()
+ assert.equal(
+ (await paymentEventPayload(f.db, root, 'evt_refund_associated', f.now))
+ .available,
+ true,
+ )
await f.db
.prepare(
"INSERT INTO platform_users(id,email,name,email_verified,created_at,updated_at) VALUES(4,'[email protected]','New owner',1,?,?)",
@@ -933,6 +949,29 @@ test('Paid checkout snapshots survive price edits; actual purchasers qualify wit
.available,
false,
)
+ assert.equal(
+ (await paymentEventPayload(f.db, root, 'evt_refund_associated', f.now))
+ .available,
+ false,
+ )
+ await (
+ await f.event('evt_late_refund_associated', 'refund.created', {
+ id: 'refund_late',
+ mode: 'test',
+ metadata: { chronoframe_checkout_id: localCheckout.id },
+ })
+ ).receive()
+ assert.equal(
+ (
+ await paymentEventPayload(
+ f.db,
+ root,
+ 'evt_late_refund_associated',
+ f.now,
+ )
+ ).available,
+ false,
+ )
assert.equal((await resolveSubscription(f.db, 1, f.now)).id, 'free')
} finally {
await f.mf.dispose()
@@ -1454,6 +1493,15 @@ test('Checkout return reconciliation verifies provider truth and ownership witho
test('Signed webhook payloads are encrypted, bound to their event, paginated and expired without losing the inbox', async () => {
const f = await fixture()
try {
+ const association = await createPaymentCheckout(
+ f.db,
+ root,
+ 1,
+ f.input,
+ 'https://app.chronoframe.art',
+ f.transport,
+ f.now,
+ )
for (let i = 0; i < 56; i++)
await (
await f.event(
@@ -1462,6 +1510,7 @@ test('Signed webhook payloads are encrypted, bound to their event, paginated and
{
id: 'refund_' + i,
mode: 'test',
+ metadata: { chronoframe_checkout_id: association.checkoutId },
customer: { email: '[email protected]' },
note: i === 0 ? 'x'.repeat(50000) : 'original',
},
@@ -1554,6 +1603,28 @@ test('Signed webhook payloads are encrypted, bound to their event, paginated and
).n,
56,
)
+ await (
+ await f.event('evt_unassociated_refund', 'refund.created', {
+ id: 'refund_unknown',
+ mode: 'test',
+ customer: { email: '[email protected]' },
+ })
+ ).receive()
+ assert.equal(
+ (await paymentEventPayload(f.db, root, 'evt_unassociated_refund', f.now))
+ .available,
+ false,
+ )
+ assert.equal(
+ (
+ await f.db
+ .prepare(
+ "SELECT state FROM platform_payment_events WHERE id='evt_unassociated_refund'",
+ )
+ .first<any>()
+ ).state,
+ 'ignored',
+ )
} finally {
await f.mf.dispose()
}
@@ -1795,3 +1866,52 @@ test('Product creation retries are idempotent and a concurrent plan edit cannot
}
}
})
+
+test('An authoritative expired checkout return releases the pending checkout without waiting for cron', async () => {
+ const f = await fixture()
+ try {
+ const first = await createPaymentCheckout(
+ f.db,
+ root,
+ 1,
+ f.input,
+ 'https://app.chronoframe.art',
+ f.transport,
+ f.now,
+ )
+ f.checkouts.get('chk_1').status = 'expired'
+ assert.deepEqual(
+ await refreshPaymentCheckout(
+ f.db,
+ root,
+ 1,
+ 1,
+ first.checkoutId,
+ f.transport,
+ f.now + 1,
+ ),
+ { state: 'expired' },
+ )
+ assert.equal(
+ (
+ await f.db
+ .prepare('SELECT state FROM platform_payment_checkouts WHERE id=?')
+ .bind(first.checkoutId)
+ .first<any>()
+ ).state,
+ 'expired',
+ )
+ const next = await createPaymentCheckout(
+ f.db,
+ root,
+ 1,
+ { ...f.input, requestId: 'after_expiry' },
+ 'https://app.chronoframe.art',
+ f.transport,
+ f.now + 2,
+ )
+ assert.ok(next.url)
+ } finally {
+ await f.mf.dispose()
+ }
+})