Sparki Polar.sh Integration Specification
Executive Summary
Sparki integrates Polar.sh as the payment processor and subscription management platform for all monetized tiers (Team, Pro, Enterprise). This specification defines the complete technical requirements for:- Checkout session creation (initiate purchases)
- Payment processing (collect payment via Polar)
- Webhook handling (synchronize subscription state)
- Subscription lifecycle management (renewals, cancellations, failures)
- Customer portal integration (invoice history, payment methods)
- Compliance & security (PCI DSS, fraud prevention, audit trails)
- Core API (Organization Access Tokens): Manage products, prices, checkouts, orders, subscriptions
- Customer Portal API (Customer Access Tokens): Allow customers to view their own orders/subscriptions
- Webhooks: Real-time events for subscription lifecycle :::
Part 1: Architecture Overview
Polar Integration Points
Part 2: Checkout Session Flow
REQ-POLAR-CHECKOUT-001: Create Checkout Session
Requirement: When user clicks “Upgrade to Team”, create Polar checkout session Implementation:REQ-POLAR-CHECKOUT-002: Create Reusable Checkout Link
Requirement: Generate shareable checkout links for upgrading (useful for teams, marketing, etc) Implementation:- Marketing page: “Upgrade to Team” button links to shareable checkout URL
- Email campaigns: “Team tier is 50% off” link
- Affiliate/referral: Track signups from partner links
Part 3: Webhook Integration
REQ-POLAR-WEBHOOK-001: Webhook Handler Setup
Requirement: Reliably receive and process Polar events Implementation:- Polar retries webhook delivery for 24 hours on failure
- 1-hour initial retry
- Exponential backoff: 1h → 2h → 4h → etc.
- Idempotency key prevents double-processing
REQ-POLAR-WEBHOOK-002: Order Created Event
Trigger: User completes payment, Polar creates order Webhook payload:REQ-POLAR-WEBHOOK-003: Subscription Updated Event
Trigger: Subscription renewal, plan change, or metadata update Webhook payload:REQ-POLAR-WEBHOOK-004: Subscription Canceled Event
Trigger: Customer cancels subscription or payment fails permanently Webhook payload:REQ-POLAR-WEBHOOK-005: Refund Event
Trigger: Customer requests refund or payment disputes Webhook payload:Part 4: Subscription State Machine
Subscription States
Part 5: Customer Portal Integration
REQ-POLAR-PORTAL-001: Create Customer Session
Requirement: Allow authenticated Sparki users to view their subscription details Implementation:REQ-POLAR-PORTAL-002: Fetch Customer Orders (Customer Portal API)
Requirement: Show invoice history and order details Implementation:- Display invoice download links
- Show payment method on file
- Display subscription renewal date
Part 6: Product & Price Management
REQ-POLAR-PRODUCTS-001: Define Sparki Products in Polar
Products to create:Part 7: Error Handling & Resilience
REQ-POLAR-ERROR-001: Checkout Failures
Failure modes:REQ-POLAR-ERROR-002: Webhook Failures
Idempotency pattern:- At-least-once delivery (may receive duplicate events)
- Exponential backoff retries (1h → 2h → 4h → 8h → etc.)
- 24-hour retry window
- Idempotency is essential
Part 8: Security & Compliance
REQ-POLAR-SECURITY-001: Secret Management
Secrets to protect:REQ-POLAR-SECURITY-002: PCI DSS Compliance
PCI requirements:REQ-POLAR-SECURITY-003: Fraud Prevention
Detection:Part 9: Testing Strategy
REQ-POLAR-TEST-001: Sandbox Environment
Polar provides sandbox:https://sandbox-api.polar.sh/v1
Testing checklist:
REQ-POLAR-TEST-002: Webhook Testing
Local testing:Part 10: Data Models
Subscription Database Schema
Payment Events Table
Part 11: Monitoring & Alerting
REQ-POLAR-MONITORING-001: Key Metrics
REQ-POLAR-MONITORING-002: Alerts
Conclusion
This specification defines a production-ready Polar.sh integration for Sparki’s tiered monetization system: ✅ Checkout flow: Seamless user upgrade experience ✅ Webhook handling: Reliable subscription synchronization ✅ State machine: Robust subscription lifecycle management ✅ Security: PCI compliance, fraud prevention, secret management ✅ Testing: Comprehensive sandbox testing strategy ✅ Monitoring: Real-time metrics and alerting The implementation prioritizes reliability (idempotent webhooks, retry logic, state verification) and user experience (quick checkouts, clear error messages, responsive customer portal).Document Statistics:
| Metric | Value |
|---|---|
| Polar API Endpoints | 15+ |
| Webhook Events | 5 major types |
| Subscription States | 9 |
| State Transitions | 12+ valid paths |
| Database Tables | 2 (subscriptions, payment_events) |
| Security Measures | 8+ (HMAC verification, secret rotation, audit logging, etc.) |
| Monitoring Metrics | 10+ (active subs, MRR, churn, latency, etc.) |
| Total Words | ~8,000 |
| Status | COMPLETE |
Document History:
| Version | Date | Author | Changes |
|---|---|---|---|
| 1.0 | 2025-12-03 | Sparki Engineering | Complete Polar integration spec |