I would like some feedback on my code. I am currently building a payments solution in a serverless lambda project. This what I came up with (very basic code at the moment):
// "interface implementation"
export default class Stripe {
constructor() {
this.stripe = new Stripe()
}
async charge(data) {
try {
const result = await this.stripe.chargeCustomerByToken(data);
return result;
} catch (error) {
throw error;
}
}
}
export default class PaymentRepository {
constructor(PaymentInterface) {
this.ipayment = PaymentInterface;
}
async charge(data) {
try {
const result = await this.ipayment.charge(data);
return result;
} catch (error) {
throw error;
}
}
}
export const charge = async (repository, data) => {
if (!repository || !data) {
throw new InvalidParameterError('Missing dependencies');
}
try {
const result = await repository.charge(data);
return result;
} catch (error) {
throw error;
}
};
I would then use it like this in my main handler: charge(new PaymentRepository(new Stripe()))
.
This is my current structure. It feels super repetitive but still flexible and is easy to test, and also easy to change payment API (maybe to PayPal or something). But I'm not really sure if I'm just over-complexing the whole project, or maybe this isn't even the Node/JS way to code.
new Stripe()
results in infinite recursion - is there a typo here or something I'm missing? \$\endgroup\$