-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstripeSetup.ts
169 lines (149 loc) · 4.73 KB
/
stripeSetup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
if (process.env.NODE_ENV === 'production') {
require('dotenv').config(); // Load from .env in production
} else {
require('dotenv').config({ path: '.env.local' }); // Load from .env.local in development
}
const Stripe = require('stripe');
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// Types
interface Plan {
name: string;
price: number;
description: string;
features: string[];
}
interface StripeProduct {
id: string;
name: string;
metadata?: {
features?: string;
};
}
interface WebhookEndpoint {
url: string;
}
// Configuration
const PUBLIC_URL = process.env.NEXT_PUBLIC_WEBSITE_URL || "http://localhost:3000";
const CURRENCY = 'usd';
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
// Product Plans
const plans: Plan[] = [
{
name: IS_PRODUCTION ? 'Basic' : 'Basic-Test',
price: 1000, // price in cents
description: 'Perfect for small teams and individuals',
features: [
'Up to 10 users',
'Up to 1000 records',
'Up to 1000 API calls'
]
},
{
name: IS_PRODUCTION ? 'Pro' : 'Pro-Test',
price: 2000,
description: 'Great for growing teams',
features: [
'Up to 100 users',
'Up to 10000 records',
'Up to 10000 API calls'
]
},
{
name: IS_PRODUCTION ? 'Enterprise' : 'Enterprise-Test',
price: 5000,
description: 'For large organizations',
features: [
'Unlimited users',
'Unlimited records',
'Unlimited API calls'
]
}
];
// Helper Functions
async function createProduct(plan: Plan): Promise<StripeProduct> {
// Check if product exists
const existingProducts = await stripe.products.list({ active: true });
let product = existingProducts.data.find((p: StripeProduct) => p.name === plan.name);
if (!product) {
// Create new product if it doesn't exist
product = await stripe.products.create({
name: plan.name,
description: plan.description,
metadata: {
features: JSON.stringify(plan.features)
}
});
console.log(`Created product: ${plan.name}`);
} else {
// Update existing product's features
product = await stripe.products.update(product.id, {
description: plan.description,
metadata: {
features: JSON.stringify(plan.features)
}
});
console.log(`Updated product: ${plan.name}`);
}
return product;
}
async function createPrice(product: StripeProduct, plan: Plan): Promise<void> {
// Check if price exists
const existingPrices = await stripe.prices.list({
product: product.id,
active: true
});
if (existingPrices.data.length === 0) {
// Create new price if none exists
const price = await stripe.prices.create({
product: product.id,
unit_amount: plan.price,
currency: CURRENCY,
recurring: { interval: 'month' }
});
// Set as default price
await stripe.products.update(product.id, {
default_price: price.id
});
console.log(`Created price for ${plan.name}: ${price.id}`);
}
}
async function setupWebhook(): Promise<void> {
// Skip webhook setup in development
if (!IS_PRODUCTION) {
console.log('Skipping webhook setup in development');
console.log('Use Stripe CLI for local testing: https://stripe.com/docs/stripe-cli');
return;
}
const webhooks = await stripe.webhookEndpoints.list();
const webhookUrl = `${PUBLIC_URL}/webhook/stripe`;
if (!webhooks.data.some((webhook: WebhookEndpoint) => webhook.url === webhookUrl)) {
await stripe.webhookEndpoints.create({
enabled_events: [
'customer.subscription.created',
'customer.subscription.deleted',
'customer.subscription.updated'
],
url: webhookUrl,
});
console.log('Created webhook endpoint');
}
}
// Main Setup Function
async function setupStripe(): Promise<void> {
try {
console.log(`Setting up Stripe in ${IS_PRODUCTION ? 'production' : 'development'} mode...`);
// Setup products and prices
for (const plan of plans) {
const product = await createProduct(plan);
await createPrice(product, plan);
}
// Setup webhook
await setupWebhook();
console.log('Stripe setup completed successfully');
} catch (error) {
console.error('Error setting up Stripe:', error);
throw error;
}
}
// Run Setup
setupStripe().catch(console.error);