Skip to content

feat: CommDesk Billing & Credit Economy Backend Infrastructure #24

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

use neon for postgress sql

📌 Objective

Build a complete enterprise-grade financial infrastructure for CommDesk capable of handling:

  • UPI payments
  • Wallet funding
  • Credit system
  • Community monetization
  • Platform fee deduction
  • Refunds
  • Sponsor payments
  • Partner settlements
  • Auto recharge
  • Billing
  • Financial analytics
  • Fraud prevention
  • Queue-driven processing
  • Audit-safe accounting
  • Scalable SaaS monetization

The system must be designed similar to:

  • Stripe Billing
  • RazorpayX
  • AWS Marketplace Billing
  • Discord Billing
  • Slack Billing
  • Shopify Payments

🧠 Final Architecture Decision

Database Strategy

CommDesk will use a hybrid database architecture.


✅ PostgreSQL (Primary Financial Database)

Used for:

  • Payments
  • Wallets
  • Credits
  • Ledger
  • Billing
  • Refunds
  • Invoices
  • Financial analytics
  • Settlements
  • Audit logs

✅ MongoDB (Existing Flexible Database)

Used for:

  • Communities
  • Events
  • Posts
  • Chats
  • Activity feeds
  • Flexible metadata
  • Non-financial analytics

❌ Important Rule

NEVER store critical wallet balances
or financial ledger logic only in MongoDB.

🏆 Final Recommended Stack

Layer Technology
Backend NestJS
Financial Database PostgreSQL
PostgreSQL Hosting Neon
Existing Database MongoDB Atlas
ORM Prisma
Payment Gateway Cashfree
Queue System RabbitMQ
Cache Redis
Redis Hosting Upstash
File Storage Bunny.net
Monitoring Grafana
Metrics Prometheus
Error Tracking Sentry
Tracing OpenTelemetry
Containerization Docker
Reverse Proxy Traefik
CI/CD GitHub Actions

☁️ Cloud Providers

PostgreSQL

Neon

https://neon.tech

Required Features:

  • Serverless PostgreSQL
  • Connection pooling
  • SSL support
  • Branching
  • Monitoring dashboard
  • Auto scaling
  • Prisma compatibility

MongoDB

MongoDB Atlas

https://mongodb.com/atlas

Redis

Upstash

https://upstash.com

RabbitMQ

CloudAMQP

https://cloudamqp.com

💳 Payment Gateway

Cashfree Payments

https://cashfree.com

Required Support:

  • UPI
  • Cards
  • Netbanking
  • Wallets
  • Refunds
  • Webhooks
  • Split settlements
  • Vendor payouts
  • Auto settlement
  • Payment verification
  • Subscription-ready APIs

💰 Billing Model

CommDesk uses a credit system.

₹10 = 100 Credits

Recharge Packs

Recharge Credits
₹150 1500
₹300 3200
₹500 5500
₹1000 12000

🏗️ High-Level Architecture

                           ┌────────────────────┐
                           │      Frontend      │
                           └─────────┬──────────┘
                                     │
                                     ▼
                           ┌────────────────────┐
                           │    API Gateway     │
                           └─────────┬──────────┘
                                     │
         ┌───────────────────────────┼───────────────────────────┐
         ▼                           ▼                           ▼

┌──────────────────┐     ┌──────────────────┐      ┌──────────────────┐
│ Payment Service  │     │ Community API    │      │ Analytics API    │
└────────┬─────────┘     └────────┬─────────┘      └────────┬─────────┘
         │                        │                         │
         ▼                        ▼                         ▼

┌──────────────────┐     ┌──────────────────┐      ┌──────────────────┐
│ PostgreSQL       │     │ MongoDB Atlas    │      │ MongoDB          │
│ Financial DB     │     │ Community Data   │      │ Analytics        │
└──────────────────┘     └──────────────────┘      └──────────────────┘

         ▼
┌──────────────────┐
│ RabbitMQ         │
│ Queue System     │
└──────────────────┘

         ▼
┌──────────────────┐
│ Redis Cache      │
└──────────────────┘

🔥 Core Financial Rules

NEVER

Trust frontend payment success response

NEVER

Grant credits directly inside webhook controller

NEVER

Update wallet balance without transaction locking

ALWAYS

Verify payment using server-side verification

ALWAYS

Use queue-driven payment processing

ALWAYS

Write immutable ledger entries

ALWAYS

Use atomic database transactions

🔒 Financial Safety Requirements

Required:

  • ACID transactions
  • Immutable ledger
  • Double-spend prevention
  • Row-level locking
  • Replay-safe webhooks
  • Idempotency keys
  • Queue-driven processing
  • Financial reconciliation
  • Fraud scoring
  • Duplicate prevention
  • Distributed tracing
  • Audit-safe logging

🗄️ Core Database Tables

Required tables:

users
wallets
wallet_transactions
ledger_entries
payments
payment_attempts
refunds
invoices
billing_events
subscriptions
usage_metering
community_settlements
fraud_events
audit_logs
payment_webhooks

🗃️ Prisma Schema Requirements

Payment Model

model Payment {
  id                    String   @id @default(uuid())

  userId                String

  orderId               String   @unique

  paymentSessionId      String?

  cashfreeOrderId       String?

  cashfreePaymentId     String?

  amount                Int

  creditsGranted        Int

  gstAmount             Int

  platformFee           Int

  currency              String   @default("INR")

  status                PaymentStatus

  paymentMethod         String?

  paymentGroup          String?

  customerPhone         String?

  customerEmail         String?

  metadata              Json?

  idempotencyKey        String   @unique

  webhookVerified       Boolean  @default(false)

  refunded              Boolean  @default(false)

  refundAmount          Int?

  fraudScore            Float?

  retryCount            Int      @default(0)

  createdAt             DateTime @default(now())

  updatedAt             DateTime @updatedAt

  @@index([userId])

  @@index([status])

  @@index([createdAt])

  @@map("payments")
}

Wallet Model

model Wallet {
  id                       String   @id @default(uuid())

  ownerId                  String

  availableCredits         Int      @default(0)

  lockedCredits            Int      @default(0)

  pendingCredits           Int      @default(0)

  lifetimePurchasedCredits BigInt   @default(0)

  autoRechargeEnabled      Boolean  @default(false)

  autoRechargeThreshold    Int      @default(200)

  createdAt                DateTime @default(now())

  updatedAt                DateTime @updatedAt

  transactions             CreditTransaction[]
}

Immutable Ledger

model CreditTransaction {
  id                 String   @id @default(uuid())

  walletId           String

  transactionType    CreditTransactionType

  credits            Int

  balanceBefore      Int

  balanceAfter       Int

  source             String

  sourceId           String?

  metadata           Json?

  idempotencyKey     String   @unique

  createdAt          DateTime @default(now())

  wallet             Wallet @relation(fields: [walletId], references: [id])

  @@index([walletId])

  @@index([createdAt])
}

⚡ Core Services

Payment Service

Responsible for:

  • Order creation
  • Payment session generation
  • Payment verification
  • Refund handling
  • Payment retries
  • Settlement tracking
  • Status synchronization

Required Methods

createPaymentOrder()

createPaymentSession()

verifyPayment()

syncPaymentStatus()

refundPayment()

handleWebhook()

generateInvoice()

retryFailedPayment()

Wallet Service

Responsible for:

  • Credit grant
  • Credit consumption
  • Balance locking
  • Auto recharge
  • Refund credit reversal

Required Methods

grantCredits()

consumeCredits()

refundCredits()

lockCredits()

releaseCredits()

syncBalance()

Ledger Service

Responsible for:

  • Immutable ledger writes
  • Financial reconciliation
  • Audit tracking
  • Transaction history

Fraud Service

Responsible for:

  • Fraud scoring
  • Duplicate payment detection
  • Abuse detection
  • Rate abuse tracking
  • Risk profiling

💳 Payment Flow

Order Creation

Frontend Request
   ↓
Backend Validation
   ↓
Create Payment Record
   ↓
Create Cashfree Order
   ↓
Generate Payment Session
   ↓
Return Hosted Payment Session

Payment Success Flow

Cashfree Webhook
   ↓
Verify Signature
   ↓
Verify Payment Status
   ↓
Push Event To Queue
   ↓
Grant Credits
   ↓
Create Ledger Entry
   ↓
Update Wallet
   ↓
Trigger Analytics
   ↓
Send Notification

🚨 Critical Rule

NEVER

Grant credits directly inside webhook controller

ALWAYS

Publish successful payment event to queue

⚡ Queue Architecture

Required Queues

Queue Purpose
payment.processing Payment verification
wallet.credit Wallet credit grant
wallet.debit Credit consumption
refund.processing Refund workflows
analytics.billing Billing analytics
fraud.detection Fraud analysis
webhook.processing Webhook handling
invoice.generate Invoice generation
notifications.billing Notifications
settlements.processing Community settlements

Queue Requirements

Required:

  • Retry support
  • Exponential backoff
  • Dead-letter queues
  • Idempotent consumers
  • Queue monitoring
  • Delayed jobs
  • Queue recovery

🔄 Auto Recharge System

Flow

Low Wallet Balance
        ↓
Trigger Recharge Event
        ↓
Create Cashfree Order
        ↓
Successful Payment
        ↓
Grant Credits

Requirements

  • Configurable thresholds
  • Retry support
  • Queue-based processing
  • Failure alerts
  • Usage monitoring

🔒 Security Requirements

Payment Security

Required:

  • Cashfree signature verification
  • Idempotency keys
  • Replay attack prevention
  • Fraud scoring
  • Duplicate prevention
  • IP validation
  • Device tracking
  • Secure webhook verification

Backend Security

Required:

  • JWT auth
  • RBAC
  • CSRF protection
  • HPP protection
  • Abuse prevention
  • Rate limiting
  • API throttling

⚙️ Infrastructure Requirements

Docker

Required:

  • Dockerized services
  • Multi-stage builds
  • Production images
  • Health checks

CI/CD

Required:

  • GitHub Actions
  • Automated testing
  • Prisma migrations
  • Docker builds
  • Vulnerability scanning

Observability

Required:

Tool Purpose
Grafana Dashboard
Prometheus Metrics
Loki Logs
Sentry Error tracking
OpenTelemetry Distributed tracing

📊 Analytics Requirements

Track:

  • Payment success rate
  • Failed payments
  • Recharge trends
  • Revenue analytics
  • Fraud attempts
  • Refund spikes
  • Queue lag
  • Webhook failures
  • Wallet inconsistencies
  • Auto recharge usage

🚨 Alerting Requirements

Trigger alerts for:

  • Failed webhooks
  • Queue stuck
  • Fraud spikes
  • Negative balances
  • High refund spikes
  • PostgreSQL connection exhaustion
  • Redis outage
  • RabbitMQ failures

🧪 Production Testing Requirements

Unit Tests

Test:

  • Credit calculation
  • GST calculation
  • Wallet logic
  • Ledger safety
  • Refund logic
  • Idempotency logic

Integration Tests

Test:

  • Cashfree integration
  • Queue processing
  • Webhook verification
  • PostgreSQL transactions
  • Redis integration
  • RabbitMQ processing

Failure Tests

Simulate:

  • Duplicate webhooks
  • Payment replay attacks
  • Queue failures
  • PostgreSQL deadlocks
  • Redis outage
  • RabbitMQ outage
  • Fraud requests
  • API abuse

Load Testing

Simulate:

50K payments/minute
10M wallet transactions
1M webhook events/day

📁 Suggested Folder Structure

src/
 ├── modules/
 │    ├── payments/
 │    ├── billing/
 │    ├── wallet/
 │    ├── ledger/
 │    ├── fraud/
 │    ├── analytics/
 │    ├── invoices/
 │    ├── settlements/
 │    ├── auto-recharge/
 │    ├── webhooks/
 │    └── notifications/
 │
 ├── queues/
 ├── prisma/
 ├── telemetry/
 ├── middleware/
 ├── infrastructure/
 ├── common/
 ├── config/
 └── tests/

📦 Required Packages

Core

npm install @prisma/client prisma
npm install zod uuid dotenv dayjs
npm install helmet compression cors
npm install pino pino-pretty

Payments

npm install cashfree-pg

Queue

npm install amqplib bullmq ioredis

Monitoring

npm install @sentry/node
npm install prom-client
npm install @opentelemetry/api

Security

npm install express-rate-limit
npm install rate-limiter-flexible
npm install csurf hpp

✅ Acceptance Criteria

  • PostgreSQL financial infrastructure operational
  • Neon database integrated
  • MongoDB hybrid architecture implemented
  • Cashfree integration complete
  • UPI payment flow operational
  • Queue-driven processing active
  • Immutable ledger implemented
  • Replay-safe webhooks implemented
  • Fraud prevention enabled
  • Auto recharge operational
  • Refund engine operational
  • Production monitoring active
  • Observability setup complete
  • Financial reconciliation system operational
  • Horizontal scaling supported

📌 Priority

P0 — Core Revenue Infrastructure

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions