Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Record Razorpay payments in billing summary#223

Merged
ishaanxgupta merged 2 commits into
mainfrom
fix/billing-payment-invoices
Jun 2, 2026
Merged

Record Razorpay payments in billing summary#223
ishaanxgupta merged 2 commits into
mainfrom
fix/billing-payment-invoices

Conversation

@ishaanxgupta
Copy link
Copy Markdown
Contributor

Summary

  • persist paid Razorpay payments as invoice records keyed by payment id
  • include recent invoices and dashboard-compatible billing fields in /api/billing/summary
  • carry checkout/webhook amount, currency, credits, and billing region into invoice rows
  • add billing tests for idempotent Pro invoice creation and top-up invoice visibility

Verification

  • .\.venv\Scripts\python.exe -m pytest tests\test_billing.py tests\api\test_billing_routes.py

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

Warnings
⚠️

📦 This PR changes 349 lines (additions + deletions). Large PRs are harder to review thoroughly — consider splitting it.

Messages
📖

📝 No CHANGELOG.md update detected. If this PR introduces a user-visible change, please add an entry.

📖

✅ Targeting main. Please squash commits before merging to keep the git history clean.

Generated by 🚫 dangerJS against 4b1dd83

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

✅ Staging Deployment Report

Item Value
Branch fix/billing-payment-invoices
Commit a58cf07
Environment Staging
Health http://3.6.255.148:8001/health
API Docs http://3.6.255.148:8001/docs
Smoke Tests success

🟢 Staging is live and healthy! Test your changes at the staging URL above.

Ready to ship? Comment /promote on this PR to merge to main and deploy to production.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

🔍 API Schema Diff

---REPORT---


Auto-generated by API Schema Diff workflow

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces payment invoice tracking and enhances the billing summary with detailed usage snapshots and invoice histories. Key changes include recording invoices during subscription and top-up grants, exposing these invoices in the billing summary, and adding database storage support. The code review identified several important issues: a missing MongoDB index on billing_account_id which could cause full collection scans, a recurring logical bug where an amount of 0 is incorrectly treated as None (potentially breaking free trials or 100% discounts), and an inaccurate current_usage calculation that fails to account for active top-up credits.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/billing/store.py
Comment thread src/api/routes/billing.py Outdated
Comment thread src/api/routes/billing.py Outdated
Comment thread src/api/routes/billing.py Outdated
Comment thread src/api/routes/billing.py Outdated
Comment thread src/api/routes/billing.py Outdated
Comment thread src/billing/service.py Outdated
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jun 2, 2026

Greptile Summary

This PR persists paid Razorpay payments as invoice records keyed by payment ID and extends /api/billing/summary to include recent invoices, a usage snapshot, and several dashboard-compatible billing fields (credit_balance, prepaid_balance_paise, account_status, last_payment_at).

  • record_payment_invoice is added to the store with idempotent upsert semantics, a new compound index, and an in-memory path for tests.
  • get_billing_summary now fetches invoices, computes current_usage from ledger debits since the period start, and derives credits_limit from the active plan.
  • Tests cover idempotent Pro invoice creation, top-up invoice visibility, zero-amount edge cases, and the zero-credit plan limit fix.

Confidence Score: 4/5

Safe to merge; invoice persistence and summary enrichment are additive and the idempotency story is sound.

The core payment recording logic is correct and idempotent. The one notable gap is that current_usage in the billing summary is computed from a 500-entry ledger fetch without a server-side date filter, so high-activity accounts could see an understated usage figure on the dashboard.

src/billing/service.py — the ledger fetch for current_month usage calculation.

Important Files Changed

Filename Overview
src/billing/service.py Adds invoice persistence calls after grant_pro_subscription and grant_topup, and extends get_billing_summary with invoice list, current_month usage, and several new fields; usage calculation uses a hardcoded 500-entry ledger cap that could undercount active accounts.
src/billing/store.py Adds record_payment_invoice (idempotent upsert keyed by razorpay_payment_id) and list_payment_invoices with a new compound index; index sort direction (ASCENDING) is opposite to query sort (DESC) but MongoDB traverses it correctly.
src/billing/types.py Adds UsageSnapshotPublic and PaymentInvoicePublic Pydantic models and extends BillingSummary with new optional fields; changes are additive and backward-compatible.
src/api/routes/billing.py Threads amount, currency, and billing_region from checkout/webhook events into grant calls; adds _minor_amount helper and stores additional fields on checkout creation.
tests/test_billing.py Adds four new tests covering idempotent invoice creation, top-up invoice visibility, zero-amount edge case, and zero-credit plan limit; clears new in-memory store in fixture.

Sequence Diagram

sequenceDiagram
    participant Client
    participant BillingRoute
    participant BillingService
    participant BillingStore
    participant MongoDB

    Client->>BillingRoute: POST /razorpay/verify-payment
    BillingRoute->>BillingStore: get_checkout(order/subscription_id)
    BillingStore-->>BillingRoute: checkout doc (amount, currency, billing_region)

    alt Pro subscription
        BillingRoute->>BillingService: grant_pro_subscription(amount, currency, billing_region)
        BillingService->>BillingStore: grant_credits (idempotent)
        BillingService->>BillingStore: record_payment_invoice(payment_id, payload)
        BillingStore->>MongoDB: find_one_and_update razorpay_payment_id (upsert)
    else Top-up
        BillingRoute->>BillingService: grant_topup(amount, currency, billing_region)
        BillingService->>BillingStore: grant_credits (idempotent)
        BillingService->>BillingStore: record_payment_invoice(payment_id, payload)
        BillingStore->>MongoDB: find_one_and_update razorpay_payment_id (upsert)
    end

    BillingRoute->>BillingService: get_billing_summary(user)
    BillingService->>BillingStore: list_payment_invoices(account_id)
    BillingStore->>MongoDB: "find billing_account_id + type=invoice, sort paid_at DESC"
    BillingService->>BillingStore: "list_ledger(account_id, limit=500)"
    BillingService-->>BillingRoute: BillingSummary (invoices, current_month, credit_balance)
    BillingRoute-->>Client: VerifyPaymentResponse
Loading

Fix All in Cursor Fix All in Codex Fix All in Claude Code

Reviews (2): Last reviewed commit: "Address billing invoice review feedback" | Re-trigger Greptile

Comment thread src/api/routes/billing.py Outdated
Comment thread src/billing/service.py Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

✅ Staging Deployment Report

Item Value
Branch fix/billing-payment-invoices
Commit 625a6dd
Environment Staging
Health http://3.6.255.148:8001/health
API Docs http://3.6.255.148:8001/docs
Smoke Tests success

🟢 Staging is live and healthy! Test your changes at the staging URL above.

Ready to ship? Comment /promote on this PR to merge to main and deploy to production.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

🔍 API Schema Diff

---REPORT---


Auto-generated by API Schema Diff workflow

@ishaanxgupta ishaanxgupta merged commit f556419 into main Jun 2, 2026
17 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant