...

/

Solution: Process Payments Through Configured Gateways

Solution: Process Payments Through Configured Gateways

Implement self-contained payment gateway strategies and delegate transaction processing through a configurable PaymentService context.

We'll cover the following...

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 ...