Payments
Process payments and validate credit card information for e-commerce transactions.Validate Credit Card
POST /api/ecom/pagos/validate
Validate credit card number using Luhn algorithm
This endpoint does not require authentication and can be used for client-side validation before checkout.
Request Body
Credit card number (13-19 digits)Spaces and hyphens are automatically removed during validation.
Response
Whether the card number is valid
Validated card number (returned only if valid)
Error message (returned only if invalid)
Example Request
Example Responses
200 OK - Valid Card
400 Bad Request - Invalid Card
400 Bad Request - Invalid Format
400 Bad Request - Missing Card Number
Card Validation Rules
The validation endpoint enforces the following rules:- Length: Card number must be between 13 and 19 digits
- Format: Only numeric digits (spaces and hyphens are automatically removed)
- Luhn Algorithm: Card number must pass Luhn checksum validation
This validation only checks if the card number format is valid using the Luhn algorithm. It does NOT:
- Verify if the card actually exists
- Check card balance or limits
- Process actual payments
- Validate expiration dates or CVV
Checkout and Process Payment
POST /api/ecom/pagos/checkout
Process payment for the customer’s shopping cart
Headers
Bearer token from login responseFormat:
Bearer {token}Request Body
Valid credit card number (13-19 digits)Must pass Luhn validation
Response
Success message: “Pago realizado en línea”
Invoice/receipt code for the completed transaction
Example Request
Example Response
201 Created - Success
Error Responses
401 Unauthorized
400 Bad Request - Missing Card Number
400 Bad Request - Invalid Card
404 Not Found
400 Bad Request - Empty Cart
400 Bad Request - Insufficient Stock
500 Internal Server Error
Payment Processing Flow
The checkout process follows these steps:- Validate Card: Card number is validated using Luhn algorithm
- Verify Cart: Check that the customer has an active cart
- Check Cart Contents: Ensure cart is not empty
- Process Transaction: Execute payment in database (atomic transaction)
- Generate Invoice: Create invoice record and return invoice code
- Clear Cart: Cart is emptied after successful payment
The payment processing is wrapped in a database transaction to ensure data consistency. If any step fails, all changes are rolled back.
Transaction Safety
The checkout endpoint ensures:- Atomic Operations: All database operations succeed or fail together
- Stock Validation: Products are checked for sufficient stock
- Price Consistency: Prices are locked at transaction time
- Cart Clearing: Cart is automatically emptied after successful payment
Complete Payment Workflow
Typical e-commerce payment flow:Business Rules
The payment system enforces these business rules:Cart Validation
- Cart must exist for the authenticated customer
- Cart must contain at least one item
- All products in cart must have sufficient stock
Card Validation
- Card number must be 13-19 digits
- Card must pass Luhn algorithm validation
- Card number is required for all payments
Transaction Processing
- All operations are atomic (succeed or fail together)
- Stock is decremented after successful payment
- Cart is automatically cleared after payment
- Invoice is generated with unique code
Error Handling
Validation Errors
Card validation errors (400 Bad Request) include:- Invalid card format
- Failed Luhn check
- Missing card number
Business Logic Errors
Checkout errors include:- Empty cart (400)
- Insufficient stock (400)
- Cart not found (404)
- Invalid authentication (401)
System Errors
Server errors (500) may occur due to:- Database connection issues
- Transaction failures
- Unexpected exceptions
Stock-related errors include the product name in the error message to help identify which product caused the issue.
Testing Credit Cards
For testing purposes, you can use these Luhn-valid test card numbers:- Visa: 4532015112830366
- Mastercard: 5425233430109903
- Amex: 374245455400126
- Discover: 6011111111111117
These are test numbers that pass Luhn validation but are not real credit cards. They can be used for development and testing.
Source Code Reference
Implementation details can be found in:- Routes:
/src/routes/ecom.pagos.routes.js:1-14 - Controller:
/src/controllers/ecom.pagos.controller.js:1-87 - DTOs:
/src/dtos/pagos.dto.js:1-36 - Model:
/src/models/pagos.model.js
Luhn Algorithm Implementation
The Luhn validation is implemented in/src/dtos/pagos.dto.js:6-23: