(invoice: Stripe.Invoice)
| 300 | } |
| 301 | |
| 302 | async function handleInvoicePaid(invoice: Stripe.Invoice) { |
| 303 | // For regular (non-auto-topup) invoices, verify credit note exists |
| 304 | const creditNotes = await stripeServer.creditNotes.list({ |
| 305 | invoice: invoice.id, |
| 306 | }) |
| 307 | |
| 308 | let customerId: string | null = null |
| 309 | if (invoice.customer) { |
| 310 | customerId = getStripeId(invoice.customer) |
| 311 | } |
| 312 | |
| 313 | if (creditNotes.data.length > 0) { |
| 314 | logger.info( |
| 315 | { |
| 316 | invoiceId: invoice.id, |
| 317 | creditNoteIds: creditNotes.data.map((cn) => cn.id), |
| 318 | customerId, |
| 319 | }, |
| 320 | 'Invoice paid with existing credit notes - no action needed', |
| 321 | ) |
| 322 | } else { |
| 323 | logger.warn( |
| 324 | { |
| 325 | invoiceId: invoice.id, |
| 326 | customerId, |
| 327 | }, |
| 328 | 'Invoice paid but no credit notes found - this may indicate a missing credit note from draft stage', |
| 329 | ) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | const webhookHandler = async (req: NextRequest): Promise<NextResponse> => { |
| 334 | let event: Stripe.Event |
no test coverage detected