Problem: Create a Pluggable Payment Processor Factory

Medium
30 min
Build a factory that selects a payment processor based on vendor config, using constructor arguments and a strategy map.

Problem statement

You’re integrating with multiple payment vendors. Each vendor (Stripe, PayPal, or Square) requires a different class and some initialization parameters:

  • Stripe uses an apiKey

  • PayPal uses a merchantId

  • Square uses an accessToken

The config object includes a vendor string and a credentials object that must be passed to the appropriate processor’s constructor.

The config looks like this:

const config = {
vendor: 'stripe',
credentials: {
apiKey: 'abc-123'
}
};

You need a factory that returns the correct payment processor instance, preconfigured with the required credentials. Each processor class has a .charge(amount) method.

Goal

Implement createPaymentProcessor(config) that returns an instance of the correct class, initialized with the right parameters.

Constraints

  • Do not use ifswitch, or ternaries to select modules.

  • All parameter logic must live in the factory.

  • Each processor must be instantiated with its own specific constructor signature.

Sample output

The examples below illustrate what the output should look like:

const stripeConfig = {
vendor: 'stripe',
credentials: { apiKey: 'abc-123' }
};
const stripeProcessor = createPaymentProcessor(stripeConfig);
console.log(stripeProcessor.charge(49.99));
/* Expected output:
Charging $49.99 via Stripe (abc-123) */
const paypalConfig = {
vendor: 'paypal',
credentials: { merchantId: 'paypal-456' }
};
const paypalProcessor = createPaymentProcessor(paypalConfig);
console.log(paypalProcessor.charge(24.99));
/* Expected output:
Charging $24.99 via PayPal (paypal-456) */
const squareConfig = {
vendor: 'square',
credentials: { accessToken: 'sq-token-789' }
};
const squareProcessor = createPaymentProcessor(squareConfig);
console.log(squareProcessor.charge(19.99));
/* Expected output:
Charging $19.99 via Square (sq-token-789) */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Create a Pluggable Payment Processor Factory

Medium
30 min
Build a factory that selects a payment processor based on vendor config, using constructor arguments and a strategy map.

Problem statement

You’re integrating with multiple payment vendors. Each vendor (Stripe, PayPal, or Square) requires a different class and some initialization parameters:

  • Stripe uses an apiKey

  • PayPal uses a merchantId

  • Square uses an accessToken

The config object includes a vendor string and a credentials object that must be passed to the appropriate processor’s constructor.

The config looks like this:

const config = {
vendor: 'stripe',
credentials: {
apiKey: 'abc-123'
}
};

You need a factory that returns the correct payment processor instance, preconfigured with the required credentials. Each processor class has a .charge(amount) method.

Goal

Implement createPaymentProcessor(config) that returns an instance of the correct class, initialized with the right parameters.

Constraints

  • Do not use ifswitch, or ternaries to select modules.

  • All parameter logic must live in the factory.

  • Each processor must be instantiated with its own specific constructor signature.

Sample output

The examples below illustrate what the output should look like:

const stripeConfig = {
vendor: 'stripe',
credentials: { apiKey: 'abc-123' }
};
const stripeProcessor = createPaymentProcessor(stripeConfig);
console.log(stripeProcessor.charge(49.99));
/* Expected output:
Charging $49.99 via Stripe (abc-123) */
const paypalConfig = {
vendor: 'paypal',
credentials: { merchantId: 'paypal-456' }
};
const paypalProcessor = createPaymentProcessor(paypalConfig);
console.log(paypalProcessor.charge(24.99));
/* Expected output:
Charging $24.99 via PayPal (paypal-456) */
const squareConfig = {
vendor: 'square',
credentials: { accessToken: 'sq-token-789' }
};
const squareProcessor = createPaymentProcessor(squareConfig);
console.log(squareProcessor.charge(19.99));
/* Expected output:
Charging $19.99 via Square (sq-token-789) */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
class StripeProcessor {
constructor({ apiKey }) {
this.apiKey = apiKey;
}
charge(amount) {
return `Charging $${amount} via Stripe (${this.apiKey})`;
}
}
class PayPalProcessor {
constructor({ merchantId }) {
this.merchantId = merchantId;
}
charge(amount) {
return `Charging $${amount} via PayPal (${this.merchantId})`;
}
}
class SquareProcessor {
constructor({ accessToken }) {
this.accessToken = accessToken;
}
charge(amount) {
return `Charging $${amount} via Square (${this.accessToken})`;
}
}
// Implement this
function createPaymentProcessor(config) {
// return correct processor instance using config
}
// Example usage
const stripeConfig = {
vendor: 'stripe',
credentials: { apiKey: 'abc-123' }
};
const stripeProcessor = createPaymentProcessor(stripeConfig);
console.log(stripeProcessor.charge(49.99));
/* Expected output:
Charging $49.99 via Stripe (abc-123) */
const paypalConfig = {
vendor: 'paypal',
credentials: { merchantId: 'paypal-456' }
};
const paypalProcessor = createPaymentProcessor(paypalConfig);
console.log(paypalProcessor.charge(24.99));
/* Expected output:
Charging $24.99 via PayPal (paypal-456) */
const squareConfig = {
vendor: 'square',
credentials: { accessToken: 'sq-token-789' }
};
const squareProcessor = createPaymentProcessor(squareConfig);
console.log(squareProcessor.charge(19.99));
/* Expected output:
Charging $19.99 via Square (sq-token-789) */