Skip to content

Commit

Permalink
Switch payment to REFUNDED if resulting captured_amount is negative too
Browse files Browse the repository at this point in the history
  • Loading branch information
radekholy24 committed May 4, 2024
1 parent 913bdbe commit 7295cf4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion payments/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enum
import json
import logging
from typing import Iterable
from typing import Optional
from typing import Union
Expand All @@ -16,6 +17,9 @@
from .core import provider_factory


logger = logging.getLogger(__name__)


class PaymentAttributeProxy:
def __init__(self, payment):
self._payment = payment
Expand Down Expand Up @@ -301,8 +305,12 @@ def refund(self, amount=None):
)
provider = provider_factory(self.variant, self)
amount = provider.refund(self, amount)
# If the initial amount is None, the check above has no chance to check that the amount is greater than the captured amount before actually performing the refund.
# But since the refund has been performed already, raising an exception would just cause inconsistencies. Thus, logging an error.
if amount > self.captured_amount:
logger.error("Refund amount of payment %s greater than captured amount: %f > %f", self.id, amount, self.captured_amount)
self.captured_amount -= amount
if self.captured_amount == 0 and self.status != PaymentStatus.REFUNDED:
if self.captured_amount <= 0 and self.status != PaymentStatus.REFUNDED:
self.change_status(PaymentStatus.REFUNDED)
self.save()

Expand Down

0 comments on commit 7295cf4

Please sign in to comment.