|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import json
|
| 4 | +import logging |
4 | 5 | from typing import Iterable
|
5 | 6 | from uuid import uuid4
|
6 | 7 |
|
|
14 | 15 | from . import PurchasedItem
|
15 | 16 | from .core import provider_factory
|
16 | 17 |
|
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
17 | 20 |
|
18 | 21 | class PaymentAttributeProxy:
|
19 | 22 | def __init__(self, payment):
|
@@ -212,15 +215,23 @@ def release(self):
|
212 | 215 | def refund(self, amount=None):
|
213 | 216 | if self.status != PaymentStatus.CONFIRMED:
|
214 | 217 | raise ValueError("Only charged payments can be refunded.")
|
215 |
| - if amount: |
216 |
| - if amount > self.captured_amount: |
217 |
| - raise ValueError( |
218 |
| - "Refund amount can not be greater then captured amount" |
219 |
| - ) |
220 |
| - provider = provider_factory(self.variant, self) |
221 |
| - amount = provider.refund(self, amount) |
222 |
| - self.captured_amount -= amount |
223 |
| - if self.captured_amount == 0 and self.status != PaymentStatus.REFUNDED: |
| 218 | + if amount and amount > self.captured_amount: |
| 219 | + raise ValueError("Refund amount can not be greater then captured amount") |
| 220 | + provider = provider_factory(self.variant, self) |
| 221 | + amount = provider.refund(self, amount) |
| 222 | + # If the initial amount is None, the code above has no chance to check whether |
| 223 | + # the actual amount is greater than the captured amount before actually |
| 224 | + # performing the refund. But since the refund has been performed already, |
| 225 | + # raising an exception would just cause inconsistencies. Thus, logging an error. |
| 226 | + if amount > self.captured_amount: |
| 227 | + logger.error( |
| 228 | + "Refund amount of payment %s greater than captured amount: %f > %f", |
| 229 | + self.id, |
| 230 | + amount, |
| 231 | + self.captured_amount, |
| 232 | + ) |
| 233 | + self.captured_amount -= amount |
| 234 | + if self.captured_amount <= 0 and self.status != PaymentStatus.REFUNDED: |
224 | 235 | self.change_status(PaymentStatus.REFUNDED)
|
225 | 236 | self.save()
|
226 | 237 |
|
|
0 commit comments