Overview
Cancels an active PayPal subscription. The user retains access to their current plan until the end of the billing period. After cancellation, the account will automatically downgrade to the FREE plan.Endpoint
Authentication
Requires authenticated session via NextAuth.js. The endpoint automatically retrieves the user ID from the server-side session.Request Body
No request body required. The endpoint retrieves subscription information from the authenticated user’s session.Response
Indicates if the cancellation was successful
Human-readable status message
ISO 8601 timestamp indicating when access to the current plan will end
Current plan level (
STARTER, PROFESSIONAL, or AGENCY)Plan the user will be downgraded to (always
FREE)Present and
true if the subscription was already cancelledCancellation Process
When a subscription is cancelled:- Validation: Verifies user has an active paid subscription
- PayPal Cancellation: Calls PayPal API to cancel the subscription
- Billing Cycle Detection: Retrieves
next_billing_timefrom PayPal to determine access end date - Database Update: Updates user record with:
subscriptionStatusset tocancelledcancelledAttimestampsubscriptionEndDateset to end of billing period- Current plan retained until end date
- Email Notification: Sends cancellation confirmation email
Access Retention
Users retain full access to their current plan features until thesubscriptionEndDate. After this date:
- Plan automatically downgrades to
FREE - White-label features are disabled
- Usage limits reset to free tier
Example Request
Example Responses
Success (200 OK)
Already Cancelled (200 OK)
Error: Unauthorized (401)
Error: User Not Found (404)
Error: No Active Subscription (400)
Error: PayPal Subscription Not Found (400)
Error: Invalid Subscription State (400)
Error: Service Unavailable (503)
Database Updates
After successful cancellation:Handling Special Cases
Non-PayPal Subscriptions
For manual or trial subscriptions without a PayPal subscription ID:- Cancellation is immediate
subscriptionEndDateset tobillingCycleEndor 24 hours from cancellation- No PayPal API call is made
PayPal Subscription Not Found
If the subscription doesn’t exist in PayPal but exists in the database:- User record is automatically updated
subscriptionStatusset toinactivepaypalSubscriptionIdcleared- Error response returned with explanation
Error Recovery
The endpoint includes comprehensive error handling for:- Network failures: Returns 503 with retry message
- PayPal API errors: Maps specific PayPal errors to user-friendly messages
- Database conflicts: Automatically reconciles state mismatches
- Access token issues: Indicates temporary service unavailability
PayPal API Integration
The endpoint calls the PayPal Subscriptions API:Implementation Details
Source:src/app/api/subscription/cancel/route.ts:9
PayPal Client: src/lib/services/paypal-client.ts:201
Database Schema: prisma/schema.prisma:11 (User model)
Related Endpoints
- Activate Subscription - Activate a new subscription
- Webhooks - Handle PayPal webhook events