-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateObjects.ts
More file actions
193 lines (171 loc) · 5.35 KB
/
createObjects.ts
File metadata and controls
193 lines (171 loc) · 5.35 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import Stripe from "stripe";
const stripeApiKey = prompt("Enter your Stripe API key:");
if (!stripeApiKey) {
console.error("❌ Stripe API key is required");
Deno.exit(1);
}
const stripe = new Stripe(stripeApiKey);
const METER_EVENT_NAME = "credit_usage";
console.info("Starting product and price creation script...");
async function createMeter() {
console.info("Creating new meter...");
return await stripe.billing.meters.create({
default_aggregation: {
formula: "sum",
},
display_name: "credits-meter",
event_name: METER_EVENT_NAME,
customer_mapping: {
type: "by_id",
event_payload_key: "stripe_customer_id",
},
value_settings: {
event_payload_key: "value",
},
});
}
async function createProduct(name: string, description: string) {
console.info(`Creating product: ${name}...`);
return await stripe.products.create({
name,
description,
});
}
async function createPrice(
productId: string,
meterId: string,
config: {
nickname: string;
currency: string;
subscriptionPrice: number;
includedCredits: number;
overagePrice: number;
interval: "month" | "year";
},
) {
console.info(`Creating price: ${config.nickname}...`);
return await stripe.prices.create({
product: productId,
nickname: config.nickname,
currency: config.currency,
billing_scheme: "tiered",
tiers_mode: "graduated",
tiers: [
{
flat_amount: config.subscriptionPrice,
up_to: config.includedCredits,
unit_amount: 0,
},
{
up_to: "inf",
unit_amount: config.overagePrice,
},
],
recurring: {
interval: config.interval,
usage_type: "metered",
meter: meterId,
},
});
}
let meterId = "";
const shouldCreateMeter = prompt("Create meter? (y/n)");
if (shouldCreateMeter === "y") {
try {
console.info("Creating new meter...");
const meter = await createMeter();
meterId = meter.id;
console.info("✓ Meter created successfully");
} catch (error) {
console.error("❌ Failed to create meter:", error);
Deno.exit(1);
}
} else {
console.info("Looking for existing meter...");
const meters = await stripe.billing.meters.list({
status: "active",
limit: 10,
});
const meter = meters.data.find((m) => m.event_name === METER_EVENT_NAME);
if (!meter) {
console.error("❌ Meter not found.");
Deno.exit(1);
}
meterId = meter.id;
console.info("✓ Found existing meter");
}
console.info("\nCreating products...");
// Create products
const basicProduct = await createProduct("Basic", "Basic subscription");
const proProduct = await createProduct("Pro", "Pro subscription");
const premiumProduct = await createProduct("Premium", "Premium subscription");
console.info("✓ All products created successfully\n");
console.info("Creating prices...");
// Basic monthly USD
const basicMonthlyPrice = await createPrice(basicProduct.id, meterId, {
nickname: "Basic Monthly USD",
currency: "usd",
subscriptionPrice: 0, // free
includedCredits: 5, // 5 credits free per month
overagePrice: 200, // 2.00 USD per credit after that
interval: "month",
});
// Pro monthly USD
const proMonthlyPrice = await createPrice(proProduct.id, meterId, {
nickname: "Pro Monthly",
currency: "usd",
subscriptionPrice: 3000, // 30 USD per month
includedCredits: 50, // 50 credits free per month
overagePrice: 100, // 1.00 USD per credit after that
interval: "month",
});
// Pro yearly USD
const proYearlyPrice = await createPrice(proProduct.id, meterId, {
nickname: "Pro Yearly",
currency: "usd",
subscriptionPrice: 3000 * 10, // 30 USD per month (2 months free)
includedCredits: 50 * 12, // 50 credits free per month
overagePrice: 100, // 1.00 USD per credit after that
interval: "year",
});
// Premium monthly USD
const premiumMonthlyPrice = await createPrice(premiumProduct.id, meterId, {
nickname: "Premium Monthly",
currency: "usd",
subscriptionPrice: 10000, // 100 USD per month
includedCredits: 250, // 250 credits free per month
overagePrice: 50, // 0.50 USD per credit after that
interval: "month",
});
// Premium yearly USD
const premiumYearlyPrice = await createPrice(premiumProduct.id, meterId, {
nickname: "Premium Yearly",
currency: "usd",
subscriptionPrice: 10000 * 10, // 100 USD per month (2 months free)
includedCredits: 250 * 12, // 250 credits free per month
overagePrice: 50, // 0.50 USD per credit after that
interval: "year",
});
console.info("✓ All stripe objects created successfully\n");
console.info(`
=== Summary ===
Meter ID: ${meterId}
Basic product ID: ${basicProduct.id}
Basic monthly price ID: ${basicMonthlyPrice.id}
Pro product ID: ${proProduct.id}
Pro monthly price ID: ${proMonthlyPrice.id}
Pro yearly price ID: ${proYearlyPrice.id}
Premium product ID: ${premiumProduct.id}
Premium monthly price ID: ${premiumMonthlyPrice.id}
Premium yearly price ID: ${premiumYearlyPrice.id}
`);
console.info(`
=== Environment variables ===
STRIPE_METER_CREDIT_USAGE_METER_ID=${meterId}
STRIPE_PRODUCT_BASIC_MONTHLY_PRICE_ID=${basicMonthlyPrice.id}
STRIPE_PRODUCT_PRO_MONTHLY_PRICE_ID=${proMonthlyPrice.id}
STRIPE_PRODUCT_PRO_YEARLY_PRICE_ID=${proYearlyPrice.id}
STRIPE_PRODUCT_PREMIUM_MONTHLY_PRICE_ID=${premiumMonthlyPrice.id}
STRIPE_PRODUCT_PREMIUM_YEARLY_PRICE_ID=${premiumYearlyPrice.id}
`);
Deno.exit(0);