Parse and validate merchant webhook payloads

Last updated: August 6, 2025

Quick Overview

Design a function that parses and validates merchant webhook payloads by verifying HMAC signatures and extracting relevant information from JSON payloads based on specified event types. The function should return structured event objects if the payload is valid, or an error message if validation fails. Ensure that the implementation handles various event types with their required fields appropriately.

Affirm
Coding & Algorithms
Software Engineer
Affirm
August 6, 2025
Software Engineer
Onsite Coding Round
Coding & Algorithms
Medium

10

11

2,803 solved


Design a function that parses and validates merchant webhook payloads by verifying HMAC signatures and extracting relevant information from JSON payloads based on specified event types. The function should return structured event objects if the payload is valid, or an error message if validation fails. Ensure that the implementation handles various event types with their required fields appropriately.

Webhook processing is a daily task at Affirm. This problem tests your ability to write defensive code that handles untrusted input, validates security signatures, and processes different event types cleanly in Python.

What the Interviewer Expects
  • Implement HMAC-SHA256 signature verification correctly
  • Parse JSON payloads defensively with proper error handling for malformed input
  • Use a clean pattern to handle multiple event types (factory or registry pattern)
  • Validate required fields per event type and return clear error messages
  • Write code that is secure against common webhook vulnerabilities
Key Topics to Cover
HMAC signature verification
JSON parsing and validation
Factory and registry patterns
Security: timing-safe comparison, replay prevention
Defensive programming
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you protect against replay attacks using webhook timestamps?
  • How would you handle webhook payload schema versioning?
  • What if the same webhook is delivered twice? How do you ensure idempotent processing?
  • How would you handle a webhook event type that your system does not recognize?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires parsing and validating webhook payloads from merchants by verifying HMAC signatures and extracting relevant event information from JSON. This involves multiple components: signatu...

Approach
  1. HMAC Signature Verification: Use HMAC with SHA-256 to verify the signature against the payload. This ensures that the payload wasn't tampered with.
  2. Parse JSON Payload: Safely parse the...

Submit Your Answer
Markdown supported

Related Questions