liveh5-nuxt/server/middleware/stripe.ts
2025-02-28 20:30:45 +08:00

26 lines
881 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default defineEventHandler(async (event) => {
const url = getRequestURL(event)
// 只处理 create-payment-intent 请求
if (url.pathname === '/create-payment-intent' && event.method === 'POST') {
try {
const body = await readBody(event)
const { items } = body
// 计算总金额
const amount = items.reduce((total: number, item: any) => total + item.amount, 0)
// 模拟创建支付意向的响应
// 注意clientSecret 格式应该类似于 'pi_xxxxx_secret_xxxxx'
return {
clientSecret: `pi_${Math.random().toString(36).substring(2)}_secret_${Math.random().toString(36).substring(2)}`
}
} catch (error) {
console.error('Create payment intent error:', error)
throw createError({
statusCode: 500,
message: '创建支付意向失败'
})
}
}
})