Skip to content

fix(payments): preserve raw body for Stripe webhook and guard payment intent status (#12) - #13

Open
shrinivas-sn wants to merge 1 commit into
10-Mohan:mainfrom
shrinivas-sn:fix/webhook-raw-body-and-payment-guards-12
Open

shrinivas-sn wants to merge 1 commit into
10-Mohan:mainfrom
shrinivas-sn:fix/webhook-raw-body-and-payment-guards-12

Conversation

@shrinivas-sn

Copy link
Copy Markdown

Summary

Resolves #12

This pull request fixes two critical payment lifecycle issues in TraffiTech:

  1. Stripe Webhook Raw Body Ingestion:

    • In \server/index.js, mounts /api/webhooks\ with \�xpress.raw({ type: 'application/json' })\ before the global \�pp.use(express.json())\ middleware.
    • Prevents the JSON parser from consuming the body stream into a parsed JavaScript object, ensuring \stripe.webhooks.constructEvent\ receives the original raw Buffer required for cryptographic signature verification.
    • Restores automatic booking state transitions from \pending\ to \paid\ and records \paymentId\ upon webhook receipt.
  2. Payment Intent Duplication & Status Guards:

    • In \server/routes/payments.js, validates \plain.paymentStatus\ prior to creating a Stripe PaymentIntent:
      • Rejects requests with HTTP 400 (\Booking is already paid) when \paymentStatus\ is \paid\ or \completed.
      • Rejects requests with HTTP 400 (\Booking is cancelled) when \paymentStatus\ is \cancelled\ or
        efunded.
    • Sanitizes server error responses by removing \�rr.stack\ leakage.
  3. Vitest / Jest Test Isolation:

    • Updates \�ite.config.js\ with \include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']\ so the frontend test runner does not scan backend Jest test files.
    • Adds a comprehensive test suite in \server/tests/paymentsAndWebhooks.test.js\ covering authentication, validation, status guards, raw buffer webhook signature checks, and database updates.

Verification

  • Backend Jest suite: \cd server && npm test\ -> 3 suites passed, 24/24 tests passed (including all 13 new payment and webhook tests).
  • Frontend Vitest suite:
    pm test\ -> 3 suites passed, 22/22 tests passed.
  • Lint check:
    px eslint server/index.js server/routes/payments.js vite.config.js server/tests/paymentsAndWebhooks.test.js\ -> 0 errors, 0 warnings.
  • Prettier check:
    px prettier --check server/index.js server/routes/payments.js vite.config.js server/tests/paymentsAndWebhooks.test.js\ -> All matched files use Prettier code style.
  • Git diff check: \git diff --check\ -> 0 whitespace errors.

@vercel

vercel Bot commented Oct 19, 2026

Copy link
Copy Markdown

@shrinivas-sn is attempting to deploy a commit to the 10-mohan's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Stripe webhook signature verification fails due to body parsing order and create-payment-intent allows charging paid/cancelled bookings

1 participant