Skip to content

Pricing Rules

Line Item Pricing

Each product line applies volume-based discounts:

Quantity Discount
1–9 0%
10–49 5%
50–99 10%
100+ 15%

Contract

calculate_line_price

Calculate the price for a single order line. Fetches the product from the catalog and applies quantity-based pricing.

Inputs
ParameterTypeDefault
product_idstr
quantityint
Output
returnLinePriceResult
LinePriceResult Pricing result for a single order line.
ParameterTypeDefault
product *str
unit_price *float
quantity *int
volume_discount_pct *float
line_total *float

Source Code

def calculate_line_price(product_id: str, quantity: int) -> LinePriceResult:
    """Calculate the price for a single order line.

    Fetches the product from the catalog and applies quantity-based pricing.
    """
    product = FetchProductCatalog().call(product_id)
    unit_price = product.base_price

    # Volume discount tiers
    if quantity >= 100:
        discount = 0.15
    elif quantity >= 50:
        discount = 0.10
    elif quantity >= 10:
        discount = 0.05
    else:
        discount = 0.0

    discounted_price = round(unit_price * (1 - discount), 2)

    return LinePriceResult(
        product=product.name,
        unit_price=unit_price,
        quantity=quantity,
        volume_discount_pct=discount * 100,
        line_total=round(discounted_price * quantity, 2),
    )

Try it Live

Playground — calculate_line_price

Calculate the price for a single order line. Fetches the product from the catalog and applies quantity-based pricing.

View source code
def calculate_line_price(product_id: str, quantity: int) -> LinePriceResult:
    """Calculate the price for a single order line.

    Fetches the product from the catalog and applies quantity-based pricing.
    """
    product = FetchProductCatalog().call(product_id)
    unit_price = product.base_price

    # Volume discount tiers
    if quantity >= 100:
        discount = 0.15
    elif quantity >= 50:
        discount = 0.10
    elif quantity >= 10:
        discount = 0.05
    else:
        discount = 0.0

    discounted_price = round(unit_price * (1 - discount), 2)

    return LinePriceResult(
        product=product.name,
        unit_price=unit_price,
        quantity=quantity,
        volume_discount_pct=discount * 100,
        line_total=round(discounted_price * quantity, 2),
    )
Mock configuration
Test cases
  • No test cases saved yet.

Order Total with Loyalty Discount

After computing line totals, the customer's loyalty tier discount is applied.

Contract

calculate_order_total

Calculate the total price for an order with loyalty discounts. Applies volume discounts per line, then the customer's loyalty discount on top.

Inputs
ParameterTypeDefault
customer_idstr
itemslist
Output
returnOrderTotal
OrderTotal Full order pricing breakdown.
ParameterTypeDefault
customer_tier *str
lines *list[LinePriceResult]
LinePriceResult Pricing result for a single order line.
ParameterTypeDefault
product *str
unit_price *float
quantity *int
volume_discount_pct *float
line_total *float
subtotal *float
loyalty_discount_pct *int
loyalty_savings *float
total *float

Execution Flow

Sequence diagram — calculate_order_total

sequenceDiagram
    participant calculate_order_total
    participant FetchCustomerTier as CustomerService / FetchCustomerTier
    calculate_order_total->>+FetchCustomerTier: customer_id
    FetchCustomerTier-->>-calculate_order_total: response

Source Code

def calculate_order_total(
    customer_id: str,
    items: list,
) -> OrderTotal:
    """Calculate the total price for an order with loyalty discounts.

    Applies volume discounts per line, then the customer's loyalty discount on top.
    """
    customer = FetchCustomerTier().call(customer_id)
    loyalty_discount = customer.discount_pct / 100

    subtotal = 0.0
    lines = []
    for item in items:
        line = calculate_line_price(item["product_id"], item["quantity"])
        lines.append(line)
        subtotal += line.line_total

    loyalty_savings = round(subtotal * loyalty_discount, 2)
    total = round(subtotal - loyalty_savings, 2)

    return OrderTotal(
        customer_tier=customer.tier,
        lines=lines,
        subtotal=subtotal,
        loyalty_discount_pct=customer.discount_pct,
        loyalty_savings=loyalty_savings,
        total=total,
    )

Try it Live

Playground — calculate_order_total

Calculate the total price for an order with loyalty discounts. Applies volume discounts per line, then the customer's loyalty discount on top.

View source code
def calculate_order_total(
    customer_id: str,
    items: list,
) -> OrderTotal:
    """Calculate the total price for an order with loyalty discounts.

    Applies volume discounts per line, then the customer's loyalty discount on top.
    """
    customer = FetchCustomerTier().call(customer_id)
    loyalty_discount = customer.discount_pct / 100

    subtotal = 0.0
    lines = []
    for item in items:
        line = calculate_line_price(item["product_id"], item["quantity"])
        lines.append(line)
        subtotal += line.line_total

    loyalty_savings = round(subtotal * loyalty_discount, 2)
    total = round(subtotal - loyalty_savings, 2)

    return OrderTotal(
        customer_tier=customer.tier,
        lines=lines,
        subtotal=subtotal,
        loyalty_discount_pct=customer.discount_pct,
        loyalty_savings=loyalty_savings,
        total=total,
    )
Mock configuration
Test cases
  • No test cases saved yet.