Skip to content

Commit 4f16db7

Browse files
make the stripe sync price unsafe
1 parent 5534931 commit 4f16db7

File tree

1 file changed

+63
-68
lines changed

1 file changed

+63
-68
lines changed

server/plugins/5_stripe_price_sync.ts

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,82 @@ export default defineNitroPlugin(async (nitroApp) => {
44
const config = useRuntimeConfig()
55
const stripeSecretKey = config.stripeSecretKey
66

7-
try {
8-
console.log('Starting Stripe product sync...')
7+
console.log('Starting Stripe product sync...')
98

10-
const productResponse = await $fetch<{ data: any[] }>('https://api.stripe.com/v1/products', {
11-
method: 'GET',
12-
headers: {
13-
'Authorization': `Bearer ${stripeSecretKey}`,
14-
},
15-
query: {
16-
active: 'true',
17-
limit: 100,
18-
'expand[]': 'data.default_price'
19-
}
20-
})
9+
const productResponse = await $fetch<{ data: any[] }>('https://api.stripe.com/v1/products', {
10+
method: 'GET',
11+
headers: {
12+
'Authorization': `Bearer ${stripeSecretKey}`,
13+
},
14+
query: {
15+
active: 'true',
16+
limit: 100,
17+
'expand[]': 'data.default_price'
18+
}
19+
})
2120

22-
console.log('Fetched', productResponse.data.length, 'products from Stripe.')
21+
console.log('Fetched', productResponse.data.length, 'products from Stripe.')
2322

2423

25-
const products = await Promise.all(productResponse.data.map(async (product: any) => {
26-
const defaultPriceId = typeof product.default_price === 'object' ? product.default_price.id : product.default_price
27-
let priceData = product.default_price
24+
const products = await Promise.all(productResponse.data.map(async (product: any) => {
25+
const defaultPriceId = typeof product.default_price === 'object' ? product.default_price.id : product.default_price
26+
let priceData = product.default_price
2827

29-
if (defaultPriceId) {
30-
try {
31-
priceData = await $fetch(`https://api.stripe.com/v1/prices/${defaultPriceId}`, {
32-
method: 'GET',
33-
headers: {
34-
'Authorization': `Bearer ${stripeSecretKey}`,
35-
},
36-
query: {
37-
'expand[]': 'currency_options'
38-
}
39-
})
40-
} catch (e) {
41-
console.error(`Failed to fetch price details for ${defaultPriceId}`, e)
42-
}
28+
if (defaultPriceId) {
29+
try {
30+
priceData = await $fetch(`https://api.stripe.com/v1/prices/${defaultPriceId}`, {
31+
method: 'GET',
32+
headers: {
33+
'Authorization': `Bearer ${stripeSecretKey}`,
34+
},
35+
query: {
36+
'expand[]': 'currency_options'
37+
}
38+
})
39+
} catch (e) {
40+
console.error(`Failed to fetch price details for ${defaultPriceId}`, e)
4341
}
42+
}
4443

45-
const price = priceData
46-
const prices: Record<string, number> = {}
44+
const price = priceData
45+
const prices: Record<string, number> = {}
4746

48-
if (price?.currency_options) {
49-
for (const [currency, option] of Object.entries(price.currency_options)) {
50-
prices[currency] = (option as any).unit_amount
51-
}
52-
}
53-
54-
if (price?.currency && !prices[price.currency]) {
55-
prices[price.currency] = price.unit_amount
47+
if (price?.currency_options) {
48+
for (const [currency, option] of Object.entries(price.currency_options)) {
49+
prices[currency] = (option as any).unit_amount
5650
}
51+
}
5752

58-
return {
59-
id: product.id,
60-
stripePriceId: price?.id,
61-
title: product.name,
62-
description: product.description,
63-
balance: Number(product.metadata?.balance || 0),
64-
prices: prices,
65-
updatedAt: new Date()
66-
}
67-
}))
53+
if (price?.currency && !prices[price.currency]) {
54+
prices[price.currency] = price.unit_amount
55+
}
6856

69-
if (products.length > 0) {
70-
const db = await connectDB()
71-
const collection = db.collection('products')
57+
return {
58+
id: product.id,
59+
stripePriceId: price?.id,
60+
title: product.name,
61+
description: product.description,
62+
balance: Number(product.metadata?.balance || 0),
63+
prices: prices,
64+
updatedAt: new Date()
65+
}
66+
}))
7267

73-
const operations = products.map((p: any) => ({
74-
updateOne: {
75-
filter: { id: p.id },
76-
update: { $set: p },
77-
upsert: true
78-
}
79-
}))
68+
if (products.length > 0) {
69+
const db = await connectDB()
70+
const collection = db.collection('products')
8071

81-
await collection.bulkWrite(operations)
82-
console.log(`Synced ${products.length} products from Stripe to MongoDB.`)
83-
} else {
84-
console.log('No active products found in Stripe.')
85-
}
72+
const operations = products.map((p: any) => ({
73+
updateOne: {
74+
filter: { id: p.id },
75+
update: { $set: p },
76+
upsert: true
77+
}
78+
}))
8679

87-
} catch (error) {
88-
console.error('Failed to sync Stripe products:', error)
80+
await collection.bulkWrite(operations)
81+
console.log(`Synced ${products.length} products from Stripe to MongoDB.`)
82+
} else {
83+
throw new Error('No active products found in Stripe.')
8984
}
9085
})

0 commit comments

Comments
 (0)