diff --git a/src/backend.ts b/src/backend.ts index d71bf30..5880345 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -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; @@ -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 @@ -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 @@ -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; @@ -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" };