Skip to content

Commit

Permalink
migrate to RecurringUserPlan.renewal_triggered_by
Browse files Browse the repository at this point in the history
  • Loading branch information
radekholy24 committed Apr 10, 2024
1 parent 786daa9 commit 368184f
Show file tree
Hide file tree
Showing 4 changed files with 89 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
-------

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
Expand Down
3 changes: 2 additions & 1 deletion plans_paypal/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
]
21 changes: 17 additions & 4 deletions plans_paypal/tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 368184f

Please sign in to comment.