Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
Fix not refunding the total amount by default
Browse files Browse the repository at this point in the history
  • Loading branch information
radekholy24 committed Apr 24, 2024
1 parent 3754fbb commit e92a3ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def refund(self, amount=None):
raise ValueError(
"Refund amount can not be greater then captured amount"
)
provider = provider_factory(self.variant, self)
amount = provider.refund(self, amount)
self.captured_amount -= amount
provider = provider_factory(self.variant, self)
amount = provider.refund(self, amount)
self.captured_amount -= amount
if self.captured_amount == 0 and self.status != PaymentStatus.REFUNDED:
self.change_status(PaymentStatus.REFUNDED)
self.save()
Expand Down
16 changes: 7 additions & 9 deletions payments/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,18 @@ def test_refund_too_high_amount(self):

@patch("payments.dummy.DummyProvider.refund")
def test_refund_without_amount(self, mocked_refund_method):
refund_amount = None
captured_amount = Decimal("200")
with patch.object(BasePayment, "save") as mocked_save_method:
mocked_save_method.return_value = None
mocked_refund_method.return_value = refund_amount
mocked_refund_method.return_value = captured_amount

captured_amount = Decimal("200")
status = PaymentStatus.CONFIRMED
payment = Payment(
variant="default", status=status, captured_amount=captured_amount
variant="default", status=PaymentStatus.CONFIRMED, captured_amount=captured_amount
)
payment.refund(refund_amount)
self.assertEqual(payment.status, status)
self.assertEqual(payment.captured_amount, captured_amount)
self.assertEqual(mocked_refund_method.call_count, 0)
payment.refund()
self.assertEqual(payment.status, PaymentStatus.REFUNDED)
self.assertEqual(payment.captured_amount, Decimal(0))
self.assertEqual(mocked_refund_method.call_count, 1)

@patch("payments.dummy.DummyProvider.refund")
def test_refund_partial_success(self, mocked_refund_method):
Expand Down

0 comments on commit e92a3ad

Please sign in to comment.