diff --git a/apps/backend/src/lib/payments.tsx b/apps/backend/src/lib/payments.tsx index 25c913dc33..1865db6ba9 100644 --- a/apps/backend/src/lib/payments.tsx +++ b/apps/backend/src/lib/payments.tsx @@ -1,7 +1,7 @@ import { PrismaClientTransaction } from "@/prisma-client"; import { PurchaseCreationSource, SubscriptionStatus } from "@prisma/client"; import { KnownErrors } from "@stackframe/stack-shared"; -import type { inlineProductSchema, productSchema } from "@stackframe/stack-shared/dist/schema-fields"; +import type { inlineProductSchema, productSchema, productSchemaWithMetadata } from "@stackframe/stack-shared/dist/schema-fields"; import { SUPPORTED_CURRENCIES } from "@stackframe/stack-shared/dist/utils/currency-constants"; import { FAR_FUTURE_DATE, addInterval, getIntervalsElapsed } from "@stackframe/stack-shared/dist/utils/dates"; import { StackAssertionError, StatusError, throwErr } from "@stackframe/stack-shared/dist/utils/errors"; @@ -16,6 +16,7 @@ import { getStripeForAccount } from "./stripe"; const DEFAULT_PRODUCT_START_DATE = new Date("1973-01-01T12:00:00.000Z"); // monday type Product = yup.InferType; +type ProductWithMetadata = yup.InferType; type SelectedPrice = Exclude[string]; export async function ensureProductIdOrInlineProduct( @@ -23,7 +24,7 @@ export async function ensureProductIdOrInlineProduct( accessType: "client" | "server" | "admin", productId: string | undefined, inlineProduct: yup.InferType | undefined -): Promise { +): Promise { if (productId && inlineProduct) { throw new StatusError(400, "Cannot specify both product_id and product_inline!"); } @@ -61,6 +62,9 @@ export async function ensureProductIdOrInlineProduct( freeTrial: value.free_trial, serverOnly: true, }])), + clientMetadata: inlineProduct.client_metadata ?? undefined, + clientReadOnlyMetadata: inlineProduct.client_read_only_metadata ?? undefined, + serverMetadata: inlineProduct.server_metadata ?? undefined, includedItems: typedFromEntries(Object.entries(inlineProduct.included_items).map(([key, value]) => [key, { repeat: value.repeat ?? "never", quantity: value.quantity ?? 0, @@ -420,13 +424,16 @@ export async function ensureCustomerExists(options: { } } -export function productToInlineProduct(product: Product): yup.InferType { +export function productToInlineProduct(product: ProductWithMetadata): yup.InferType { return { display_name: product.displayName ?? "Product", customer_type: product.customerType, stackable: product.stackable === true, server_only: product.serverOnly === true, included_items: product.includedItems, + client_metadata: product.clientMetadata ?? null, + client_read_only_metadata: product.clientReadOnlyMetadata ?? null, + server_metadata: product.serverMetadata ?? null, prices: product.prices === "include-by-default" ? {} : typedFromEntries(typedEntries(product.prices).map(([key, value]) => [key, filterUndefined({ ...typedFromEntries(SUPPORTED_CURRENCIES.map(c => [c.code, getOrUndefined(value, c.code)])), interval: value.interval, @@ -552,7 +559,7 @@ export async function grantProductToCustomer(options: { tenancy: Tenancy, customerType: "user" | "team" | "custom", customerId: string, - product: Product, + product: ProductWithMetadata, quantity: number, productId: string | undefined, priceId: string | undefined, @@ -691,7 +698,7 @@ export async function getOwnedProductsForCustomer(options: { } for (const purchase of oneTimePurchases) { - const product = purchase.product as Product; + const product = purchase.product as ProductWithMetadata; ownedProducts.push({ id: purchase.productId ?? null, type: "one_time", diff --git a/apps/e2e/tests/backend/endpoints/api/v1/payments/before-offer-to-product-rename/outdated--validate-code.test.ts b/apps/e2e/tests/backend/endpoints/api/v1/payments/before-offer-to-product-rename/outdated--validate-code.test.ts index ef63b9980c..1e507627a9 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/payments/before-offer-to-product-rename/outdated--validate-code.test.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/payments/before-offer-to-product-rename/outdated--validate-code.test.ts @@ -41,6 +41,8 @@ it("should allow valid code and return offer data", async ({ expect }) => { "charges_enabled": false, "conflicting_products": [], "product": { + "client_metadata": null, + "client_read_only_metadata": null, "customer_type": "user", "display_name": "Test Product", "included_items": {}, @@ -53,6 +55,7 @@ it("should allow valid code and return offer data", async ({ expect }) => { ], }, }, + "server_metadata": null, "server_only": false, "stackable": false, }, @@ -221,6 +224,8 @@ it("should include conflicting_group_offers when switching within the same group }, ], "product": { + "client_metadata": null, + "client_read_only_metadata": null, "customer_type": "user", "display_name": "Offer B", "included_items": {}, @@ -233,6 +238,7 @@ it("should include conflicting_group_offers when switching within the same group ], }, }, + "server_metadata": null, "server_only": false, "stackable": false, }, diff --git a/apps/e2e/tests/backend/endpoints/api/v1/payments/create-purchase-url.test.ts b/apps/e2e/tests/backend/endpoints/api/v1/payments/create-purchase-url.test.ts index a649b5208b..26d329be51 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/payments/create-purchase-url.test.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/payments/create-purchase-url.test.ts @@ -279,6 +279,89 @@ it("should allow product_inline when calling from server", async ({ expect }) => expect(response.body.url).toMatch(new RegExp(`^https?:\\/\\/localhost:${withPortPrefix("01")}\/purchase\/[a-z0-9-_]+$`)); }); +it("should return inline product metadata when validating purchase code", async ({ expect }) => { + await Project.createAndSwitch({ config: { magic_link_enabled: true } }); + await Payments.setup(); + + const { userId } = await Auth.Otp.signIn(); + const createResponse = await niceBackendFetch("/api/latest/payments/purchases/create-purchase-url", { + method: "POST", + accessType: "server", + body: { + customer_type: "user", + customer_id: userId, + product_inline: { + display_name: "Metadata Inline Product", + customer_type: "user", + server_only: true, + prices: { + "monthly-metadata": { + USD: "1500", + interval: [1, "month"], + }, + }, + included_items: {}, + server_metadata: { + reference_id: "ref-123", + features: ["priority-support", "analytics"], + }, + }, + }, + }); + expect(createResponse.status).toBe(200); + const url = (createResponse.body as { url: string }).url; + const codeMatch = url.match(/\/purchase\/([a-z0-9-_]+)/); + const fullCode = codeMatch ? codeMatch[1] : undefined; + expect(fullCode).toBeDefined(); + + const validateResponse = await niceBackendFetch("/api/latest/payments/purchases/validate-code", { + method: "POST", + accessType: "client", + body: { + full_code: fullCode, + }, + }); + expect(validateResponse).toMatchInlineSnapshot(` + NiceResponse { + "status": 200, + "body": { + "already_bought_non_stackable": false, + "charges_enabled": false, + "conflicting_products": [], + "product": { + "client_metadata": null, + "client_read_only_metadata": null, + "customer_type": "user", + "display_name": "Metadata Inline Product", + "included_items": {}, + "prices": { + "monthly-metadata": { + "USD": "1500", + "interval": [ + 1, + "month", + ], + }, + }, + "server_metadata": { + "features": [ + "priority-support", + "analytics", + ], + "reference_id": "ref-123", + }, + "server_only": true, + "stackable": false, + }, + "project_id": "", + "stripe_account_id": , + "test_mode": true, + }, + "headers": Headers {