mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
Stories were written for a subscription SaaS model, but SurfSense is a self-hosted product with BYOK + optional PAYG page packs via Stripe. Key corrections: - Story 3.5: Not "remove BYOK + token billing" → actual gap is adding HTTP-layer quota pre-check before document upload enqueue - Story 5.1: Pricing UI already exists (Free/PAYG/Enterprise) → gap is wiring "Get Started" button to existing Stripe checkout endpoint - Story 5.2: mode=payment PAYG already works → needs verification/hardening not a subscription checkout rewrite - Story 5.3: Webhook already handles checkout.session.completed correctly → no subscription events needed, gap is idempotency test + purchase history UI - Story 5.4: PageLimitService + enforcement in tasks/connectors already exists → gap is HTTP-layer pre-check, quota UI indicator, and 402 frontend handling Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3.8 KiB
3.8 KiB
Story 5.2: Xác minh & Hardening Stripe PAYG Flow
Status: ready-for-dev
Context / Correction Note
⚠️ Story gốc bị sai hướng. Story gốc mô tả implement
mode: "subscription"Stripe Checkout. Thực tế, SurfSense dùng mô hình PAYG (Pay-As-You-Go page packs) vớimode: "payment"(one-time purchase). EndpointPOST /api/v1/stripe/create-checkout-sessionđã tồn tại và hoạt động. Story này chỉ cần hardening, không cần implement mới.
Story
As a Kỹ sư Hệ thống, I want đảm bảo Stripe PAYG checkout flow hoạt động ổn định end-to-end, so that user có thể mua page packs và pages được cộng vào tài khoản đúng cách.
Actual Architecture (as-is)
Đã implement:
POST /api/v1/stripe/create-checkout-session— tạo Stripe Checkout Session (mode: "payment")- Nhận
search_space_id,quantity - Tạo
PagePurchaserecord với statusPENDING - Trả về
checkout_url
- Nhận
_fulfill_completed_purchase()— khi webhook confirm, tănguser.pages_limit_get_checkout_urls()— tạo success/cancel URL cho Stripe redirect
Config cần thiết (env vars):
STRIPE_SECRET_KEYSTRIPE_PRICE_ID— price ID cho 1 page packSTRIPE_WEBHOOK_SECRETSTRIPE_PAGES_PER_UNIT— pages per pack (default: 1000)
Còn thiếu / chưa xác minh:
- Frontend chưa gọi endpoint này (chặn bởi Story 5.1)
- Chưa có test end-to-end với Stripe test mode
STRIPE_PRICE_IDcần được tạo trên Stripe Dashboard và config đúng
Acceptance Criteria
- Với Stripe test keys, tạo checkout session thành công, redirect đến Stripe test checkout page.
- Sau khi hoàn tất thanh toán (dùng test card
4242 4242 4242 4242), webhook trigger vàpages_limitcủa user tăng lên đúng số lượng. - Nếu
STRIPE_SECRET_KEYhoặcSTRIPE_PRICE_IDkhông được cấu hình, API trả về lỗi có thể đọc được (không crash 500).
Tasks / Subtasks
- Task 1: Verify Stripe config
- Subtask 1.1: Kiểm tra
surfsense_backend/app/config.py— đảm bảoSTRIPE_SECRET_KEY,STRIPE_PRICE_ID,STRIPE_WEBHOOK_SECRET,STRIPE_PAGES_PER_UNITđược đọc từ env vars. - Subtask 1.2: Đảm bảo
_ensure_page_buying_enabled()trả về lỗi 503 rõ ràng thay vì crash khi Stripe chưa config.
- Subtask 1.1: Kiểm tra
- Task 2: Test end-to-end với Stripe CLI
- Subtask 2.1: Dùng
stripe listen --forward-to localhost:8000/api/v1/stripe/webhookđể test webhook locally. - Subtask 2.2: Verify
PagePurchase.statuschuyển từPENDING→COMPLETEDsau webhook. - Subtask 2.3: Verify
user.pages_limittăng đúngquantity × STRIPE_PAGES_PER_UNIT.
- Subtask 2.1: Dùng
- Task 3: Thêm Stripe setup vào
.env.example- Subtask 3.1: Bổ sung
STRIPE_SECRET_KEY,STRIPE_PRICE_ID,STRIPE_WEBHOOK_SECRET,STRIPE_PAGES_PER_UNITvàosurfsense_backend/.env.examplevới comment hướng dẫn.
- Subtask 3.1: Bổ sung
Dev Notes
Current Flow (hoạt động)
FE → POST /api/v1/stripe/create-checkout-session
→ Stripe Checkout (hosted page)
→ Stripe → POST /api/v1/stripe/webhook (checkout.session.completed)
→ _fulfill_completed_purchase() → user.pages_limit += pages_granted
→ Stripe redirects FE → success_url
PagePurchase Status Enum
PENDING— checkout tạo nhưng chưa thanh toánCOMPLETED— thanh toán thành công, pages đã được grantFAILED— thanh toán thất bại
References
surfsense_backend/app/routes/stripe_routes.pysurfsense_backend/app/db.py(classPagePurchase, lines ~1616+)- Stripe Test Cards: https://stripe.com/docs/testing
Dev Agent Record
Agent Model Used
TBD
File List
surfsense_backend/app/routes/stripe_routes.pysurfsense_backend/app/config.pysurfsense_backend/.env.example