Skip to content

Commit

Permalink
Merge pull request #8 from radekholy24/refunds_are_delayed
Browse files Browse the repository at this point in the history
fix multiple deduction of the refund amount from payment.captured_amount
  • Loading branch information
PetrDlouhy authored May 14, 2024
2 parents b86bbd3 + 32f26a5 commit 0a141da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
History
-------


1.4.2 (2024-05-14)
******************
* fix multiple deduction of the refund amount from `payment.captured_amount`
* change statuses of payments refunded with an amount greater than `payment.captured_amount` to `REFUNDED` instead of just deducing `captured_amount`

1.4.1 (2024-05-14)
******************
* fix captured_amount not being saved when processing data
Expand Down
26 changes: 23 additions & 3 deletions payments_payu/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,16 @@ def process_notification(self, payment, request):
print(refunded_price, payment.total)
if data["refund"]["status"] == "FINALIZED":
payment.message += data["refund"]["reasonDescription"]
if refunded_price == payment.captured_amount:
if refunded_price >= payment.captured_amount:
if refunded_price > payment.captured_amount:
logger.error(
"refund %s of payment %s has amount greater than the payment's captured_amount: "
"%f > %f",
data["refund"].get("refundId", "???"),
payment.id,
refunded_price,
payment.captured_amount,
)
payment.change_status(PaymentStatus.REFUNDED)
else:
payment.captured_amount -= refunded_price
Expand Down Expand Up @@ -692,7 +701,11 @@ def refund(self, payment, amount=None):
)
if refund_status == "CANCELED":
raise ValueError(f"refund {refund_id} of payment {payment.id} canceled")
elif refund_status not in {"PENDING", "FINALIZED"}:
elif refund_status == "FINALIZED":
raise NotImplementedError(
f"refund {refund_id} of payment {payment.id} being FINALIZED already is not supported yet"
)
elif refund_status not in {"PENDING"}:
raise PayuApiError(
f"invalid status of refund {refund_id} of payment {payment.id}"
)
Expand All @@ -701,7 +714,14 @@ def refund(self, payment, amount=None):
f"refund {refund_id} of payment {payment.id} in different currency not supported yet: "
f"{refund_currency}"
)
return refund_amount
if amount is not None and refund_amount != amount:
raise NotImplementedError(
f"refund {refund_id} of payment {payment.id} having a different amount than requested not supported "
f"yet: {refund_amount}"
)
# Return 0 in order not to change captured_amount yet. If we returned the amount, captured_amount would change
# twice (now and once we get a notification from PayU).
return Decimal(0)


class PaymentProcessor(object):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_payu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ def test_refund(self):
amount = self.provider.refund(self.payment, Decimal(110))

self.assertEqual(refund_request_mock.call_count, 1)
self.assertEqual(amount, Decimal(110))
self.assertEqual(amount, Decimal(0))
self.assertEqual(self.payment.total, Decimal(220))
self.assertEqual(self.payment.captured_amount, Decimal(210))
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
Expand Down Expand Up @@ -1417,7 +1417,7 @@ def test_refund_no_amount(self):
amount = self.provider.refund(self.payment)

self.assertEqual(refund_request_mock.call_count, 1)
self.assertEqual(amount, Decimal(220))
self.assertEqual(amount, Decimal(0))
self.assertEqual(self.payment.total, Decimal(220))
self.assertEqual(self.payment.captured_amount, Decimal(220))
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
Expand Down Expand Up @@ -1482,7 +1482,7 @@ def test_refund_no_get_refund_ext_id(self):
amount = self.provider.refund(self.payment, Decimal(110))

self.assertEqual(refund_request_mock.call_count, 1)
self.assertEqual(amount, Decimal(110))
self.assertEqual(amount, Decimal(0))
self.assertEqual(self.payment.total, Decimal(220))
self.assertEqual(self.payment.captured_amount, Decimal(220))
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
Expand Down Expand Up @@ -1544,7 +1544,7 @@ def test_refund_no_ext_id(self):
amount = self.provider.refund(self.payment, Decimal(110))

self.assertEqual(refund_request_mock.call_count, 1)
self.assertEqual(amount, Decimal(110))
self.assertEqual(amount, Decimal(0))
self.assertEqual(self.payment.total, Decimal(220))
self.assertEqual(self.payment.captured_amount, Decimal(220))
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
Expand Down Expand Up @@ -1608,7 +1608,7 @@ def test_refund_no_ext_id_twice(self):

self.assertEqual(refund_request_mock.call_count, 2)
self.assertEqual(amount2, amount1)
self.assertEqual(amount2, Decimal(200))
self.assertEqual(amount2, Decimal(0))
self.assertEqual(self.payment.total, Decimal(220))
self.assertEqual(self.payment.captured_amount, Decimal(220))
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
Expand All @@ -1626,7 +1626,7 @@ def test_refund_no_ext_id_twice(self):
)
self.assertFalse(caught_warnings)

def test_refund_finalized(self):
def test_refund_pending(self):
with warnings.catch_warnings(record=True) as caught_warnings:
warnings.simplefilter("always")
self.set_up_provider(
Expand All @@ -1648,7 +1648,7 @@ def test_refund_finalized(self):
"currencyCode": "USD",
"description": "desc 1234 110",
"creationDateTime": "2020-07-02T09:19:03.896+02:00",
"status": "FINALIZED",
"status": "PENDING",
"statusDateTime": "2020-07-02T09:19:04.013+02:00",
},
"status": {
Expand All @@ -1671,7 +1671,7 @@ def test_refund_finalized(self):
amount = self.provider.refund(self.payment, Decimal(110))

self.assertEqual(refund_request_mock.call_count, 1)
self.assertEqual(amount, Decimal(110))
self.assertEqual(amount, Decimal(0))
self.assertEqual(self.payment.total, Decimal(220))
self.assertEqual(self.payment.captured_amount, Decimal(220))
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
Expand Down

0 comments on commit 0a141da

Please sign in to comment.