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): refresh entitlements when reusing an older product

Timothy Yin52a67aeab952
diff --git a/packages/platform/src/payment-product-sync.ts b/packages/platform/src/payment-product-sync.ts
index b844f481..fbc57ed8 100644
--- a/packages/platform/src/payment-product-sync.ts
+++ b/packages/platform/src/payment-product-sync.ts
@@ -251,7 +251,7 @@ export async function syncPaymentProducts(
throw new SubscriptionError(502, 'payment_product_response_invalid')
productId = created.id
}
- const actual = await creemRequest(
+ let actual = await creemRequest(
credentials,
'/v1/products?product_id=' + encodeURIComponent(productId!),
undefined,
@@ -269,6 +269,39 @@ export async function syncPaymentProducts(
actual.usage_prices?.length
)
throw new SubscriptionError(422, 'payment_product_mismatch')
+ // An idempotent create can return a product whose description was edited later.
+ if (
+ actual.name !== body.name ||
+ actual.description !== body.description
+ ) {
+ await creemRequest(
+ credentials,
+ '/v1/products/' + encodeURIComponent(productId!),
+ { name: body.name, description: body.description },
+ transport,
+ { method: 'PATCH' },
+ )
+ actual = await creemRequest(
+ credentials,
+ '/v1/products?product_id=' + encodeURIComponent(productId!),
+ undefined,
+ transport,
+ )
+ if (
+ actual.id !== productId ||
+ actual.mode !== 'test' ||
+ actual.price !== body.price ||
+ actual.currency !== body.currency ||
+ actual.billing_type !== 'recurring' ||
+ actual.billing_period !== body.billing_period ||
+ (actual.status && actual.status !== 'active') ||
+ Number(actual.trial_period_days ?? 0) > 0 ||
+ actual.usage_prices?.length ||
+ actual.name !== body.name ||
+ actual.description !== body.description
+ )
+ throw new SubscriptionError(422, 'payment_product_mismatch')
+ }
}
if (!productId)
throw new SubscriptionError(502, 'payment_product_response_invalid')
diff --git a/tests/platform-payments.test.ts b/tests/platform-payments.test.ts
index 4d5d572e..070e47c3 100644
--- a/tests/platform-payments.test.ts
+++ b/tests/platform-payments.test.ts
@@ -1565,6 +1565,9 @@ test('Automatic catalog sync publishes month/quarter products, updates rights an
)
.first<any>()
const original = p.products.get(mapping.product_id)
+ const originalPlan = await f.db
+ .prepare("SELECT * FROM platform_plans WHERE id='pro'")
+ .first<any>()
assert.ok(original.description.includes('Platform storage'))
assert.equal(original.billing_period, 'every-month')
const checkout = await createPaymentCheckout(
@@ -1656,6 +1659,25 @@ test('Automatic catalog sync publishes month/quarter products, updates rights an
const calls = p.calls.length
await syncPaymentProducts(f.db, root, p.transport, f.now + 4, 20)
assert.equal(p.calls.length, calls)
+ // Returning to an older quote may reuse a previously edited idempotent product.
+ await f.db
+ .prepare(
+ "UPDATE platform_plans SET name=?,policy_json=?,monthly_price=1200,revision=revision+1 WHERE id='pro'",
+ )
+ .bind(originalPlan.name, originalPlan.policy_json)
+ .run()
+ await syncPaymentProducts(f.db, root, p.transport, f.now + 5, 20)
+ const reverted = await f.db
+ .prepare(
+ "SELECT product_id FROM platform_payment_products WHERE plan_id='pro' AND interval=1",
+ )
+ .first<any>()
+ assert.equal(reverted.product_id, mapping.product_id)
+ assert.equal(
+ p.products.get(reverted.product_id).description,
+ original.description,
+ )
+ assert.equal(p.products.get(reverted.product_id).name, original.name)
assert.ok(
(await paymentProductSyncStatus(f.db))
.filter((r) => r.plan_id === 'free')