POST /api/orders
Create a new subscription order after payment has been processed. This endpoint validates all data server-side and creates both a customer record and order.Important: This endpoint should only be called after successfully processing payment through Stripe or PayPal. The
paymentReceiptId must be the payment intent ID or order ID from the payment provider.Rate Limiting
- Limit: 5 requests per IP
- Window: 10 minutes
- Purpose: Anti-spam protection
Security Features
- Honeypot field detection (
_hp) to catch bots - Server-side price validation to prevent tampering
- Input sanitization on all fields
- Plan and duration validation
Request Body
Customer’s full name (max 100 characters)Example:
"Juan Pérez"Customer’s email address (must be valid email format)Example:
"[email protected]"Customer’s phone number (sanitized format)Example:
"+1234567890"Plan identifier (max 50 characters)Must be one of:
plan-1 (Basic), plan-2 (Standard), plan-3 (Premium)Example: "plan-2"Number of simultaneous devices (1-10 range, must match plan)
1for Basic plan2for Standard plan3for Premium plan
2Subscription duration in months (1-12 range)Must be one of:
1, 2, 3, 6, or 12Example: 6Payment amount in USD (1-10000 range)Must exactly match the plan’s price for the selected duration (validated server-side within 0.01 tolerance)Example:
70Payment provider usedMust be either:
"stripe" or "paypal"Example: "stripe"Payment receipt ID from payment provider (max 200 characters)
- For Stripe: Payment Intent ID (e.g.,
pi_xxxxx) - For PayPal: Captured Order ID (e.g.,
7XX12345XX123456X)
"pi_3AbCdEfGhIjKlMnO"Honeypot field (should be empty for legitimate requests)This hidden field catches bots - if filled, request is silently rejected with fake success response
Response
Unique identifier for the created order
ISO 8601 date string indicating when the subscription expiresCalculated as: current date + months from request
Example Request
Example Response
201 Created
Error Responses
Validation Rules
The endpoint performs extensive server-side validation:| Field | Validation |
|---|---|
name | Required, max 100 chars, sanitized string |
email | Required, valid email format |
phone | Required, sanitized phone format |
planId | Required, must exist in PLANS, max 50 chars |
devices | Required, 1-10 range, must match plan |
months | Required, must be 1, 2, 3, 6, or 12 |
amount | Required, 1-10000 range, must match plan price |
paymentMethod | Required, must be “stripe” or “paypal” |
paymentReceiptId | Required, max 200 chars |
Implementation Details
The endpoint is implemented in
/src/app/api/orders/route.ts:19 and uses the following architecture:- Use Case:
CreateOrderUseCase(Clean Architecture) - Repositories:
MongoCustomerRepository,MongoOrderRepository - Security: Rate limiting, honeypot detection, input sanitization
- Validation: Server-side price and plan validation
Workflow Example
- User selects Standard plan (plan-2) for 6 months
- Client calculates price: $70
- Client creates Stripe payment intent via
/api/stripe - User completes payment, receives payment intent ID:
pi_3AbCdEfGhIjKlMnO - Client calls
/api/orderswith all details includingpaymentReceiptId - Server validates everything and creates order
- Response includes
orderIdandexpirationDate - Client shows confirmation to user
Testing
See Also
- Stripe API - Create payment intents for Stripe payments
- PayPal API - Create and capture PayPal orders
- API Overview - General API information and authentication
