112 lines
3.2 KiB
TypeScript
112 lines
3.2 KiB
TypeScript
#!/usr/bin/env tsx
|
|
|
|
import { PrismaClient } from '@prisma/client'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
// 模拟修复后的 webhook 处理逻辑
|
|
async function testWebhookFix() {
|
|
try {
|
|
console.log('🧪 Testing webhook fix...')
|
|
|
|
// 模拟 Stripe 订阅数据
|
|
const mockSubscription = {
|
|
id: "sub_1Rt8nRLW0cChKPJ0Osn5UBcV",
|
|
customer: "cus_SojwymqWZ4EXlZ",
|
|
status: "active",
|
|
start_date: 1754492307,
|
|
items: {
|
|
data: [{
|
|
price: { id: "price_1RslfmLW0cChKPJ0VurJSg9I" },
|
|
current_period_start: 1754492307,
|
|
current_period_end: 1757170707
|
|
}]
|
|
}
|
|
}
|
|
|
|
// 提取数据(使用修复后的逻辑)
|
|
const customerId = mockSubscription.customer
|
|
const status = mockSubscription.status
|
|
const stripeSubscriptionId = mockSubscription.id
|
|
const items = mockSubscription.items
|
|
const priceId = items?.data[0]?.price?.id
|
|
const currentPeriodStart = items?.data[0]?.current_period_start || mockSubscription.start_date
|
|
const currentPeriodEnd = items?.data[0]?.current_period_end || (mockSubscription.start_date + 30 * 24 * 60 * 60)
|
|
|
|
console.log('📊 Extracted data:')
|
|
console.log(' customerId:', customerId)
|
|
console.log(' status:', status)
|
|
console.log(' stripeSubscriptionId:', stripeSubscriptionId)
|
|
console.log(' priceId:', priceId)
|
|
console.log(' currentPeriodStart:', currentPeriodStart)
|
|
console.log(' currentPeriodEnd:', currentPeriodEnd)
|
|
|
|
// 验证日期
|
|
if (!currentPeriodStart || !currentPeriodEnd) {
|
|
console.error('❌ Missing period dates')
|
|
return
|
|
}
|
|
|
|
const startDate = new Date(currentPeriodStart * 1000)
|
|
const endDate = new Date(currentPeriodEnd * 1000)
|
|
|
|
console.log('📅 Converted dates:')
|
|
console.log(' startDate:', startDate.toISOString())
|
|
console.log(' endDate:', endDate.toISOString())
|
|
|
|
// 检查日期是否有效
|
|
if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
|
|
console.error('❌ Invalid dates')
|
|
return
|
|
}
|
|
|
|
console.log('✅ Dates are valid!')
|
|
|
|
// 查找用户
|
|
const user = await prisma.user.findFirst({
|
|
where: { stripeCustomerId: customerId }
|
|
})
|
|
|
|
if (!user) {
|
|
console.error('❌ User not found for customer:', customerId)
|
|
return
|
|
}
|
|
|
|
console.log('👤 Found user:', user.email)
|
|
|
|
// 查找套餐
|
|
const plan = await prisma.subscriptionPlan.findFirst({
|
|
where: { stripePriceId: priceId }
|
|
})
|
|
|
|
if (!plan) {
|
|
console.error('❌ Plan not found for price:', priceId)
|
|
return
|
|
}
|
|
|
|
console.log('📦 Found plan:', plan.name)
|
|
|
|
// 模拟创建订阅记录
|
|
console.log('🔄 Would create subscription with data:')
|
|
console.log({
|
|
userId: user.id,
|
|
subscriptionPlanId: plan.id,
|
|
stripeSubscriptionId,
|
|
stripeCustomerId: customerId,
|
|
isActive: true,
|
|
status: 'active',
|
|
startDate,
|
|
endDate,
|
|
})
|
|
|
|
console.log('✅ Webhook fix test completed successfully!')
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error testing webhook fix:', error)
|
|
} finally {
|
|
await prisma.$disconnect()
|
|
}
|
|
}
|
|
|
|
testWebhookFix()
|