From 0e527e1da02865f81de56d99e70343c5d1d4dbd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Hol=C3=BD?= Date: Wed, 24 Apr 2024 19:42:59 +0200 Subject: [PATCH] Fix not refunding the total amount by default Fixes https://github.com/jazzband/django-payments/issues/401 --- CHANGELOG.rst | 1 + payments/models.py | 6 +++--- payments/test_core.py | 18 +++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 01561ac69..daea9e7d0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,7 @@ v3.0.0 - Added support for Python 3.11, Django 4.1 and Django 4.2. - Stripe backends now supports webhooks - New :ref:`webhook settings ` +- Fixed ``base_payment.refund()`` not making any refund v2.0.0 ------ diff --git a/payments/models.py b/payments/models.py index 52e7e76da..21d84e973 100644 --- a/payments/models.py +++ b/payments/models.py @@ -217,9 +217,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() diff --git a/payments/test_core.py b/payments/test_core.py index 8c9190dd6..0ff939452 100644 --- a/payments/test_core.py +++ b/payments/test_core.py @@ -113,20 +113,20 @@ 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):