Table of Contents
- Installation
- Basic Setup
- Protecting API Routes
- Server Components
- Client Components
- Middleware Setup
- Payment Flow Integration
- Complete Example
Installation
Install the required packages:Basic Setup
1. Environment Variables
Create a.env.local file:
2. Initialize SolvaPay Client
Create a shared SolvaPay instance:Protecting API Routes
Basic API Route Protection
Use thepayable.next() adapter to protect Next.js App Router API routes:
Using Next.js Helpers
The@solvapay/next package provides helper functions that simplify common operations:
Available Helper Functions
Check Purchase
Create Payment Intent
Process Payment
Create Checkout Session
Sync Customer
Server Components
Use SolvaPay in Server Components to check purchase status:Client Components
Use React hooks and components for client-side payment flows:Middleware Setup
Authentication Middleware
Set up authentication middleware (implemented inproxy.ts) to extract user
information and make it available to API routes:
Supabase Auth Middleware
If using Supabase, use the provided middleware helper (exported from@solvapay/next/middleware and used from proxy.ts):
Payment Flow Integration
1. Set Up Provider
Wrap your app withSolvaPayProvider:
2. Create API Routes
Set up the required API routes using Next.js helpers:3. Use Payment Components
Use React components and hooks in your pages:Complete Example
Here’s a complete Next.js application with SolvaPay integration:Project Structure
Root Layout
Protected API Route
Checkout Page
Dashboard Page
Cache Management
The@solvapay/next package includes purchase caching to reduce API calls:
Best Practices
-
Use Environment Variables: Store API keys and configuration in
.env.local. - Separate API Routes: Keep API routes separate from page routes for better organization.
-
Error Handling: Always handle errors from helper functions (they may return
NextResponse). - Type Safety: Use TypeScript for better type safety.
- Cache Management: Use purchase caching to reduce API calls and improve performance.
- Middleware: Set up authentication middleware to extract user information early.
Next Steps
- React Integration Guide - Learn about React components and hooks
- Usage Events - Track and bill usage-based features
- Webhooks - Handle payment and subscription events
- API Reference - Full API documentation