25 lines
		
	
	
		
			822 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			822 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 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) {
 | ||
|       throw createError({
 | ||
|         statusCode: 500,
 | ||
|         message: '创建支付意向失败'
 | ||
|       })
 | ||
|     }
 | ||
|   }
 | ||
| }) 
 |