|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { stripe } from '@/actions/organization/lib/stripe'; |
| 4 | +import { env } from '@/env.mjs'; |
| 5 | +import { client } from '@comp/kv'; |
| 6 | +import Stripe from 'stripe'; |
| 7 | + |
| 8 | +type PriceDetails = { |
| 9 | + id: string; |
| 10 | + unitAmount: number | null; |
| 11 | + currency: string; |
| 12 | + interval: Stripe.Price.Recurring.Interval | null; |
| 13 | + productName: string | null; |
| 14 | +}; |
| 15 | + |
| 16 | +export type CachedPrices = { |
| 17 | + monthlyPrice: PriceDetails | null; |
| 18 | + yearlyPrice: PriceDetails | null; |
| 19 | + fetchedAt: number; |
| 20 | +}; |
| 21 | + |
| 22 | +const CACHE_DURATION = 30 * 60; // 30 minutes in seconds |
| 23 | + |
| 24 | +export async function fetchStripePriceDetails(): Promise<CachedPrices> { |
| 25 | + // Fetch from Stripe |
| 26 | + const monthlyPriceId = env.NEXT_PUBLIC_STRIPE_SUBSCRIPTION_MANAGED_MONTHLY_PRICE_ID; |
| 27 | + const yearlyPriceId = env.NEXT_PUBLIC_STRIPE_SUBSCRIPTION_MANAGED_YEARLY_PRICE_ID; |
| 28 | + |
| 29 | + // Create a unique cache key that includes the price IDs |
| 30 | + const cacheKey = `stripe:managed-prices:${monthlyPriceId || 'none'}:${yearlyPriceId || 'none'}`; |
| 31 | + |
| 32 | + try { |
| 33 | + // Check cache first |
| 34 | + const cached = await client.get<CachedPrices>(cacheKey); |
| 35 | + if (cached && cached.fetchedAt && Date.now() - cached.fetchedAt < CACHE_DURATION * 1000) { |
| 36 | + return cached; |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + console.error('[STRIPE] Error reading from cache:', error); |
| 40 | + } |
| 41 | + |
| 42 | + let monthlyPrice: PriceDetails | null = null; |
| 43 | + let yearlyPrice: PriceDetails | null = null; |
| 44 | + |
| 45 | + try { |
| 46 | + // Fetch monthly price if ID exists |
| 47 | + if (monthlyPriceId) { |
| 48 | + const price = await stripe.prices.retrieve(monthlyPriceId, { |
| 49 | + expand: ['product'], |
| 50 | + }); |
| 51 | + |
| 52 | + monthlyPrice = { |
| 53 | + id: price.id, |
| 54 | + unitAmount: price.unit_amount, |
| 55 | + currency: price.currency, |
| 56 | + interval: price.recurring?.interval ?? null, |
| 57 | + productName: |
| 58 | + price.product && typeof price.product === 'object' && !price.product.deleted |
| 59 | + ? price.product.name |
| 60 | + : null, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // Fetch yearly price if ID exists |
| 65 | + if (yearlyPriceId) { |
| 66 | + const price = await stripe.prices.retrieve(yearlyPriceId, { |
| 67 | + expand: ['product'], |
| 68 | + }); |
| 69 | + |
| 70 | + yearlyPrice = { |
| 71 | + id: price.id, |
| 72 | + unitAmount: price.unit_amount, |
| 73 | + currency: price.currency, |
| 74 | + interval: price.recurring?.interval ?? null, |
| 75 | + productName: |
| 76 | + price.product && typeof price.product === 'object' && !price.product.deleted |
| 77 | + ? price.product.name |
| 78 | + : null, |
| 79 | + }; |
| 80 | + } |
| 81 | + } catch (error) { |
| 82 | + console.error('[STRIPE] Error fetching prices:', error); |
| 83 | + } |
| 84 | + |
| 85 | + const priceData: CachedPrices = { |
| 86 | + monthlyPrice, |
| 87 | + yearlyPrice, |
| 88 | + fetchedAt: Date.now(), |
| 89 | + }; |
| 90 | + |
| 91 | + // Cache the results |
| 92 | + try { |
| 93 | + await client.set(cacheKey, priceData, { |
| 94 | + ex: CACHE_DURATION, |
| 95 | + }); |
| 96 | + } catch (error) { |
| 97 | + console.error('[STRIPE] Error caching price data:', error); |
| 98 | + } |
| 99 | + |
| 100 | + return priceData; |
| 101 | +} |
0 commit comments