Overview
Activates a PayPal subscription after the user completes approval on PayPal. This endpoint validates the subscription, retrieves plan details from PayPal, and updates the user’s account with subscription status and billing information.Endpoint
Authentication
Requires authenticated session via NextAuth.js. The endpoint automatically retrieves the user ID from the server-side session.Request Body
PayPal subscription ID returned after user approval
Optional plan enum. If provided, must be one of:
FREE, STARTER, PROFESSIONAL, AGENCY. The actual plan will be detected from the PayPal subscription details.Response
Indicates if the subscription was activated successfully
Human-readable status message
Signals the frontend to refresh the user session to get updated subscription data
Subscription Activation Flow
When a subscription is activated, the following operations occur:- PayPal Verification: Fetches subscription details from PayPal API
- Plan Detection: Maps PayPal plan ID to internal plan using
PAYPAL_PLAN_TO_DB_PLANmapping - Trial Detection: Determines if subscription includes trial period based on
next_billing_time - User Update: Updates user record with:
- Plan level (STARTER, PROFESSIONAL, or AGENCY)
- PayPal subscription ID
- Subscription status set to
active - Billing cycle dates
- Trial tracking fields
- White-label enabled (all paid plans include white-label)
- Payment Record: Creates initial payment record if payment has been made
- Email Notification: Sends upgrade success email to user
PayPal Plan Mapping
The endpoint uses hardcoded PayPal plan ID mappings:Example Request
Example Response
Success (200 OK)
Error: Unauthorized (401)
Error: Missing Subscription ID (400)
Error: Invalid Plan (400)
Error: Server Error (500)
Database Updates
After successful activation, the User model is updated with:Error Handling
All errors are logged server-side and return a generic error message to prevent exposing internal details. Check server logs for detailed error information.Related Endpoints
- Cancel Subscription - Cancel an active subscription
- Webhooks - Handle PayPal webhook events
Source Code
Implementation:src/app/api/payments/activate-subscription/route.ts:12