Commission Subscribers Reference
Commission Calculation Subscriber
commission-order-set-placed-handler
Automatically calculates and creates commission lines for all orders when an order set is placed. Event:order_set.placed (OrderSetWorkflowEvents.PLACED)Source:
packages/modules/commission/src/subscribers/commission-order-set-placed.ts
Functionality
When an order set is placed (typically when a customer completes checkout with items from multiple sellers), this subscriber:- Retrieves Order Set: Fetches the order set and all associated orders
- Iterates Through Orders: Processes each order in the set individually
- Identifies Sellers: For each order, retrieves the associated seller using the seller-order link
- Calculates Commission: Executes the
calculateCommissionWorkflowfor each order-seller pair - Skips Invalid Orders: Returns early if no seller is found for an order
Event Payload
Workflow Execution
For each order in the order set, the subscriber executes:- Determines applicable commission rules based on seller, product category, and product type
- Calculates commission amounts for each line item
- Creates commission line records in the database
Use Cases
- Automated Commission Tracking: Ensures every order has commission records created immediately
- Multi-Seller Orders: Handles orders from multiple sellers in a single transaction
- Commission Calculation: Applies the appropriate commission rules based on seller and product configuration
- Financial Reporting: Provides data for seller payouts and marketplace revenue tracking
Data Flow
Commission Calculation Logic
The workflow uses a priority-based rule selection:- Seller + Product Type: Most specific rule
- Seller + Product Category: Seller-specific category rule
- Seller: General seller rule
- Product Type: Marketplace-wide type rule
- Product Category: Marketplace-wide category rule
- Site Default: Fallback rule for all products
- Percentage Rate:
(item_price * percentage) / 100 - Flat Rate: Fixed amount from the price set
Error Handling
- Missing Seller: Returns early without throwing an error
- Workflow Failures: Handled by the workflow’s built-in retry and compensation mechanisms
- Invalid Data: Validated within the
calculateCommissionWorkflow
Performance Considerations
- Sequential Processing: Orders are processed one at a time to maintain data consistency
- Batch Potential: Could be optimized to process multiple orders in parallel if needed
- Database Queries: Uses GraphQL queries for efficient data retrieval
- Idempotency: Commission calculations are idempotent and can be safely retried
Notes
Event-Driven Commission Calculation
The Commission Module uses an event-driven approach for commission calculation: Advantages:- Automatic Execution: Commission is calculated without manual intervention
- Decoupled Architecture: Commission logic is separate from order placement logic
- Extensibility: Other modules can listen to the same event for additional processing
- Reliability: Event bus ensures the subscriber is executed even if there are temporary failures
- The subscriber listens to
order_set.placedrather thanorder.placed - This ensures commission is calculated after all orders in a multi-seller purchase are created
- Single-seller purchases also go through the order set workflow, so all orders are covered
Subscriber Configuration
The subscriber is configured with:- event: Specifies which event triggers this subscriber
- subscriberId: Unique identifier for tracking and debugging
- context: Optional metadata that can be used for filtering or monitoring
Integration with Payout System
Commission lines created by this subscriber are later used for:- Payout Calculation: The payout system reads commission lines to calculate seller earnings
- Financial Reports: Admin dashboards show commission breakdown by order
- Seller Statements: Sellers can view commission applied to their orders
- Revenue Tracking: Marketplace operators track total commission earned
- “Show me all commission for this seller”
- “What was the commission on this specific order?”
- “Calculate total marketplace revenue for this period”
Testing and Debugging
To test or debug commission calculation:- Check Event Emission: Verify that
order_set.placedevents are being emitted - Subscriber Execution: Check logs for the subscriber ID
- Commission Rules: Ensure appropriate commission rules exist
- Commission Lines: Query the
commission_linetable to verify records were created - Workflow Logs: Review workflow execution logs for detailed calculation steps