Chain of Responsability
About
Example
export default interface PaymentProcessor {
next?: PaymentProcessor;
processPayment(input: Input): Promise<Output>;
}
export class PjBankProcessor implements PaymentProcessor {
constructor(readonly next?: PaymentProcessor) {}
async processPayment(input: Input): Promise<Output> {
try {
const pjBankGateway = new PjBankGateway();
return await pjBankGateway.createTransaction(input);
} catch(error: any) {
if (!this.next) throw new Error('Out of processors.');
return this.next.processPayment(input);
}
}
}
export class CieloBankProcessor implements PaymentProcessor {
constructor(readonly next?: PaymentProcessor) {}
async processPayment(input: Input): Promise<Output> {
try {
const cieloBankGateway = new CieloBankGateway();
return await cieloBankGateway.createTransaction(input);
} catch(error: any) {
if (!this.next) throw new Error('Out of processors.');
return this.next.processPayment(input);
}
}
}Last updated