Problem: Process Payments Through Configured Gateways

med
30 min
Build a payment service that can process transactions through different gateways—Stripe, PayPal, or crypto—selected dynamically at runtime.

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:

  1. Gateways (Strategies):

    1. StripeGateway: Logs a message like [Stripe] Processed payment of $100 successfully.

    2. PayPalGateway: Logs a message like [PayPal] Sent payment of $200 successfully.

    3. CryptoGateway: Logs a message like [Crypto] Confirmed transfer of 300 tokens.

    4. Each must implement .pay(amount) that returns a confirmation string.

  2. PaymentService (Context):

    1. Delegates all payment processing to the active gateway’s .pay() method.

    2. Supports runtime switching through .setGateway().

Constraints

  • No if or switch statements inside PaymentService.

  • 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

med
30 min
Build a payment service that can process transactions through different gateways—Stripe, PayPal, or crypto—selected dynamically at runtime.

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:

  1. Gateways (Strategies):

    1. StripeGateway: Logs a message like [Stripe] Processed payment of $100 successfully.

    2. PayPalGateway: Logs a message like [PayPal] Sent payment of $200 successfully.

    3. CryptoGateway: Logs a message like [Crypto] Confirmed transfer of 300 tokens.

    4. Each must implement .pay(amount) that returns a confirmation string.

  2. PaymentService (Context):

    1. Delegates all payment processing to the active gateway’s .pay() method.

    2. Supports runtime switching through .setGateway().

Constraints

  • No if or switch statements inside PaymentService.

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

Node.js
// --- Strategies (implement these) ---
class StripeGateway {}
class PayPalGateway {}
class CryptoGateway {}
// --- Context (implement this too) ---
class PaymentService {
constructor(gateway) {}
setGateway(gateway) {}
pay(amount) {}
}
// Example usage
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.
*/