POST /api/stripe
Create a Stripe payment intent for processing subscription payments. This endpoint validates the plan and pricing, then returns a client secret for completing the payment on the client side.This endpoint creates a payment intent but does not charge the customer. Use the returned
clientSecret with Stripe.js or mobile SDKs to collect payment details and confirm the payment.Rate Limiting
- Limit: 10 requests per IP
- Window: 15 minutes
- Purpose: Prevent payment intent spam
Security Features
- Server-side price validation (prevents client-side tampering)
- Plan and duration validation
- Input sanitization
- Stripe API integration with automatic payment methods
Request Body
Plan identifier (max 50 characters)Must be one of:
plan-1 (Basic), plan-2 (Standard), plan-3 (Premium)Example: "plan-2"Subscription 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. The server validates this to prevent price manipulation (tolerance: 0.01).Example:
70Response
Stripe payment intent client secretUse this with Stripe.js or mobile SDKs to complete the payment. Format:
pi_xxxxx_secret_xxxxxExample Request
Example Response
200 OK
Error Responses
Stripe Payment Intent Details
The payment intent created by this endpoint has the following configuration:Amount in cents (USD)Calculated as:
Math.round(amount * 100)Example: $70.00 becomes 7000 centsCurrency codeAlways set to:
"usd"Payment method configurationSet to:
{ enabled: true } to support cards, wallets, etc.Custom metadata attached to the payment intentContains:
planId(string): The selected plan IDmonths(string): Subscription duration
{ planId: "plan-2", months: "6" }Validation Rules
| Field | Validation |
|---|---|
planId | Required, max 50 chars, must exist in PLANS |
months | Required, must be 1, 2, 3, 6, or 12 |
amount | Required, 1-10000 range, must match plan price |
Price Validation
The endpoint validates that the amount matches the expected price:Implementation Details
The endpoint is implemented in
/src/app/api/stripe/route.ts:11 and uses:- Stripe SDK:
stripepackage - Environment:
STRIPE_SECRET_KEYrequired - Security: Rate limiting, input sanitization, price validation
- Payment Methods: Automatic payment methods enabled (cards, wallets)
Complete Payment Flow
- Client: Call
/api/stripewith plan details - Server: Validate plan, months, and amount
- Server: Create Stripe payment intent
- Server: Return
clientSecret - Client: Use
clientSecretwith Stripe.js to collect payment - Client: Confirm payment with Stripe
- Stripe: Process payment and return payment intent ID
- Client: Call
/api/orderswith payment intent ID aspaymentReceiptId - Server: Create order and customer records
- Client: Show success confirmation
Example: Full Integration
Testing
Test Scenarios
| Scenario | Expected Result |
|---|---|
| Valid plan and price | Returns clientSecret |
| Invalid plan ID | 400 error: “Plan inválido.” |
| Wrong amount for plan | 400 error: “Monto inválido.” |
| Invalid duration | 400 error: “Duración inválida.” |
| 11th request in 15 min | 429 error: Rate limited |
Environment Variables
Required environment variable:See Also
- Orders API - Create orders after payment succeeds
- PayPal API - Alternative payment method
- API Overview - General API information
- Stripe Documentation - Official Stripe Payment Intents docs
