Last active
October 26, 2022 01:32
-
-
Save axel-andrade/de340353aae0bb5e43acfe9fdf3e9530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Order = { | |
id: number; | |
value: number; | |
customer: string; | |
}; | |
type Card = { | |
id: number; | |
ownerName: string; | |
cardNumber: number; | |
expiryDate: Date; | |
securityCode: string; | |
billingAddress: string; | |
}; | |
interface CardProcessorService { | |
executePayment(card: Card, value: number): Promise<void>; | |
} | |
class CardProcessorApiXYZ implements CardProcessorService { | |
public async executePayment(card: Card, value: number): Promise<void>{ | |
// implementation of card processor api XYZ here | |
} | |
} | |
class CreditPayment { | |
private card: Card; | |
private cardProcessorService: CardProcessorService; | |
private feeValue: number; | |
private order: Order; | |
constructor( | |
card: Card, | |
cardProcessorService: CardProcessorService, | |
order: Order | |
) { | |
this.card = card; | |
this.cardProcessorService = cardProcessorService; | |
this.order = order; | |
this.feeValue = 5; | |
} | |
public async executePayment(): Promise<void> { | |
const finalValue = this.getFinalValue(); | |
await this.cardProcessorService.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