Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/(authenticated)/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { cadStringToCents } from "@/lib/money";
import {
createPrice,
createProduct,
deactivateActivePricesForProduct,
getStripeServiceData,
replaceProductPrice,
updateProduct,
} from "@/lib/stripe";

Expand Down Expand Up @@ -329,10 +330,10 @@ export async function updateService(
});

if (cents !== undefined) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (cents !== undefined) {
const current = await getStripeServiceData(row.stripeProductId);
if (current?.priceCents !== cents) {
const { priceId } = await replaceProductPrice(row.stripeProductId, cents);
}

// Stripe Prices are immutable: deactivate the current active
// price(s) and create a new one at the new amount.
await deactivateActivePricesForProduct(row.stripeProductId);
await createPrice(row.stripeProductId, cents);
const current = await getStripeServiceData(row.stripeProductId);
if (current?.priceCents !== cents) {
await replaceProductPrice(row.stripeProductId, cents);
}
}

const dbPatch: Partial<typeof services.$inferInsert> = {};
Expand Down
41 changes: 38 additions & 3 deletions lib/stripe.ts
Comment thread
martin0024 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,55 @@ export async function createPrice(
unit_amount: amountCents,
currency: "cad",
});

return { priceId: price.id };
}

export async function deactivateActivePricesForProduct(
export async function replaceProductPrice(
productId: string,
): Promise<void> {
amountCents: number,
): Promise<{ priceId: string }> {
// get the current default price for the product
const product = await stripe.products.retrieve(productId, {
expand: ["default_price"],
});

const currentDefaultPrice =
typeof product.default_price === "string"
? product.default_price
: product.default_price?.id;

// create the new price FIRST (rlly important)
const newPrice = await stripe.prices.create({
product: productId,
unit_amount: amountCents,
currency: "cad",
});

// set the new price as the product default
await stripe.products.update(productId, {
default_price: newPrice.id,
});

// archive old active prices but NEVER archive the new default price
const prices = await stripe.prices.list({
product: productId,
active: true,
limit: 100,
});

for (const p of prices.data) {
await stripe.prices.update(p.id, { active: false });
if (p.id !== newPrice.id && p.id !== currentDefaultPrice) {
await stripe.prices.update(p.id, { active: false });
}
}

// archive the old default price after default_price has been swapped
if (currentDefaultPrice && currentDefaultPrice !== newPrice.id) {
await stripe.prices.update(currentDefaultPrice, { active: false });
}

return { priceId: newPrice.id };
}

export type StripeServiceData = {
Expand Down