Skip to content

Instantly share code, notes, and snippets.

@axel-andrade
Last active October 26, 2022 01:08
Show Gist options
  • Save axel-andrade/66c52d8bb10053e3d9c92ae6cb47b5e1 to your computer and use it in GitHub Desktop.
Save axel-andrade/66c52d8bb10053e3d9c92ae6cb47b5e1 to your computer and use it in GitHub Desktop.
type Order = {
id: number;
value: number;
customer: string;
};
type Card = {
id: number;
ownerName: string;
cardNumber: number;
expiryDate: Date;
securityCode: string;
billingAddress: string;
};
class CardProcessorApiXYZ {
public async executePayment(card: Card, value: number): Promise<void> {
// implementation of card processor api XYZ here
}
}
class CreditPayment {
private card: Card;
private cardProcessorApiXYZ: CardProcessorApiXYZ;
private feeValue: number;
private order: Order;
constructor(card: Card, order: Order) {
this.card = card;
this.cardProcessorApiXYZ = new CardProcessorApiXYZ();
this.order = order;
this.feeValue = 5;
}
public async executePayment(): Promise<void> {
const finalValue = this.getFinalValue();
await this.cardProcessorApiXYZ.executePayment(this.card, finalValue);
}
public getFinalValue(): number {
const order = this.order;
const finalValue = order.value + this.getFee(order.value);
return finalValue;
}
private getFee(value: number): number {
return (value * this.feeValue) / 100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment