Patrones de Diseño y Principios SOLID
Patrones de Diseño y Principios SOLID en Python
Principios SOLID
Principio de Responsabilidad Única (SRP) en Python
Procesador de Pagos con Stripe en Python
Aplicar el Principio de Responsabilidad Única (SRP)
Principio Abierto/Cerrado (OCP) en Python
Aplicar el Principio Abierto/Cerrado (OCP)
Principio de Sustitución de Liskov (LSP) en Python
Aplicar el Principio de Sustitución de Liskov (LSP)
Principio de Segregación de Interfaces (ISP) en Python
Aplicar el Principio de Segregación de Interfaces (ISP)
Principio de Inversión de Dependencias (DIP) en Python
Aplicar el Principio de Inversión de Dependencias (DIP)
Reestructuración del proyecto
Reestructuración de un proyecto en Python
Patrones de Diseño
Introducción a los Patrones de Diseño
Patrón Strategy en Python
Implementando el Patrón Strategy
Patrón Factory en Python
Implementando el Patrón Factory
Patrón Decorator en Python
Implementando el Patrón Decorador: Mejora tu Servicio de Pagos
Patrón Builder en Python
Implementando el Patrón Builder: Construye Servicios de Pago
Patrón Observer en Python
Implementando el Patrón Observer
Patrón Chain of Responsibility en Python
Implementando el Patrón Chain of Responsibility: Flujo Eficiente de Validaciones
Patrones de Diseño y Principios SOLID en un Procesador de Pagos
You don't have access to this class
Keep learning! Join and start boosting your career
Building a payment processor is a common challenge in the software development industry, and in this course we will do it by applying SOLID principles and design patterns. The initial code will be basic and we will refactor it to conform to best practices as we go along.
The code starts with a simple class that has a single method called processTransaction
. This method receives two parameters: customerData
and paymentData
. Within this method, three basic validations are performed:
Payment processing is handled through integration with Stripe. We use Stripe's API key, which is stored in environment variables, and create a charge based on the amount and data provided. The method handles any errors with a try-except
block, sending a notification if the payment fails.
The code includes mechanisms to notify the customer by email or text message. However, since there is no SMTP server configured, annotated examples are used to illustrate what sending emails would look like. In the case of text messages, a mock that simulates an SMS gateway is used.
Finally, the method saves the transaction details in a transactions.log
file. This file contains information such as the name of the customer, the amount collected and the final status of the payment. These records are useful for future reference and auditing.
The code is flexible, and we can modify the customer and payment information to test different scenarios. For example, we can change the customer name, the amount to be charged, and the payment method, including using card tokens instead of card numbers. Stripe provides several tokens and test numbers that can be used to simulate transactions under different conditions.
An essential part of the code is the configuration of environment variables, which are handled by the Python .env
module. This module loads the Stripe API key from the .env
file, which ensures that the key is protected and accessible only during program execution.
Contributions 16
Questions 4
Want to see more contributions, questions and answers from the community?