Skip to main content
POST
/
transactions
curl -X POST https://api.example.com/transactions \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "total": 59.97,
    "coupon": "SUMMER20",
    "contents": [
      {
        "productId": 1,
        "quantity": 2,
        "price": 19.99
      },
      {
        "productId": 5,
        "quantity": 1,
        "price": 19.99
      }
    ]
  }'
{
  "message": "sale successfully created"
}

Authentication

Requires JWT authentication. The authenticated user’s ID is automatically extracted using the @CurrentUser decorator.

Request Body

total
number
required
Total amount before discount
coupon
string
Coupon code to apply discount (optional)
contents
array
required
Array of transaction line items (must not be empty)

Response

message
string
Success message
curl -X POST https://api.example.com/transactions \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "total": 59.97,
    "coupon": "SUMMER20",
    "contents": [
      {
        "productId": 1,
        "quantity": 2,
        "price": 19.99
      },
      {
        "productId": 5,
        "quantity": 1,
        "price": 19.99
      }
    ]
  }'
{
  "message": "sale successfully created"
}

Behavior

Transaction Processing

  1. Inventory Management: Product inventory is automatically decremented by the quantity purchased
  2. Coupon Application: If a coupon is provided, the discount is calculated and applied to the total
  3. Atomic Operations: All operations are performed within a database transaction to ensure data consistency
  4. User Association: The transaction is automatically linked to the authenticated user via @CurrentUser('id')

Calculation Logic

  • Subtotal is calculated from line items: sum(price * quantity)
  • If coupon is valid, discount is applied: discount = (coupon.percentage / 100) * subtotal
  • Final total: total = subtotal - discount

Validation

  • All products must exist
  • Sufficient inventory must be available for each product
  • Contents array cannot be empty
  • All numeric fields must be valid numbers
  • Quantities and product IDs must be integers
The @CurrentUser decorator automatically extracts the user ID from the JWT token, so you don’t need to include it in the request body.
This operation modifies product inventory. If the transaction fails, all changes are rolled back.

Build docs developers (and LLMs) love