Problem: Process Payments Through Configured Gateways
Problem statement
Your e-commerce team is rolling out multi-provider payments. Some regions support Stripe (credit cards), while others use PayPal, and a few utilize crypto payments.
Checkout systems often hardcode payment gateway logic with conditionals like:
if (method === 'stripe') { /* Stripe logic */ }else if (method === 'paypal') { /* PayPal logic */ }else if (method === 'crypto') { /* Crypto logic */ }
Every time a new gateway is added, developers must modify this block and redeploy. You’ve decided to implement a Strategy Pattern so each gateway’s logic is encapsulated, and the payment workflow remains stable.
Goal
Implement both components of the pattern:
Gateways (Strategies):
StripeGateway: Logs a message like[Stripe] Processed payment of $100 successfully.PayPalGateway: Logs a message like[PayPal] Sent payment of $200 successfully.CryptoGateway: Logs a message like[Crypto] Confirmed transfer of 300 tokens.Each must implement
.pay(amount)that returns a confirmation string.
PaymentService(Context):Delegates all payment processing to the active gateway’s
.pay()method.Supports runtime switching through
.setGateway().
Constraints
No
iforswitchstatements insidePaymentService.Each gateway must define its own
.pay()implementation.Adding a new gateway must not require modifying
PaymentService.The design must simulate real external integrations via console logs.
Sample output
The examples below illustrate what the output should look like:
const service = new PaymentService(new StripeGateway());console.log(service.pay(100));/* Expected output:[Stripe] Processed payment of $100 successfully.*/service.setGateway(new PayPalGateway());console.log(service.pay(200));/* Expected output:[PayPal] Sent payment of $200 successfully.*/service.setGateway(new CryptoGateway());console.log(service.pay(300));/* Expected output:[Crypto] Confirmed transfer of 300 tokens.*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Process Payments Through Configured Gateways
Problem statement
Your e-commerce team is rolling out multi-provider payments. Some regions support Stripe (credit cards), while others use PayPal, and a few utilize crypto payments.
Checkout systems often hardcode payment gateway logic with conditionals like:
if (method === 'stripe') { /* Stripe logic */ }else if (method === 'paypal') { /* PayPal logic */ }else if (method === 'crypto') { /* Crypto logic */ }
Every time a new gateway is added, developers must modify this block and redeploy. You’ve decided to implement a Strategy Pattern so each gateway’s logic is encapsulated, and the payment workflow remains stable.
Goal
Implement both components of the pattern:
Gateways (Strategies):
StripeGateway: Logs a message like[Stripe] Processed payment of $100 successfully.PayPalGateway: Logs a message like[PayPal] Sent payment of $200 successfully.CryptoGateway: Logs a message like[Crypto] Confirmed transfer of 300 tokens.Each must implement
.pay(amount)that returns a confirmation string.
PaymentService(Context):Delegates all payment processing to the active gateway’s
.pay()method.Supports runtime switching through
.setGateway().
Constraints
No
iforswitchstatements insidePaymentService.Each gateway must define its own
.pay()implementation.Adding a new gateway must not require modifying
PaymentService.The design must simulate real external integrations via console logs.
Sample output
The examples below illustrate what the output should look like:
const service = new PaymentService(new StripeGateway());console.log(service.pay(100));/* Expected output:[Stripe] Processed payment of $100 successfully.*/service.setGateway(new PayPalGateway());console.log(service.pay(200));/* Expected output:[PayPal] Sent payment of $200 successfully.*/service.setGateway(new CryptoGateway());console.log(service.pay(300));/* Expected output:[Crypto] Confirmed transfer of 300 tokens.*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.