Payment System
Understanding the payment and subscription system in NestSaaS
NestSaaS includes a comprehensive payment system built on Stripe, allowing you to monetize your content and services through one-time purchases and recurring subscriptions.
Payment Features
- Stripe Integration: Secure payment processing with Stripe
- One-time Purchases: Sell digital products, content access, or services
- Subscriptions: Offer recurring subscription plans with different tiers
- Customer Portal: Allow users to manage their subscriptions
- Webhook Integration: Automatically process payment events
- Access Control: Restrict content based on purchase or subscription status
Payment Models
NestSaaS supports two primary payment models:
One-time Purchases
One-time purchases are handled through the Purchase
model, which tracks individual transactions:
Field | Type | Description |
---|---|---|
id | string | Unique identifier (CUID) |
product | string | Product identifier |
amount | decimal | Purchase amount |
currency | string | Currency code (default: USD) |
description | string | Product description |
status | enum | Purchase status |
stripeSessionId | string | Stripe checkout session ID |
stripePaymentIntentId | string | Stripe payment intent ID |
createdAt | date | Creation timestamp |
updatedAt | date | Last update timestamp |
userId | int | Buyer's user ID |
articleId | int | Associated article ID (optional) |
Purchase Status
Purchases can have the following statuses:
- PENDING: Payment initiated but not completed
- COMPLETED: Payment successfully processed
- FAILED: Payment attempt failed
- REFUNDED: Purchase was refunded
Subscriptions
Recurring subscriptions are managed through the Subscription
model:
Field | Type | Description |
---|---|---|
id | string | Unique identifier (CUID) |
stripeCustomerId | string | Stripe customer ID |
stripeSubscriptionId | string | Stripe subscription ID |
stripePriceId | string | Stripe price ID |
service | string | Service identifier |
plan | string | Subscription plan name |
description | string | Plan description |
interval | string | Billing interval (monthly, yearly) |
status | enum | Subscription status |
isPaid | boolean | Whether subscription is paid |
startDate | date | Subscription start date |
endDate | date | Subscription end date |
userId | int | Subscriber's user ID |
spaceSlug | string | Associated Space slug (optional) |
articleId | int | Associated article ID (optional) |
Subscription Status
Subscriptions can have the following statuses:
- pending: Payment pending
- active: Subscription is active
- canceled: Subscription has been canceled
- past_due: Payment is past due
- unpaid: Payment failed
- incomplete: Setup incomplete
- incomplete_expired: Setup period expired
- trialing: In trial period
Payment Flow
One-time Purchase Flow
- User selects a product to purchase
- System creates a Purchase record with status PENDING
- User is redirected to Stripe Checkout
- After payment, Stripe sends a webhook notification
- System updates the Purchase status to COMPLETED
- User gains access to the purchased content
Subscription Flow
- User selects a subscription plan
- System creates a Subscription record
- User is redirected to Stripe Checkout
- After subscription setup, Stripe sends a webhook notification
- System updates the Subscription with Stripe IDs and status
- User gains access to the subscription benefits
Implementation
Server Actions
NestSaaS provides server actions for handling payments:
Purchase Actions
Subscription Actions
Webhook Handling
NestSaaS automatically processes Stripe webhook events to update purchase and subscription statuses:
checkout.session.completed
: Updates purchase/subscription statuscustomer.subscription.updated
: Updates subscription detailscustomer.subscription.deleted
: Marks subscription as canceledinvoice.payment_succeeded
: Records successful paymentsinvoice.payment_failed
: Handles failed payments
Access Control
NestSaaS integrates the payment system with content access control:
Purchase-based Access
Access to content can be restricted based on purchase status:
Subscription-based Access
Access to content can be restricted based on subscription status:
Configuration
To use the payment system, you need to configure Stripe in your environment variables:
Best Practices
- Test Mode: Always test payments in Stripe test mode before going live
- Webhook Security: Secure your webhook endpoint and validate Stripe signatures
- Error Handling: Implement robust error handling for payment failures
- Idempotency: Handle webhook events idempotently to prevent duplicate processing
- User Experience: Provide clear feedback about payment status to users
- Refund Policy: Define and communicate a clear refund policy
- Tax Compliance: Consider tax implications for different regions
Next Steps
- Admin Panel - Learn how to manage payments and subscriptions
- Setup & Configuration - Detailed Stripe setup instructions