← Stripe

Stripe System DesignDesignFree previewFree

Payment Webhook System

Last asked

onsiteSWE
SWEwebhookpaymentidempotencyretrycircuit-breakermessagingqueueingscalingsecurityhttp

Problem

You need to design a webhook service for a payment platform. The goal is to notify merchants whenever a payment event happens.

For example, when a customer pays, gets a refund, or disputes a charge, the platform must send this information to the merchant's server immediately.

Think about the workflow: an online store connects to a payment platform to handle money. The store needs to know right away when a payment succeeds so they can ship the product. They also need to know about refunds to update their stock. A webhook system does this by sending HTTP POST requests to a specific URL that the merchant sets up.

What the System Needs

Your system must handle three main things:

  • Merchant Registration: allow merchants to save a webhook URL and choose which types of events they want to receive.
  • Sending Events: when a payment event happens, send an HTTP POST request with the data to all registered webhooks.
  • Reliability: ensure the message gets delivered, even if the merchant's server is down for a short time.

Merchants save a new webhook endpoint with registerWebhook (the starter code). When an event happens, the system sends an HTTP POST request to the merchant's URL:

{
    "id": "evt_1234567890",
    "type": "payment.success",
    "created_at": "2024-12-15T10:30:00Z",
    "data": {
        "payment_id": "pay_abc123",
        "amount": 5000,
        "currency": "usd",
        "customer_id": "cus_xyz789"
    }
}

The merchant must reply with an HTTP 200 code to confirm they got the message.

Things to Think About

  • Delivery Guarantees: how do you make sure every message is delivered at least once? How should merchants handle it if they get the same message twice?
  • Retry Strategy: what do you do if the merchant's server is down? How many times do you try again, and for how long?
  • Latency: some merchant servers might take 10-15 seconds to reply. How do you handle this without slowing down your system?
  • Security: how can the merchant prove that the request really came from your platform?
  • Scale: the system might create millions of events every day. How do you handle this huge amount of traffic?

Starter code

WebhookRegistration registerWebhook(
    String merchantId,
    String callbackUrl,
    List<String> eventTypes  // Example: ["payment.success", "refund.created"]
)

Follow-ups

  • Idempotency: how do merchants safely process the same webhook twice without errors? What unique ID should they use?

  • Signature Verification: how do you sign the requests? Should you use Symmetric (HMAC) or Asymmetric (RSA) keys so merchants can check the signature?

  • Circuit Breaking: when do you stop trying to send data to a broken URL? How do you tell the merchant there is a problem?

  • Event Ordering: is it possible for events to arrive in the wrong order? How should the merchant handle that?

  • Endpoint Validation: how do you verify that the merchant actually owns the URL they entered before you send them sensitive payment data?

  • SSRF Prevention: how do you stop attackers from using your webhook system to hit your internal servers?

  • Observability: how do merchants see logs or debug why a delivery failed?

  • Backpressure: if one merchant has millions of stuck events, how do you make sure it doesn't slow down delivery for other merchants?