Problem: Create a Pluggable Payment Processor Factory
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
apiKeyPayPal uses a
merchantIdSquare 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
if,switch, 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
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
apiKeyPayPal uses a
merchantIdSquare 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
if,switch, 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.