Modular Payments Processing with the Factory Pattern in Python

Modular Payments Processing with the Factory Pattern in Python

The Factory Pattern can be particularly useful in the context of payments processing, where different payment methods require different implementations of the payment processing logic. For example, credit card payments may require integration with a credit card processing API, while PayPal payments may require integration with the PayPal API.

In a hypothetical payments processing system, we could use the Factory Pattern to encapsulate the creation of payment processors for different payment methods. Here's an example implementation in Python:

class PaymentProcessorFactory:
    def get_payment_processor(self, payment_method):
        if payment_method == "credit_card":
            return CreditCardProcessor()
        elif payment_method == "paypal":
            return PayPalProcessor()
        elif payment_method == "apple_pay":
            return ApplePayProcessor()
        else:
            raise ValueError("Invalid payment method")
        
class PaymentProcessor:
    def process_payment(self, amount):
        raise NotImplementedError
        
class CreditCardProcessor(PaymentProcessor):
    def process_payment(self, amount):
        # Process credit card payment using credit card processing API
        pass
    
class PayPalProcessor(PaymentProcessor):
    def process_payment(self, amount):
        # Process PayPal payment using PayPal API
        pass
    
class ApplePayProcessor(PaymentProcessor):
    def process_payment(self, amount):
        # Process Apple Pay payment using Apple Pay API
        pass        

In this example, we have a PaymentProcessorFactory class with a get_payment_processor() method that creates and returns an instance of the appropriate PaymentProcessor based on the payment_method argument. The PaymentProcessor class is an abstract base class with a process_payment() method that raises a NotImplementedError, which is intended to be overridden by the concrete payment processor classes.

The CreditCardProcessor, PayPalProcessor, and ApplePayProcessor classes inherit from the PaymentProcessor class and override the process_payment() method to process payments via credit card, PayPal, and Apple Pay, respectively.

Here's an example of how the PaymentProcessorFactory can be used:

factory = PaymentProcessorFactory()


credit_card_processor = factory.get_payment_processor("credit_card")
credit_card_processor.process_payment(100.0)


paypal_processor = factory.get_payment_processor("paypal")
paypal_processor.process_payment(50.0)


apple_pay_processor = factory.get_payment_processor("apple_pay")
apple_pay_processor.process_payment(25.0)
        

In this example, we create an instance of PaymentProcessorFactory and use it to create instances of the appropriate PaymentProcessor based on the payment_method argument. We then use the concrete payment processor instances to process payments via credit card, PayPal, and Apple Pay, respectively.

The use of the Factory Pattern in this example provides several benefits. First, it allows us to centralize and modularize the creation of payment processors for different payment methods. This can make it easier to add new payment methods or modify existing ones, as we only need to modify the factory class rather than scattered parts of the codebase. Second, it promotes loose coupling between the different payment processors and the rest of the code, which can make the code more flexible and easier to maintain. Finally, it improves code readability by making it clear how different payment processors are created and used within the system.

To view or add a comment, sign in

More articles by Hesham Sayed

Insights from the community

Others also viewed

Explore topics