Skip to content

Commit

Permalink
add optional returning orders when payments are refunded
Browse files Browse the repository at this point in the history
  • Loading branch information
radekholy24 committed Apr 9, 2024
1 parent 407825b commit fe2c7c5
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
History
-------

1.3.0 (Unreleased)
++++++++++++++++++

* add optional returning orders when payments are refunded

1.2.2 (2023-12-20)
++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Django plans payments
:target: https://codecov.io/gh/PetrDlouhy/django-plans-payments

Almost automatic integration between `django-plans <https://github.com/django-getpaid/django-plans>`_ and `django-payments <https://github.com/mirumee/django-payments>`_.
This will add payment buttons to the order page and automatically confirm the `Order` after the payment.
This will add payment buttons to the order page and automatically confirm the `Order` after the payment. Optionally, it can return the corresponding order when a payment is refunded.

Documentation
-------------
Expand Down
6 changes: 6 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ Add Django plans payments's URL patterns:
url(r'^', include(plans_payments_urls)),
...
]
To enable returning orders when payments are refunded, set `PLANS_PAYMENTS_RETURN_ORDER_WHEN_PAYMENT_REFUNDED` to `True` in your settings.

.. code-block:: python
PLANS_PAYMENTS_RETURN_ORDER_WHEN_PAYMENT_REFUNDED = True
10 changes: 9 additions & 1 deletion plans_payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ def change_payment_status(sender, *args, **kwargs):
order.user.userplan.recurring.token_verified = True
order.user.userplan.recurring.save()
order.complete_order()
if order.status != Order.STATUS.COMPLETED and payment.status not in (
if (
getattr(settings, "PLANS_PAYMENTS_RETURN_ORDER_WHEN_PAYMENT_REFUNDED", False)
and payment.status == PaymentStatus.REFUNDED
):
order._change_reason = (
f"Django-plans-payments: Payment status changed to {payment.status}"
)
order.return_order()
elif order.status != Order.STATUS.COMPLETED and payment.status not in (
PaymentStatus.CONFIRMED,
PaymentStatus.WAITING,
PaymentStatus.INPUT,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Additional requirements go here

django-plans
django-plans>=1.0.7
django-payments
django-related-admin
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_version(*file_paths):
],
include_package_data=True,
install_requires=[
"django-plans",
"django-plans>=1.0.7",
"django-payments",
],
license="MIT",
Expand Down
51 changes: 50 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from decimal import Decimal

import pytz
from django.test import TestCase
from django.test import TestCase, override_settings
from freezegun import freeze_time
from model_bakery import baker
from payments import PaymentStatus
Expand Down Expand Up @@ -275,6 +275,17 @@ def test_change_payment_status_refunded(self):
self.assertEqual(p.status, "refunded")
self.assertEqual(p.order.status, Order.STATUS.COMPLETED)

@override_settings(PLANS_PAYMENTS_RETURN_ORDER_WHEN_PAYMENT_REFUNDED=True)
def test_change_payment_status_refunded_return_enabled(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.COMPLETED),
status=PaymentStatus.REFUNDED,
)
baker.make("UserPlan", user=p.order.user)
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "refunded")
self.assertEqual(p.order.status, Order.STATUS.RETURNED)

def test_change_payment_status_refunded_order_not_valid(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.NOT_VALID),
Expand All @@ -285,6 +296,44 @@ def test_change_payment_status_refunded_order_not_valid(self):
self.assertEqual(p.status, "refunded")
self.assertEqual(p.order.status, Order.STATUS.CANCELED)

@override_settings(PLANS_PAYMENTS_RETURN_ORDER_WHEN_PAYMENT_REFUNDED=True)
def test_change_payment_status_refunded_order_not_valid_return_enabled(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.NOT_VALID),
status=PaymentStatus.REFUNDED,
)
baker.make("UserPlan", user=p.order.user)
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "refunded")
self.assertEqual(p.order.status, Order.STATUS.RETURNED)

# This should not happen practically but with Django Admin, anything can happen...
def test_change_payment_status_refunded_order_canceled(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.CANCELED),
status=PaymentStatus.REFUNDED,
)
baker.make("UserPlan", user=p.order.user)
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "refunded")
self.assertEqual(p.order.status, Order.STATUS.CANCELED)

# This should not happen practically but with Django Admin, anything can happen...
@override_settings(PLANS_PAYMENTS_RETURN_ORDER_WHEN_PAYMENT_REFUNDED=True)
def test_change_payment_status_refunded_order_canceled_return_enabled(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.CANCELED),
status=PaymentStatus.REFUNDED,
)
baker.make("UserPlan", user=p.order.user)
with self.assertRaisesRegex(
ValueError,
r"^Cannot return order with status other than COMPLETED and NOT_VALID: 4$",
):
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "refunded")
self.assertEqual(p.order.status, Order.STATUS.CANCELED)

def test_change_payment_status_confirmed_no_recurring(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.NEW),
Expand Down

0 comments on commit fe2c7c5

Please sign in to comment.