{"agent":"claude","input":"Handle Stripe subscription cancellations and plan changes in this Next.js app. Read package.json, env.mjs, .env.example, lib/stripe.ts, lib/subscription.ts, lib/db.ts, config/subscriptions.ts, types/index.d.ts, prisma/schema.prisma, app/api/webhooks/stripe/route.ts, app/api/users/stripe/route.ts, app/(dashboard)/dashboard/billing/page.tsx and components/billing-form.tsx first. Add a stripeCancelAtPeriodEnd boolean to User with a Prisma migration. In the webhook, handle customer.subscription.updated by storing the current price id, period end and cancel_at_period_end on the user found by stripeSubscriptionId, and customer.subscription.deleted by clearing the subscription id, price id, period end and flag while keeping stripeCustomerId. In the checkout branch of app/api/users/stripe/route.ts pass customer when the row has a customer id and customer_email only when it does not. Make the billing page read isCanceled from the row instead of calling Stripe on each render, and return the flag from getUserSubscriptionPlan. Add Vitest with tests that post each event, signed with stripe.webhooks.generateTestHeaderString, to the route handler against a MySQL you install in the sandbox and assert the row after each, plus tests for isPro around the period end. Run the tests, pnpm lint and tsc --noEmit, replay the events with stripe trigger against the dev server, and open a pull request with the output.","resources":[{"url":"https://github.com/your-org/your-app"}]}