Payment
Learn how to set up and use Stripe for handling payments and subscriptions in your application
MkSaaS uses Stripe for payment processing and subscription management. This integration handles both one-time payments and recurring subscriptions with a flexible, developer-friendly approach.
Setup
MkSaaS boilerplate uses three price plans by default: Free plan, Pro subscription plan (monthly/yearly), and Lifetime plan (one-time payment), follow these steps to set up Stripe integration:
-
Create a Stripe account at stripe.com
-
Get your API keys from the Stripe dashboard:
- Go to the Stripe Dashboard > Developers > API keys
- Copy your Secret key (starts with
sk_test_
for test mode orsk_live_
for live mode) - Save it to your
.env
file asSTRIPE_SECRET_KEY
-
Set up webhook and get your Webhook Secret:
- Go to the Stripe Dashboard > Developers > Webhooks
- Click "Add endpoint"
- Enter your webhook URL:
https://your-domain.com/api/webhooks/stripe
- Select the events to listen for:
checkout.session.completed
customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
- Click "Reveal" to view your Webhook Signing Secret (starts with
whsec_
) - Save it to your
.env
file asSTRIPE_WEBHOOK_SECRET
-
Create products and pricing plans in Stripe:
- Go to the Stripe Dashboard > Products
- Create Pro subscription product:
- Click "Add product"
- Name: "Pro Plan"
- Description: "Advanced features with subscription pricing"
- Add monthly price:
- Click "Add price"
- Price: $9.90 (currency select USD)
- Recurring: Monthly
- Save and copy the Price ID (starts with
price_
), this will be used forNEXT_PUBLIC_STRIPE_PRICE_PRO_MONTHLY
- Add yearly price:
- Click "Add price"
- Price: $99.00 (currency select USD)
- Recurring: Yearly
- Save and copy the Price ID (starts with
price_
), this will be used forNEXT_PUBLIC_STRIPE_PRICE_PRO_YEARLY
- Create Lifetime product:
- Click "Add product"
- Name: "Lifetime Plan"
- Description: "One-time payment for lifetime access"
- Add a price:
- Price: $199.00 (currency select USD)
- Type: One-off
- Save and copy the Price ID (starts with
price_
), this will be used forNEXT_PUBLIC_STRIPE_PRICE_LIFETIME
-
Add the following environment variables:
- Update the
website.tsx
file to use Stripe as the payment provider in the payment configuration, and configure your pricing plans in the price configuration:
If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.
Environment Setup
Set up environment variables
Payment System Structure
The payment system in MkSaaS is designed with the following components:
This modular structure makes it easy to extend the payment system with new providers, pricing plans, and UI components.
Core Features
- One-time payments for lifetime access
- Recurring subscription payments (monthly and yearly)
- Customer portal integration for subscription management
- Webhook handling for payment events
- Subscription status tracking and verification
- Pre-built pricing components (tables, cards, buttons)
- Server actions for secure payment operations
- React hooks for payment state management
- Multiple pricing plans support (Free, Pro, Lifetime)
- Metadata support for tracking payment sources
Usage
MkSaaS provides simple payment utilities for handling checkout sessions and customer portals:
Webhooks
Stripe webhooks are essential for handling asynchronous events like successful payments and subscription changes.
Development
For local development, you can use the Stripe CLI to forward events to your local server:
Login to Stripe:
Forward events to your local server:
The webhook secret is printed in the terminal. Copy it and add it to your .env
file:
You can trigger test events using the Stripe CLI, or test events on the website:
Production
- Go to the Stripe Dashboard > Developers > Webhooks
- Click "Add endpoint"
- Enter your webhook URL:
https://your-domain.com/api/webhooks/stripe
- Select the events to listen for:
checkout.session.completed
customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
- After creating, click "Reveal" to view your Webhook Signing Secret
- Copy the webhook secret (starts with
whsec_
) and add it to your environment variables
UI Components
MkSaaS includes pre-built React components for handling payments:
Server Actions
MkSaaS provides server actions for payment operations:
React Hooks
MkSaaS provides hooks for payment operations:
Webhook Events
The payment system handles these webhook events in handleWebhookEvent
method:
Customization
Creating a New Payment Provider
MkSaaS makes it easy to extend the payment system with new providers:
- Create a new file in the
src/payment/provider
directory - Implement the
PaymentProvider
interface fromtypes.ts
- Update the provider selection logic in
index.ts
Example implementation for a new provider:
Then update the provider selection in index.ts
:
Testing Cards
For testing Stripe integration, use Stripe's test mode and test credit cards:
- 4242 4242 4242 4242 - Successful payment
- 4000 0000 0000 3220 - 3D Secure authentication required
- 4000 0000 0000 9995 - Insufficient funds failure
Best Practices
- Secure API Keys: Never expose your Stripe secret key in client-side code
- Validate Webhook Signatures: Always validate the signature of webhook events
- Handle Errors Gracefully: Provide user-friendly error messages when payments fail
- Use Idempotency Keys: Prevent duplicate charges with idempotency keys
- Test Webhooks Thoroughly: Ensure all webhook events are properly handled
Next Steps
Now that you understand how to work with payments in MkSaaS, explore these related integrations: