...
/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...
We'll cover the following...
Solution explanation
Lines 2–24: We define three gateway classes:
StripeGateway,PayPalGateway, andCryptoGateway.Each class implements the
.pay()method, encapsulating its own integration logic.StripeGatewaysimulates a credit card payment.PayPalGatewaymimics an account-based payment.CryptoGatewayrepresents a token transfer.All return and log consistent confirmation messages.
Lines 27–40: The
PaymentServiceclass is the context.It receives a gateway strategy in its ...