From f4f894fcf762fdcb17ab5ea81cc8326f4ea1d136 Mon Sep 17 00:00:00 2001 From: Benson Cho <100653148+choden-dev@users.noreply.github.com> Date: Sun, 8 Mar 2026 23:22:22 +1300 Subject: [PATCH 1/2] Refactor createUniqueProducts function and update checkout logic --- lib/stripe.ts | 31 ++++++++++++------------------- pages/api/checkout.ts | 4 ++-- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/stripe.ts b/lib/stripe.ts index 17abd7e..cdd62be 100644 --- a/lib/stripe.ts +++ b/lib/stripe.ts @@ -90,36 +90,29 @@ export const getPriceForPages = async (pages: number, isColor: boolean) => { }; }; -export const createUniqueProductsForDuplicates = async ( +export const createUniqueProducts = async ( items: (StripeBackendItem & { name: string })[], ) => { const stripe: Stripe = await makeStripeConnection(); - await Promise.all( - items.map(async (item, index) => { - const duplicateIndex = items.findIndex( - (otherItem, otherIndex) => - otherIndex !== index && otherItem.price === item.price, - ); - - if (duplicateIndex !== -1) { - const duplicate = items[duplicateIndex]; - const price = await stripe.prices.retrieve(duplicate.priceId); + return await Promise.all( + items.map(async (item) => { + const price = await stripe.prices.retrieve(item.priceId); const product = await stripe.products.create({ - name: duplicate.name, + name: item.name, default_price_data: { unit_amount_decimal: price.unit_amount_decimal as string, currency: price.currency, }, }); - items[index].productId = product.id; - items[index].price = product.default_price as string; - } + return { + ...item, + productId: product.id, + price: product.default_price as string + } }), ); - - return items; }; export const createCoupons = async ( @@ -129,8 +122,9 @@ export const createCoupons = async ( const itemsWithBulkDiscount = getItemsWithBulkDiscount(items); if (itemsWithBulkDiscount.length === 0) return undefined; + if (getPercentOff() === 0) return undefined; - const coupon = await stripe.coupons.create({ +return await stripe.coupons.create({ percent_off: getPercentOff(), applies_to: { products: itemsWithBulkDiscount.map((item) => item.productId), @@ -138,7 +132,6 @@ export const createCoupons = async ( duration: "once", name: `${getMinimumItemsForDiscount()} or more discount!`, }); - return coupon; }; export const createSession = async ( diff --git a/pages/api/checkout.ts b/pages/api/checkout.ts index 2444e97..fd9ae22 100644 --- a/pages/api/checkout.ts +++ b/pages/api/checkout.ts @@ -3,7 +3,7 @@ import { z } from "zod"; import { createCoupons, createSession, - createUniqueProductsForDuplicates, + createUniqueProducts, } from "../../lib/stripe"; const CheckoutSchema = z.object({ @@ -27,7 +27,7 @@ export default async function handler( try { const parsedBody = CheckoutSchema.parse(JSON.parse(req.body)); - const updatedItems = await createUniqueProductsForDuplicates( + const updatedItems = await createUniqueProducts( parsedBody.items, ); const coupon = await createCoupons(updatedItems); From 60c106dbca4b27e288595d3e731fc6c805424bc1 Mon Sep 17 00:00:00 2001 From: Benson Cho <100653148+choden-dev@users.noreply.github.com> Date: Sun, 8 Mar 2026 23:24:20 +1300 Subject: [PATCH 2/2] Refactor createUniqueProducts function and update checkout logic --- lib/stripe.ts | 30 +++++++++++++++--------------- pages/api/checkout.ts | 4 +--- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/stripe.ts b/lib/stripe.ts index cdd62be..9e8aec1 100644 --- a/lib/stripe.ts +++ b/lib/stripe.ts @@ -97,20 +97,20 @@ export const createUniqueProducts = async ( return await Promise.all( items.map(async (item) => { - const price = await stripe.prices.retrieve(item.priceId); - const product = await stripe.products.create({ - name: item.name, - default_price_data: { - unit_amount_decimal: price.unit_amount_decimal as string, - currency: price.currency, - }, - }); - - return { - ...item, - productId: product.id, - price: product.default_price as string - } + const price = await stripe.prices.retrieve(item.priceId); + const product = await stripe.products.create({ + name: item.name, + default_price_data: { + unit_amount_decimal: price.unit_amount_decimal as string, + currency: price.currency, + }, + }); + + return { + ...item, + productId: product.id, + price: product.default_price as string, + }; }), ); }; @@ -124,7 +124,7 @@ export const createCoupons = async ( if (itemsWithBulkDiscount.length === 0) return undefined; if (getPercentOff() === 0) return undefined; -return await stripe.coupons.create({ + return await stripe.coupons.create({ percent_off: getPercentOff(), applies_to: { products: itemsWithBulkDiscount.map((item) => item.productId), diff --git a/pages/api/checkout.ts b/pages/api/checkout.ts index fd9ae22..17c0363 100644 --- a/pages/api/checkout.ts +++ b/pages/api/checkout.ts @@ -27,9 +27,7 @@ export default async function handler( try { const parsedBody = CheckoutSchema.parse(JSON.parse(req.body)); - const updatedItems = await createUniqueProducts( - parsedBody.items, - ); + const updatedItems = await createUniqueProducts(parsedBody.items); const coupon = await createCoupons(updatedItems); const session = await createSession(