Skip to main content
POST
/
api
/
user
/
purchase
{
  "courseId": "60d5ec49f1b2c72b8c8e4f1a"
}
{
  "success": true,
  "purchaseId": "65f1a2b3c4d5e6f7g8h9i0j1",
  "orderId": "order_NXs8jKl9mP0qRs",
  "keyId": "rzp_test_1DP5mmOlF5G5ag"
}

Authentication

This endpoint requires authentication via Clerk. The userId is automatically extracted from the authentication token.

Request Body

courseId
string
required
The unique identifier of the course to purchase

Response

success
boolean
required
Indicates whether the request was successful
purchaseId
string
Unique identifier for the purchase record (used in payment verification)
orderId
string
Razorpay order ID for the payment
keyId
string
Razorpay key ID for client-side payment integration
message
string
Error message if success is false

Request Example

{
  "courseId": "60d5ec49f1b2c72b8c8e4f1a"
}

Response Examples

{
  "success": true,
  "purchaseId": "65f1a2b3c4d5e6f7g8h9i0j1",
  "orderId": "order_NXs8jKl9mP0qRs",
  "keyId": "rzp_test_1DP5mmOlF5G5ag"
}
{
  "success": false,
  "message": "Invalid request data"
}
{
  "success": false,
  "message": "Data Not Found"
}
{
  "success": false,
  "message": "An unexpected error occurred"
}

Error Codes

Status CodeDescription
200Success or data not found (check success field)
400Invalid request data - courseId is required
401Unauthorized - Invalid or missing authentication token
500Internal server error

Purchase Flow

  1. Create Purchase Record: A purchase record is created with status 'created' in the database
  2. Calculate Amount: Final amount is calculated after applying any discounts: price - (discount * price / 100)
  3. Create Razorpay Order: A Razorpay order is created with the purchase details
  4. Return Order Details: The client receives purchaseId, orderId, and keyId to initiate payment
  5. Payment Verification: After payment, use the /api/user/verify-razorpay endpoint to complete the purchase

Important Notes

  • The purchase record is created immediately with status 'created'
  • If Razorpay order creation fails, the purchase record is automatically deleted to keep the database clean
  • The purchaseId must be passed to the payment verification endpoint after successful payment
  • The amount is calculated using the course’s coursePrice and discount fields
  • Currency defaults to the CURRENCY environment variable or 'INR'

Validation Schema

The request body is validated using Zod:
{
  courseId: z.string().min(1)
}