Record Razorpay payments in billing summary#223
Conversation
|
✅ Staging Deployment Report
🟢 Staging is live and healthy! Test your changes at the staging URL above. Ready to ship? Comment |
🔍 API Schema Diff---REPORT--- Auto-generated by API Schema Diff workflow |
There was a problem hiding this comment.
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.
|
| 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
Reviews (2): Last reviewed commit: "Address billing invoice review feedback" | Re-trigger Greptile
✅ Staging Deployment Report
🟢 Staging is live and healthy! Test your changes at the staging URL above. Ready to ship? Comment |
🔍 API Schema Diff---REPORT--- Auto-generated by API Schema Diff workflow |
Summary
/api/billing/summaryVerification
.\.venv\Scripts\python.exe -m pytest tests\test_billing.py tests\api\test_billing_routes.py