Skip to content

Fix net_amount double-deducting GST on Razorpay gateway fees#27

Open
TheShahnawaaz wants to merge 1 commit into
mainfrom
fix/razorpay-gateway-fee-net-amount
Open

Fix net_amount double-deducting GST on Razorpay gateway fees#27
TheShahnawaaz wants to merge 1 commit into
mainfrom
fix/razorpay-gateway-fee-net-amount

Conversation

@TheShahnawaaz
Copy link
Copy Markdown
Owner

Root Cause

Razorpay's payment API returns two fields in the payment object:

  • fee — the total amount deducted from settlement, which is base_fee + GST (all-in)
  • tax — the GST portion only, which is already contained within fee

The previous code treated tax as an additional deduction on top of fee:

// Before — GST counted twice
net_amount = amount - fee - tax

// After — correct
net_amount = amount - fee

This meant every confirmed payment stored a net_amount that was understated by exactly the tax value (22 paise per ₹60 payment). Every financial report showed a lower "Net received" than what Razorpay actually settled.

Example (29-payment trip):

Amount
Gross collected ₹1,740.00
Razorpay deduction (fee) ₹41.18
Razorpay settled (correct) ₹1,698.82
Previously reported net ₹1,692.44
Discrepancy ₹6.38 (= total GST across 29 payments)

Changes

backend/src/services/payment/payment.service.ts

Fixed net_amount calculation in all three code paths:

  • Payment verification flow (frontend-initiated)
  • Webhook handler: pending → confirmed
  • Webhook handler: already confirmed (fill-in update)

backend/migrations/021_create_reports_module.sql

  • report_financials view: fixed the NULL fallback formula from amount - (fee + tax) / 100 to amount - fee / 100
  • reports_summary view: fixed total_gateway_deductions from SUM(gateway_fees + gateway_tax) to SUM(gateway_fees) — was double-counting GST in the summary

backend/migrations/014_add_payment_fee_columns.sql

Corrected the net_amount column comment from amount - fee - tax to amount - fee so future developers don't reintroduce the same bug.

frontend/src/pages/admin/reports/ReportDetailPage.tsx

Fixed the "Gateway fees" display line from gateway_fees + gateway_tax to gateway_fees only, making the on-screen breakdown self-consistent:

Online Payments    ₹1,740.00
Gateway fees         -₹41.18   ← actual Razorpay deduction
Net received       ₹1,698.82   ← matches Razorpay dashboard

Notes

  • Historical payment records are intentionally left unchanged
  • gateway_tax column is still stored for GST accounting/tax records — it is just no longer subtracted from net_amount
  • On a fresh database with all migrations applied, all calculations will be correct from the first transaction

Razorpay's payment object returns two fields:
- `fee`: total amount deducted from settlement (base fee + GST, inclusive)
- `tax`: the GST portion, which is already contained within `fee`

The previous calculation subtracted both, causing net_amount to be
understated by the GST amount on every confirmed payment:

  Before: net_amount = amount - fee - tax  (GST counted twice)
  After:  net_amount = amount - fee        (correct)

This made every financial report show a lower "Net received" than
what Razorpay actually settled. The discrepancy per trip was exactly
equal to the sum of gateway_tax across all confirmed payments.

Changes:
- payment.service.ts: fix net_amount calculation in all three paths
  (verify flow, webhook pending→confirmed, webhook already-confirmed)
- 021_create_reports_module.sql: fix fallback formula in report_financials
  view for payments where net_amount is NULL, and fix total_gateway_deductions
  in reports_summary view (was summing gateway_fees + gateway_tax, double-counting)
- 014_add_payment_fee_columns.sql: correct the net_amount column comment
- ReportDetailPage.tsx: fix "Gateway fees" display to show gateway_fees
  only (not gateway_fees + gateway_tax), making the breakdown consistent
  with the actual settlement deduction
Copilot AI review requested due to automatic review settings May 2, 2026 16:32
@vercel
Copy link
Copy Markdown

vercel Bot commented May 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cab-management-system Ready Ready Preview, Comment May 2, 2026 4:33pm
cab-management-system-b59a Ready Ready Preview, Comment May 2, 2026 4:33pm

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect “net received” calculations by removing a double-deduction of GST (tax) on Razorpay gateway fees across backend payment flows, reporting SQL views, and the admin report UI—aligning stored/reporting values with Razorpay settlements.

Changes:

  • Backend: compute net_amount as amount - fee (since Razorpay fee already includes GST) in verification + webhook paths.
  • SQL: update report_financials and reports_summary views to stop subtracting/aggregating gateway_tax on top of gateway_fee.
  • Frontend: display “Gateway fees” as gateway_fees only (no longer gateway_fees + gateway_tax).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
frontend/src/pages/admin/reports/ReportDetailPage.tsx Updates gateway fees line item to match the corrected definition of deductions (avoid adding GST twice).
backend/src/services/payment/payment.service.ts Fixes net_amount calculation in verify + webhook flows to avoid double-deducting GST.
backend/migrations/021_create_reports_module.sql Fixes report views’ fallback and summary aggregations to avoid double-counting GST in deductions/net revenue.
backend/migrations/014_add_payment_fee_columns.sql Updates net_amount column documentation to reflect the corrected calculation.

Comment on lines 15 to 16
COMMENT ON COLUMN payments.gateway_fee IS 'Payment gateway fee in paise';
COMMENT ON COLUMN payments.gateway_tax IS 'GST on gateway fee in paise';
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.

2 participants