Commission Module
In this section of the documentation, you will find resources to learn more about the Commission Module and how to use it in your application. Mercur has marketplace commission features available out-of-the-box through the Commission Module. A module is a standalone package that provides features for a single domain. Each of Mercur’s marketplace features are placed in custom modules, such as this Commission Module. Learn more about why modules are isolated in this documentation.Commission Features
- Flexible Commission Rules: Create commission rules based on seller, product type, product category, or combinations thereof.
- Priority-Based Rule Selection: Automatically select the most specific applicable commission rule using a priority system.
- Multiple Commission Types: Support for both percentage-based and flat-rate commissions with min/max limits.
- Tax-Inclusive Options: Calculate commissions with or without tax included.
- Automatic Calculation: Calculate commission for all line items when orders are placed.
- Line-Level Tracking: Store commission amount for each individual line item in an order.
How to Use the Commission Module
In your Medusa application, you build flows around Commerce Modules. A flow is built as a Workflow, which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism. You can build custom workflows and steps. Mercur provides pre-built workflows for commission operations in the@mercurjs/commission package.
For example, the calculateCommissionWorkflow:
src/workflows/commission/calculate-commission.ts
Subscriber
src/subscribers/commission-order-set-placed.ts
Concepts
In this document, you’ll learn about the main concepts related to commissions in Mercur.Commission Rule
A commission rule defines when and how marketplace commissions should be calculated. It is represented by the CommissionRule data model. A commission rule holds information about:- Rule name (for identification)
- Reference type (what the rule applies to)
- Reference ID (which specific entity)
- Active status (whether the rule is currently applied)
- Associated commission rate
Rule Reference Types
Commission rules support six reference types with a priority system for selecting the applicable rule:Priority Order (Highest to Lowest)
- seller+product_type: Most specific - applies to products of a specific type sold by a specific seller
- seller+product_category: Applies to products in a specific category sold by a specific seller
- seller: Applies to all products sold by a specific seller
- product_type: Applies to all products of a specific type regardless of seller
- product_category: Applies to all products in a specific category regardless of seller
- site: Default commission applied to all products (lowest priority)
Reference ID Format
- site: Empty string
"" - seller:
"sel_123" - product_type:
"ptyp_456" - product_category:
"pcat_789" - seller+product_type:
"sel_123+ptyp_456" - seller+product_category:
"sel_123+pcat_789"
Commission Rate
A commission rate defines how the commission amount is calculated. It is represented by the CommissionRate data model.Commission Types
Percentage-Based:Min/Max Limits
Commission rates can have minimum and maximum limits:Commission Line
A commission line represents the actual calculated commission for a specific order line item. It is represented by the CommissionLine data model. A commission line holds information about:- The order line item ID it applies to
- The commission rule that was used
- Currency code
- Calculated commission value
- Financial reporting
- Payout calculations (subtracting commission from seller payout)
- Analytics and insights
Automatic Commission Calculation
When an order set is placed, commission is calculated automatically: Process:OrderSetWorkflowEvents.PLACEDevent is emitted- Subscriber retrieves all orders in the order set
- For each order:
- Find the seller via
SELLER_ORDER_LINK - Execute
calculateCommissionWorkflow - Calculate commission for each line item
- Create commission line records
- Find the seller via
Commission Calculation Algorithm
For each line item in an order:- Build Context: Extract
seller_id,product_type_id,product_category_id - Select Rule: Call
selectCommissionForProductLinewith context - Apply Rule: Use the selected rule’s rate to calculate commission
- Apply Limits: Clamp result between min/max if specified
- Store Line: Create commission line record with calculated value
selectCommissionForProductLine method queries rules in priority order and returns the first match.
Tax Inclusivity
Theinclude_tax property determines whether commission is calculated on the pre-tax or post-tax amount:
Tax-Exclusive (include_tax: false)