Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

added support for promotion codes #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
63 changes: 32 additions & 31 deletions src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export const customerHasFeature = async ({ customerId, feature }) => {
subscription.id,
{ expand: ["items.data.price.product"] }
);
const features = (subscription.items.data[0].price
.product as Stripe.Product).metadata.features;
const features = (
subscription.items.data[0].price.product as Stripe.Product
).metadata.features;
return features?.includes(feature);
}
return false;
Expand All @@ -47,12 +48,11 @@ async function useSubscription({ customerId }) {
// Retrieve products based on default billing portal config

// First, retrieve the configuration
const configurations = await stripeApiClient.billingPortal.configurations.list(
{
const configurations =
await stripeApiClient.billingPortal.configurations.list({
is_default: true,
expand: ["data.features.subscription_update.products"],
}
);
});

// Stripe doesn't let us expand as much as we'd like.
// Run this big mess to manually expand
Expand All @@ -61,28 +61,29 @@ async function useSubscription({ customerId }) {
const products = new Array(
configurations.data[0].features.subscription_update.products.length
);
const pricePromises = configurations.data[0].features.subscription_update.products
.map((product, i) =>
product.prices.map(async (price, j) => {
const priceData = await stripeApiClient.prices.retrieve(price, {
expand: ["product"],
});
const cleanPriceData = {
...priceData,
product: (priceData.product as Stripe.Product).id,
};
if (!products[i]) {
products[i] = {
product: priceData.product,
prices: new Array(product.prices.length),
const pricePromises =
configurations.data[0].features.subscription_update.products
.map((product, i) =>
product.prices.map(async (price, j) => {
const priceData = await stripeApiClient.prices.retrieve(price, {
expand: ["product"],
});
const cleanPriceData = {
...priceData,
product: (priceData.product as Stripe.Product).id,
};
products[i].prices[j] = cleanPriceData;
} else {
products[i].prices[j] = cleanPriceData;
}
})
)
.flat();
if (!products[i]) {
products[i] = {
product: priceData.product,
prices: new Array(product.prices.length),
};
products[i].prices[j] = cleanPriceData;
} else {
products[i].prices[j] = cleanPriceData;
}
})
)
.flat();

let subscription;
const subscriptionPromise = stripeApiClient.customers
Expand All @@ -106,12 +107,11 @@ async function redirectToCustomerPortal({ customerId, body }) {
}

async function redirectToCheckout({ customerId, body }) {
const configurations = await stripeApiClient.billingPortal.configurations.list(
{
const configurations =
await stripeApiClient.billingPortal.configurations.list({
is_default: true,
expand: ["data.features.subscription_update.products"],
}
);
});

// Make sure the price ID is in here somewhere
let go = false;
Expand All @@ -132,6 +132,7 @@ async function redirectToCheckout({ customerId, body }) {
cancel_url: body.cancelUrl,
line_items: [{ price: body.price, quantity: 1 }],
mode: "subscription",
allow_promotion_codes: true,
});
}
return { error: "Error" };
Expand Down