Skip to content

feat(finance): Build Production-Grade Revenue Split Engine with Platform Fee, GST, Cashfree Charges ; Role-Based Settlement SystemΒ #25

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

πŸ“Œ Objective

Build a financial-grade revenue distribution infrastructure for CommDesk where:

  • Any community can earn revenue

  • Revenue automatically splits across community roles

  • CommDesk platform fee is deducted automatically

  • Cashfree payment gateway charges are deducted

  • GST calculations are handled properly

  • Settlement accounting is audit-safe

  • Community members receive net earnings

  • Payout-ready internal wallets are maintained

This system should behave similar to:


🎯 Core Goals

The system must support:

  • Platform fee deduction

  • GST calculations

  • Cashfree charge deduction

  • Dynamic revenue splitting

  • Role-based earnings

  • Community settlements

  • Internal earning wallets

  • Financial-safe accounting

  • Tax-ready ledger system

  • Audit-safe payout infrastructure


πŸ’° Financial Flow

Before ANY community distribution:

Customer Payment
      ↓
GST Deduction
      ↓
Cashfree Charges
      ↓
Platform Fee
      ↓
Net Community Revenue
      ↓
Role-Based Split
      ↓
Wallet Distribution

🧠 Example Revenue Calculation


Example 1 β€” Event Revenue

Customer Pays = β‚Ή10,000

GST (18%) = β‚Ή1,800

Remaining = β‚Ή8,200

Cashfree Charges:
2% + GST = β‚Ή193

Remaining = β‚Ή8,007

CommDesk Platform Fee:
10% = β‚Ή800

Net Community Revenue:
β‚Ή7,207


Role Distribution

Owner = 50% = β‚Ή3,603.5

Admin Team = 20% = β‚Ή1,441.4

Moderator Team = 10% = β‚Ή720.7

Event Team = 20% = β‚Ή1,441.4


Example 2 β€” Sponsor Revenue

Sponsor Payment = β‚Ή50,000

GST = β‚Ή9,000

Remaining = β‚Ή41,000

Cashfree Fee = β‚Ή968

Remaining = β‚Ή40,032

Platform Fee (12%) = β‚Ή4,803

Community Pool = β‚Ή35,229


πŸ—οΈ Revenue Split Architecture

Customer Payment
↓
Cashfree Verification
↓
Financial Calculation Engine
↓
GST Deduction
↓
Gateway Charge Deduction
↓
Platform Fee Deduction
↓
Net Revenue Pool
↓
Role Distribution Engine
↓
Role Wallet Credit
↓
Settlement Queue

🧱 Backend Stack


Runtime

Node.js + TypeScript

Framework

Preferred:


Database

Preferred:


ORM

Preferred:


Queue

Preferred:


Cache


Payment Provider


πŸ“¦ Required Packages


Core

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

Queue

npm install amqplib bullmq ioredis

Cashfree

npm install cashfree-pg

Security

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

πŸ—„οΈ Core Database Schemas


Community Wallet

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

communityId String @unique

availableBalance Int @default(0)

lockedBalance Int @default(0)

pendingSettlement Int @default(0)

totalGrossRevenue BigInt @default(0)

totalPlatformFees BigInt @default(0)

totalGatewayFees BigInt @default(0)

totalGSTCollected BigInt @default(0)

totalNetRevenue BigInt @default(0)

totalWithdrawn BigInt @default(0)

createdAt DateTime @default(now())

updatedAt DateTime @updatedAt
}


Revenue Transaction Ledger

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

communityId String

sourceType RevenueSourceType

sourceId String

grossAmount Int

gstAmount Int

paymentGatewayFee Int

paymentGatewayGST Int

platformFee Int

platformFeeGST Int

netRevenue Int

metadata Json?

idempotencyKey String @unique

createdAt DateTime @default(now())

@@index([communityId])

@@index([createdAt])
}


Role Distribution Table

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

transactionId String

userId String

roleId String

roleName String

percentage Float

amount Int

createdAt DateTime @default(now())

@@index([transactionId])

@@index([userId])
}


Platform Revenue Table

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

transactionId String

amount Int

gstAmount Int

feePercentage Float

createdAt DateTime @default(now())
}


πŸ’Έ Financial Calculation Engine


Required Flow

Gross Revenue
↓
GST Extraction
↓
Cashfree Charges
↓
Cashfree GST
↓
CommDesk Platform Fee
↓
Platform GST
↓
Net Revenue Pool
↓
Role Distribution

Required Service Methods

calculateGST()

calculateGatewayFees()

calculatePlatformFee()

calculateNetRevenue()

splitRevenueAcrossRoles()

creditRoleWallets()

createLedgerEntries()


⚑ Dynamic Fee System


Admin Configurable

Setting Example
Platform Fee 5–20%
Sponsor Fee Custom
Event Fee Custom
Premium Community Fee Custom
GST Percentage 18%

Queue Rules

Required:

  • Retry support

  • Exponential backoff

  • Dead-letter queues

  • Delayed retries

  • Idempotent consumers


πŸ“Š Analytics Requirements

Track:

  • Gross revenue

  • Net revenue

  • Platform earnings

  • Gateway charges

  • GST collected

  • Community earnings

  • Top earning communities

  • Payout volume


πŸ“ˆ Dashboard Features

Users should see:

  • Gross revenue

  • Platform fee breakdown

  • Cashfree fee breakdown

  • GST deductions

  • Net earnings

  • Revenue split analytics

  • Wallet history

  • Monthly reports


πŸ§ͺ Testing Requirements


Unit Tests

Test:

  • GST calculations

  • Platform fee calculations

  • Gateway fee calculations

  • Revenue splitting

  • Settlement generation


Integration Tests

Test:

  • Cashfree integration

  • Revenue distribution

  • Wallet synchronization

  • Queue processing


Failure Tests

Test:

  • Duplicate payouts

  • Queue crash

  • DB deadlock

  • Cashfree downtime

  • Partial transaction failures


Load Testing

Simulate:

100K financial split events/min

πŸ“ˆ Monitoring & Alerts


Metrics

Track:

  • Revenue throughput

  • Settlement delays

  • Failed payouts

  • Fraud attempts

  • Queue lag

  • Wallet inconsistencies


Alerts

Trigger alerts for:

  • Failed settlements

  • High fraud activity

  • Queue failures

  • Negative balances

  • Payment inconsistencies


πŸ—‚οΈ Suggested Folder Structure

src/
 β”œβ”€β”€ modules/
 β”‚    β”œβ”€β”€ revenue-engine/
 β”‚    β”œβ”€β”€ gst-engine/
 β”‚    β”œβ”€β”€ gateway-fees/
 β”‚    β”œβ”€β”€ settlements/
 β”‚    β”œβ”€β”€ payouts/
 β”‚    β”œβ”€β”€ role-wallets/
 β”‚    β”œβ”€β”€ fraud-detection/
 β”‚    └── finance-analytics/
 β”‚
 β”œβ”€β”€ queues/
 β”œβ”€β”€ telemetry/
 β”œβ”€β”€ middleware/
 β”œβ”€β”€ prisma/
 β”œβ”€β”€ common/
 └── tests/

βœ… Acceptance Criteria

  • Platform fee deduction implemented

  • GST calculations implemented

  • Cashfree charge deduction implemented

  • Role-based revenue splitting implemented

  • Immutable ledger implemented

  • Settlement system implemented

  • Cashfree payout integration working

  • Queue-driven architecture enabled

  • Fraud prevention active

  • Full observability setup

  • Production-grade testing completed

  • Horizontally scalable architecture supported


πŸ“Œ Priority

P0 β€” Financial Infrastructure Core

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions