checkPurchase
SolvaPay SDK / next/src / checkPurchase
Function: checkPurchase()
checkPurchase(
request,options):Promise<PurchaseCheckResult|NextResponse<unknown>>
Defined in: packages/next/src/index.ts:98
Check user purchase status with automatic deduplication and caching.
This Next.js helper function provides optimized purchase checking with:
- Automatic request deduplication (concurrent requests share the same promise)
- Short-term caching (2 seconds) to prevent duplicate sequential requests
- Fast path optimization using cached customer references from client
- Automatic customer creation if needed
The function:
- Extracts user ID from request (via requireUserId from @solvapay/auth)
- Gets user email and name from Supabase JWT token (if available)
- Validates cached customer reference (if provided via header)
- Ensures customer exists in SolvaPay backend
- Returns customer purchase information
Parameters
request
Request
Next.js request object (NextRequest extends Request)
options
CheckPurchaseOptions = {}
Configuration options
Returns
Promise<PurchaseCheckResult | NextResponse<unknown>>
Purchase check result with customer data and purchases, or NextResponse error
Example
import { NextRequest, NextResponse } from 'next/server';
import { checkPurchase } from '@solvapay/next';
export async function GET(request: NextRequest) {
const result = await checkPurchase(request);
if (result instanceof NextResponse) {
return result; // Error response
}
return NextResponse.json(result);
}
See
- clearPurchaseCache for cache management
- getPurchaseCacheStats for cache monitoring
Since
1.0.0