0
0
LLDsystem_design~7 mins

Cancellation and refund policy in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Users often cancel orders or subscriptions, and without a clear policy, the system may incorrectly process refunds or fail to handle cancellations consistently. This can lead to financial losses, customer dissatisfaction, and disputes due to unclear or inconsistent refund handling.
Solution
Implement a well-defined cancellation and refund policy as part of the system's business logic. This policy enforces rules on when cancellations are allowed, how refunds are calculated, and how to handle partial or full refunds. The system validates cancellation requests against these rules and processes refunds accordingly to ensure fairness and consistency.
Architecture
User Request
(Cancel/Refund)
Cancellation
Validation
& Rules Check

This diagram shows the flow from a user's cancellation or refund request through the policy engine that validates and applies rules, then processes the refund via the payment gateway.

Trade-offs
✓ Pros
Ensures consistent and fair handling of cancellations and refunds.
Reduces financial risk by enforcing business rules on refunds.
Improves customer trust with transparent and automated refund processing.
Simplifies dispute resolution by having clear, codified policies.
✗ Cons
Adds complexity to the order management system with additional validation logic.
Requires careful maintenance to keep policies up to date with business changes.
May delay refund processing if rules are complex or require manual review.
Use when your system handles paid orders or subscriptions with potential cancellations, especially if refund amounts vary by timing or conditions. Recommended for systems with over 1000 transactions daily to automate and scale refund handling.
Avoid if your system only handles free services or non-refundable transactions, or if cancellations are extremely rare (less than 10 per month), where manual handling is simpler.
Real World Examples
Amazon
Amazon enforces cancellation and refund policies that vary by product category and timing, automating refunds only when conditions are met to reduce fraud and errors.
Airbnb
Airbnb uses cancellation policies that define refund eligibility based on how far in advance a booking is canceled, automating partial or full refunds accordingly.
Uber
Uber applies cancellation fees or refunds based on timing and driver status, enforcing policies to balance driver compensation and rider satisfaction.
Code Example
The before code processes refunds without any rules, always refunding full amount if cancelled. The after code introduces a CancellationPolicy class that calculates refunds based on timing, allowing full refunds within 24 hours and partial refunds afterward. This enforces business rules consistently.
LLD
### Before: No policy enforcement, refunds processed directly
class Order:
    def __init__(self, amount):
        self.amount = amount
        self.is_cancelled = False

    def cancel(self):
        self.is_cancelled = True

    def refund(self):
        if self.is_cancelled:
            return self.amount  # Full refund without checks
        return 0


### After: Policy enforced with timing and partial refund rules
from datetime import datetime, timedelta

class CancellationPolicy:
    def __init__(self, full_refund_period_hours, partial_refund_percent):
        self.full_refund_period = timedelta(hours=full_refund_period_hours)
        self.partial_refund_percent = partial_refund_percent

    def calculate_refund(self, order_time, cancel_time, amount):
        if cancel_time - order_time <= self.full_refund_period:
            return amount  # Full refund
        else:
            return amount * self.partial_refund_percent / 100  # Partial refund

class OrderWithPolicy:
    def __init__(self, amount, order_time):
        self.amount = amount
        self.order_time = order_time
        self.is_cancelled = False
        self.cancel_time = None
        self.policy = CancellationPolicy(full_refund_period_hours=24, partial_refund_percent=50)

    def cancel(self, cancel_time):
        self.is_cancelled = True
        self.cancel_time = cancel_time

    def refund(self):
        if not self.is_cancelled:
            return 0
        return self.policy.calculate_refund(self.order_time, self.cancel_time, self.amount)
OutputSuccess
Alternatives
Manual Refund Processing
Refunds and cancellations are handled manually by customer service agents without automated policy enforcement.
Use when: Choose when transaction volume is low and refund cases are rare, making automation cost-ineffective.
Fixed Refund Policy
A simple policy that always refunds full or no amount without conditional rules or timing considerations.
Use when: Choose when simplicity is prioritized over flexibility, such as for low-value transactions.
Summary
Cancellation and refund policies prevent inconsistent or unfair refund processing.
They enforce business rules to calculate refunds based on timing and conditions.
Automating these policies improves customer trust and reduces financial risk.