|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Stripe Test Environment Setup Script |
| 5 | + * |
| 6 | + * This script creates products and prices in your Stripe test environment and |
| 7 | + * updates the .env file with the new price IDs. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * node bin/sync-stripe-test-env.js |
| 11 | + */ |
| 12 | + |
| 13 | +import Stripe from 'stripe'; |
| 14 | +import dotenv from 'dotenv-flow'; |
| 15 | +import fs from 'fs'; |
| 16 | +import path from 'path'; |
| 17 | +import { fileURLToPath } from 'url'; |
| 18 | + |
| 19 | +// Get the directory name |
| 20 | +const __filename = fileURLToPath(import.meta.url); |
| 21 | +const __dirname = path.dirname(__filename); |
| 22 | + |
| 23 | +// Load environment variables |
| 24 | +dotenv.config({ path: path.join(__dirname, '..') }); |
| 25 | + |
| 26 | +// Configuration |
| 27 | +const PRODUCTS = [ |
| 28 | + { |
| 29 | + name: 'PDF Service Subscription', |
| 30 | + description: 'Subscription for PDF conversion service', |
| 31 | + prices: [ |
| 32 | + { |
| 33 | + nickname: 'Monthly Subscription', |
| 34 | + unit_amount: 500, // $5.00 |
| 35 | + currency: 'usd', |
| 36 | + recurring: { |
| 37 | + interval: 'month' |
| 38 | + }, |
| 39 | + metadata: { |
| 40 | + plan: 'monthly' |
| 41 | + } |
| 42 | + }, |
| 43 | + { |
| 44 | + nickname: 'Yearly Subscription', |
| 45 | + unit_amount: 3000, // $30.00 |
| 46 | + currency: 'usd', |
| 47 | + recurring: { |
| 48 | + interval: 'year' |
| 49 | + }, |
| 50 | + metadata: { |
| 51 | + plan: 'yearly' |
| 52 | + } |
| 53 | + } |
| 54 | + ] |
| 55 | + } |
| 56 | +]; |
| 57 | + |
| 58 | +// Initialize Stripe |
| 59 | +const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { |
| 60 | + apiVersion: '2023-10-16', |
| 61 | +}); |
| 62 | + |
| 63 | +async function main() { |
| 64 | + console.log('Starting Stripe test environment setup...'); |
| 65 | + |
| 66 | + // Create/update products and prices |
| 67 | + let monthlyPriceId = null; |
| 68 | + let yearlyPriceId = null; |
| 69 | + |
| 70 | + for (const productConfig of PRODUCTS) { |
| 71 | + // Check if product exists |
| 72 | + let product; |
| 73 | + const existingProducts = await stripe.products.list({ |
| 74 | + active: true, |
| 75 | + }); |
| 76 | + |
| 77 | + const existingProduct = existingProducts.data.find(p => p.name === productConfig.name); |
| 78 | + |
| 79 | + if (existingProduct) { |
| 80 | + console.log(`Product '${productConfig.name}' already exists with ID: ${existingProduct.id}`); |
| 81 | + product = existingProduct; |
| 82 | + } else { |
| 83 | + console.log(`Creating product: ${productConfig.name}`); |
| 84 | + product = await stripe.products.create({ |
| 85 | + name: productConfig.name, |
| 86 | + description: productConfig.description, |
| 87 | + active: true, |
| 88 | + }); |
| 89 | + console.log(`Created product with ID: ${product.id}`); |
| 90 | + } |
| 91 | + |
| 92 | + // Process prices for this product |
| 93 | + for (const priceConfig of productConfig.prices) { |
| 94 | + // Check for existing prices with the same nickname |
| 95 | + const existingPrices = await stripe.prices.list({ |
| 96 | + active: true, |
| 97 | + product: product.id, |
| 98 | + }); |
| 99 | + |
| 100 | + const existingPrice = existingPrices.data.find(p => p.nickname === priceConfig.nickname); |
| 101 | + |
| 102 | + if (existingPrice) { |
| 103 | + console.log(`Price '${priceConfig.nickname}' already exists with ID: ${existingPrice.id}`); |
| 104 | + |
| 105 | + // Store the price IDs |
| 106 | + if (priceConfig.metadata.plan === 'monthly') { |
| 107 | + monthlyPriceId = existingPrice.id; |
| 108 | + } else if (priceConfig.metadata.plan === 'yearly') { |
| 109 | + yearlyPriceId = existingPrice.id; |
| 110 | + } |
| 111 | + } else { |
| 112 | + console.log(`Creating price: ${priceConfig.nickname}`); |
| 113 | + const newPrice = await stripe.prices.create({ |
| 114 | + product: product.id, |
| 115 | + nickname: priceConfig.nickname, |
| 116 | + unit_amount: priceConfig.unit_amount, |
| 117 | + currency: priceConfig.currency, |
| 118 | + recurring: priceConfig.recurring, |
| 119 | + metadata: priceConfig.metadata, |
| 120 | + }); |
| 121 | + |
| 122 | + console.log(`Created price with ID: ${newPrice.id}`); |
| 123 | + |
| 124 | + // Store the price IDs |
| 125 | + if (priceConfig.metadata.plan === 'monthly') { |
| 126 | + monthlyPriceId = newPrice.id; |
| 127 | + } else if (priceConfig.metadata.plan === 'yearly') { |
| 128 | + yearlyPriceId = newPrice.id; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Update .env file with the new price IDs if they exist |
| 135 | + if (monthlyPriceId || yearlyPriceId) { |
| 136 | + console.log('Updating .env file with new price IDs...'); |
| 137 | + |
| 138 | + const envFilePath = path.join(__dirname, '..', '.env'); |
| 139 | + let envContent = fs.readFileSync(envFilePath, 'utf8'); |
| 140 | + |
| 141 | + if (monthlyPriceId) { |
| 142 | + // Check if STRIPE_MONTHLY_PRICE_ID already exists in .env |
| 143 | + if (envContent.includes('STRIPE_MONTHLY_PRICE_ID=')) { |
| 144 | + // Replace existing value |
| 145 | + envContent = envContent.replace( |
| 146 | + /STRIPE_MONTHLY_PRICE_ID=.*/g, |
| 147 | + `STRIPE_MONTHLY_PRICE_ID=${monthlyPriceId}` |
| 148 | + ); |
| 149 | + } else { |
| 150 | + // Add new variable |
| 151 | + envContent += `\nSTRIPE_MONTHLY_PRICE_ID=${monthlyPriceId}`; |
| 152 | + } |
| 153 | + console.log(`Set STRIPE_MONTHLY_PRICE_ID=${monthlyPriceId}`); |
| 154 | + } |
| 155 | + |
| 156 | + if (yearlyPriceId) { |
| 157 | + // Check if STRIPE_YEARLY_PRICE_ID already exists in .env |
| 158 | + if (envContent.includes('STRIPE_YEARLY_PRICE_ID=')) { |
| 159 | + // Replace existing value |
| 160 | + envContent = envContent.replace( |
| 161 | + /STRIPE_YEARLY_PRICE_ID=.*/g, |
| 162 | + `STRIPE_YEARLY_PRICE_ID=${yearlyPriceId}` |
| 163 | + ); |
| 164 | + } else { |
| 165 | + // Add new variable |
| 166 | + envContent += `\nSTRIPE_YEARLY_PRICE_ID=${yearlyPriceId}`; |
| 167 | + } |
| 168 | + console.log(`Set STRIPE_YEARLY_PRICE_ID=${yearlyPriceId}`); |
| 169 | + } |
| 170 | + |
| 171 | + // Write updated content back to .env file |
| 172 | + fs.writeFileSync(envFilePath, envContent); |
| 173 | + |
| 174 | + console.log('.env file updated successfully!'); |
| 175 | + } |
| 176 | + |
| 177 | + console.log('Stripe test environment setup complete!'); |
| 178 | +} |
| 179 | + |
| 180 | +// Run the main function |
| 181 | +main().catch(error => { |
| 182 | + console.error('Error:', error); |
| 183 | + process.exit(1); |
| 184 | +}); |
0 commit comments