Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional returning orders when payments are refunded #8

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development Lead
Contributors
------------

None yet. Why not be the first?
* BlenderKit <[email protected]>
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
85 changes: 84 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 @@ -208,6 +208,7 @@ def test_change_payment_status(self):
p = models.Payment(order=baker.make("Order", status=Order.STATUS.NEW))
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "waiting")
self.assertEqual(p.order.status, Order.STATUS.NEW)

def test_change_payment_status_confirmed(self):
p = models.Payment(
Expand All @@ -219,6 +220,7 @@ def test_change_payment_status_confirmed(self):
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "confirmed")
self.assertEqual(recurring_user_plan.token_verified, True)
self.assertEqual(p.order.status, Order.STATUS.COMPLETED)

@freeze_time("2018-01-01")
def test_change_payment_status_confirmed_double_submit(self):
Expand Down Expand Up @@ -251,6 +253,86 @@ def test_change_payment_status_rejected(self):
baker.make("RecurringUserPlan", user_plan=userplan)
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "rejected")
self.assertEqual(p.order.status, Order.STATUS.CANCELED)

def test_change_payment_status_rejected_order_completed(self):
p = models.Payment(
order=baker.make("Order", status=Order.STATUS.COMPLETED),
status=PaymentStatus.REJECTED,
)
baker.make("UserPlan", user=p.order.user)
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "rejected")
self.assertEqual(p.order.status, Order.STATUS.COMPLETED)

def test_change_payment_status_refunded(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.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),
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)

@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(
Expand All @@ -260,6 +342,7 @@ def test_change_payment_status_confirmed_no_recurring(self):
baker.make("UserPlan", user=p.order.user)
models.change_payment_status("sender", instance=p)
self.assertEqual(p.status, "confirmed")
self.assertEqual(p.order.status, Order.STATUS.COMPLETED)

def test_renew_accounts_no_variant(self):
p = models.Payment()
Expand Down
Loading