Created
May 27, 2020 22:39
-
-
Save mariobittencourt/1b9990d509f620e339e631ba8e2b95ce to your computer and use it in GitHub Desktop.
Add refund
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
class Payment: | |
# ... existing methods | |
def refund(self, amount_refunded: float, refund_id: str) -> None: | |
# business rules | |
if self.status != 'SETTLED': | |
raise Exception('Only settled payments can be refunded') | |
if amount_refunded > self.amount_due: | |
raise Exception('You can only refund amounts equal or smaller than the amount due') | |
if amount_refunded <= 0: | |
raise Exception('You can only refund amounts greater than zero') | |
self.record_that(PaymentRefunded( | |
self.payment_id, amount_refunded, refund_id) | |
) | |
def apply_payment_refunded(self, event: PaymentRefunded): | |
self._status = 'REFUNDED' | |
self._amount_due -= event.amount_refunded | |
def record_that(self, event: DomainEvent): | |
if isinstance(event, PaymentCreated): | |
self.apply_payment_created(event) | |
elif isinstance(event, PaymentRefunded): | |
self.apply_payment_refunded(event) | |
self.events.append(event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment