The Payment service provides a mock payment processing implementation for testing and development. It simulates payment intent round-trips with realistic latency and random failure scenarios.
Types
PaymentResult
Discriminated union representing the outcome of a payment transaction.
type PaymentResult =
| { success : true }
| { success : false ; error : string }
Payment transaction result Payment completed successfully
Human-readable error message in Spanish
Functions
processMockPayment
Simulates a payment intent with realistic latency (2 seconds) and a 15% random failure rate.
processMockPayment ( amount : number ): Promise < PaymentResult >
Payment amount in currency units (currently not validated, reserved for future use)
Payment outcome after 2-second delay true if payment succeeded (85% probability), false otherwise
Only present when success: false. Contains localized error message
Example
import { processMockPayment } from '@/src/lib/core/payments/paymentService' ;
const result = await processMockPayment ( 45.50 );
if ( result . success ) {
console . log ( 'Payment successful!' );
// Navigate to success screen
} else {
console . error ( 'Payment failed:' , result . error );
// Show error message to user
alert ( result . error );
}
Type guard usage
const result = await processMockPayment ( totalAmount );
// TypeScript narrows the type based on success field
if ( result . success ) {
// result is { success: true }
showSuccessNotification ();
} else {
// result is { success: false; error: string }
showErrorMessage ( result . error );
}
Behavior
Latency simulation
All payment requests include a 2-second delay (setTimeout) to simulate network round-trip and payment processor latency.
Failure simulation
The service has a 15% random failure rate (Math.random() > 0.15). Failed payments return the following Spanish error message:
Lo sentimos, el banco rechazo la transaccion. Intenta con otro metodo.
This mock service is intended for development and testing only. Replace with a real payment processor (Stripe, PayPal, etc.) in production.
Production considerations
When replacing this service with a real payment processor:
Maintain the same PaymentResult type signature for minimal refactoring
Handle additional error cases (network failures, declined cards, etc.)
Implement proper error logging and monitoring
Add PCI compliance requirements
Implement idempotency for retry scenarios