diff --git a/HISTORY.rst b/HISTORY.rst index b3d474c..b23bd4a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,11 @@ History ------- +0.7.0 (xxxx-xx-xx) +++++++++++++++++++ +* migrate code to RecurringUserPlan.renewal_triggered_by +* migrate data of RecurringUserPlans with payment_provider="paypal-recurring" and renewal_triggered_by=TASK to renewal_triggered_by=OTHER + 0.6.0 (2023-03-22) +++++++++++++++++++ * Fix amount received on recurring payments, add tests diff --git a/plans_paypal/hooks.py b/plans_paypal/hooks.py index 936ed96..ae44bfd 100644 --- a/plans_paypal/hooks.py +++ b/plans_paypal/hooks.py @@ -4,6 +4,7 @@ from django.conf import settings from paypal.standard.ipn.signals import valid_ipn_received from paypal.standard.models import ST_PP_COMPLETED, ST_PP_PENDING +from plans.base.models import AbstractRecurringUserPlan from plans.models import Order from .models import PayPalPayment @@ -113,7 +114,7 @@ def receive_ipn(sender, **kwargs): token=ipn_obj.subscr_id, payment_provider="paypal-recurring" + ("-sandbox" if ipn_obj.test_ipn else ""), - has_automatic_renewal=True, + renewal_triggered_by=AbstractRecurringUserPlan.RENEWAL_TRIGGERED_BY.OTHER, token_verified=True, ) paypal_payment = PayPalPayment.objects.create( diff --git a/plans_paypal/migrations/0003_recurringuserplan_renewal_triggered_by_task_to_other.py b/plans_paypal/migrations/0003_recurringuserplan_renewal_triggered_by_task_to_other.py new file mode 100644 index 0000000..075859a --- /dev/null +++ b/plans_paypal/migrations/0003_recurringuserplan_renewal_triggered_by_task_to_other.py @@ -0,0 +1,65 @@ +# Generated by Django 4.2.11 on 2024-04-10 13:57 + +from enum import IntEnum + +from django.db import migrations + + +def _paypal_recurringuserplan_renewal_triggered_by_task_to_other(apps, schema_editor): + RecurringUserPlan = apps.get_model("plans", "RecurringUserPlan") + paypal_recurringuserplans_changed = ( + RecurringUserPlan.objects.select_for_update().filter( + payment_provider="paypal-recurring", + renewal_triggered_by=_RenewalTriggeredByEnum.TASK, + ) + ) + for paypal_recurringuserplan_changed in paypal_recurringuserplans_changed: + print( + "RecurringUserPlan's renewal_triggered_by will be overwritten:", + paypal_recurringuserplan_changed.pk, + ) + paypal_recurringuserplans_changed.update( + renewal_triggered_by=_RenewalTriggeredByEnum.OTHER + ) + + +def _paypal_recurringuserplan_renewal_triggered_by_other_to_task(apps, schema_editor): + RecurringUserPlan = apps.get_model("plans", "RecurringUserPlan") + paypal_recurringuserplans_changed = ( + RecurringUserPlan.objects.select_for_update().filter( + payment_provider="paypal-recurring", + renewal_triggered_by=_RenewalTriggeredByEnum.OTHER, + ) + ) + for paypal_recurringuserplan_changed in paypal_recurringuserplans_changed: + print( + "RecurringUserPlan's renewal_triggered_by will be overwritten:", + paypal_recurringuserplan_changed.pk, + ) + paypal_recurringuserplans_changed.update( + renewal_triggered_by=_RenewalTriggeredByEnum.TASK + ) + + +class _RenewalTriggeredByEnum(IntEnum): + OTHER = 1 + USER = 2 + TASK = 3 + + +class Migration(migrations.Migration): + + dependencies = [ + ("plans_paypal", "0002_auto_20211130_1117"), + ( + "plans", + "0014_recurringuserplan_has_automatic_renewal_backup_deprecated_to_renewal_triggered_by", + ), + ] + + operations = [ + migrations.RunPython( + _paypal_recurringuserplan_renewal_triggered_by_task_to_other, + reverse_code=_paypal_recurringuserplan_renewal_triggered_by_other_to_task, + ) + ] diff --git a/plans_paypal/tests/test_hooks.py b/plans_paypal/tests/test_hooks.py index afc2d15..fc7952f 100644 --- a/plans_paypal/tests/test_hooks.py +++ b/plans_paypal/tests/test_hooks.py @@ -4,6 +4,7 @@ from django.test import TestCase, override_settings from model_bakery import baker from paypal.standard.models import ST_PP_COMPLETED +from plans.base.models import AbstractRecurringUserPlan from plans.models import Invoice, Order from plans_paypal.hooks import parse_custom, receive_ipn @@ -94,7 +95,10 @@ def test_receive_ipn_completed(self): self.assertIsNone(user.userplan.recurring.tax) self.assertEqual(user.userplan.recurring.token, "") self.assertEqual(user.userplan.recurring.payment_provider, "paypal-recurring") - self.assertTrue(user.userplan.recurring.has_automatic_renewal) + self.assertEqual( + user.userplan.recurring.renewal_triggered_by, + AbstractRecurringUserPlan.RENEWAL_TRIGGERED_BY.OTHER, + ) self.assertTrue(user.userplan.recurring.token_verified) def test_receive_ipn_completed_order_completed(self): @@ -129,7 +133,10 @@ def test_receive_ipn_completed_order_completed(self): self.assertIsNone(user.userplan.recurring.tax) self.assertEqual(user.userplan.recurring.token, "") self.assertEqual(user.userplan.recurring.payment_provider, "paypal-recurring") - self.assertTrue(user.userplan.recurring.has_automatic_renewal) + self.assertEqual( + user.userplan.recurring.renewal_triggered_by, + AbstractRecurringUserPlan.RENEWAL_TRIGGERED_BY.OTHER, + ) self.assertTrue(user.userplan.recurring.token_verified) @override_settings( @@ -182,7 +189,10 @@ def test_receive_ipn_renewal(self): self.assertEqual(new_recurring_plan.tax, 12.0) self.assertEqual(new_recurring_plan.token, "") self.assertEqual(new_recurring_plan.payment_provider, "paypal-recurring") - self.assertTrue(new_recurring_plan.has_automatic_renewal) + self.assertEqual( + new_recurring_plan.renewal_triggered_by, + AbstractRecurringUserPlan.RENEWAL_TRIGGERED_BY.OTHER, + ) self.assertTrue(new_recurring_plan.token_verified) invoice = Invoice.objects.get(type=Invoice.INVOICE_TYPES.INVOICE) self.assertEqual(invoice.total, 112.00) @@ -223,7 +233,10 @@ def test_receive_ipn_renewal_wrong_amount(self): self.assertIsNone(user.userplan.recurring.tax) self.assertIsNone(user.userplan.recurring.token) self.assertIsNone(user.userplan.recurring.payment_provider) - self.assertFalse(user.userplan.recurring.has_automatic_renewal) + self.assertEqual( + user.userplan.recurring.renewal_triggered_by, + AbstractRecurringUserPlan.RENEWAL_TRIGGERED_BY.USER, + ) self.assertFalse(user.userplan.recurring.token_verified) def test_receive_ipn_cancellation(self):