Search⌘ K
AI Features

Solution: Process Payments Through Configured Gateways

Explore how to design a Node.js payment system using the Strategy Pattern to handle multiple payment gateways. Understand how to encapsulate payment logic for Stripe, PayPal, and Crypto, enabling runtime swapping without modifying core checkout code. Learn to build flexible, maintainable payment processing that adapts to new requirements seamlessly.

Solution explanation

  • Lines 2–24: We define three gateway classes: StripeGatewayPayPalGateway, and CryptoGateway.

    • Each class implements the .pay() method, encapsulating its own integration logic.

    • StripeGateway simulates a credit card payment.

    • PayPalGateway mimics an account-based payment.

    • CryptoGateway represents a token transfer.

    • All return and log consistent confirmation messages.

  • Lines 27–40: The PaymentService class is the context.

    • It receives a gateway strategy in its ...