diff --git a/stripe_to_invoice/app/api/stripe/webhook/route.ts b/stripe_to_invoice/app/api/stripe/webhook/route.ts index 0be4f4f..20db5da 100644 --- a/stripe_to_invoice/app/api/stripe/webhook/route.ts +++ b/stripe_to_invoice/app/api/stripe/webhook/route.ts @@ -83,7 +83,6 @@ export async function POST(req: NextRequest) { .limit(1); if (!stripeAccount) { - console.error("❌ Stripe account not registered:", stripeAccountId); return NextResponse.json( { error: "Stripe account not registered" }, { status: 500 } @@ -106,14 +105,12 @@ export async function POST(req: NextRequest) { ); } - if (!xeroConn.salesAccountCode || !xeroConn.stripeClearingAccountCode) { - throw new Error( - "Xero account codes not configured (sales / stripe clearing)" - ); + if (!xeroConn.salesAccountCode) { + throw new Error("Sales account code not configured"); } // -------------------------------------------------- - // 5️⃣ Get VALID Xero access token (refresh handled centrally) + // 5️⃣ Get VALID Xero access token // -------------------------------------------------- const accessToken = await getValidXeroAccessToken(stripeAccount.userId); const xero = getXeroClient(accessToken); @@ -155,7 +152,7 @@ export async function POST(req: NextRequest) { } // -------------------------------------------------- - // 7️⃣ Create AUTHORISED invoice + // 7️⃣ Create AUTHORISED invoice (NO PAYMENT) // -------------------------------------------------- if (!session.amount_total || !session.currency) { throw new Error("Stripe session missing amount or currency"); @@ -168,6 +165,8 @@ export async function POST(req: NextRequest) { throw new Error(`Unsupported currency: ${session.currency}`); } + const today = new Date().toISOString().slice(0, 10); + const invoiceResponse = await xero.accountingApi.createInvoices( xeroConn.tenantId, { @@ -176,6 +175,8 @@ export async function POST(req: NextRequest) { type: Invoice.TypeEnum.ACCREC, status: Invoice.StatusEnum.AUTHORISED, contact: { contactID: contact.contactID }, + date: today, + dueDate: today, lineItems: [ { description: `Stripe payment (${session.id})`, @@ -197,34 +198,14 @@ export async function POST(req: NextRequest) { } // -------------------------------------------------- - // 8️⃣ Mark invoice as PAID → Stripe Clearing - // -------------------------------------------------- - const paymentReference = - typeof session.payment_intent === "string" - ? session.payment_intent - : session.id; - - await xero.accountingApi.createPayments(xeroConn.tenantId, { - payments: [ - { - invoice: { invoiceID: invoice.invoiceID }, - amount, - date: new Date().toISOString().slice(0, 10), - reference: paymentReference, - account: { code: xeroConn.stripeClearingAccountCode }, - }, - ], - }); - - // -------------------------------------------------- - // 9️⃣ Record idempotency (LAST STEP) + // 8️⃣ Record idempotency (LAST STEP) // -------------------------------------------------- await db.insert(processedStripeEvents).values({ stripeEventId: event.id, stripeAccountId, }); - console.log("✅ Stripe → Xero sync complete", { + console.log("✅ Stripe → Xero invoice created", { eventId: event.id, invoiceId: invoice.invoiceID, stripeAccountId,