Skip to content

Commit 444dbdc

Browse files
committed
Make a basic version of subscriber_sync
1 parent 08d848d commit 444dbdc

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
sync_subscribers_from_paddle command.
3+
"""
4+
from django.core.management.base import BaseCommand
5+
6+
from ...models import Subscription
7+
8+
9+
class Command(BaseCommand):
10+
"""Sync subscribers from paddle."""
11+
12+
help = "Sync subscribers from paddle."
13+
14+
def handle(self, *args, **options):
15+
"""Call sync_from_paddle_data for each subscriber returned by api_list."""
16+
for sub_data in Subscription.api_list():
17+
sub = Subscription.sync_from_paddle_data(sub_data)
18+
print("Synchronized {0}".format(str(sub)))

djpaddle/models.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.db.models.signals import post_save
33
from django.utils.translation import gettext_lazy as _
44
from django.dispatch import receiver
5+
from django.utils import timezone
56

67
from . import settings, signals, api
78
from .fields import PaddleCurrencyCodeField
@@ -123,6 +124,7 @@ class Subscription(PaddleBaseModel):
123124
(STATUS_PAUSED, _("paused")),
124125
(STATUS_DELETED, _("deleted")),
125126
)
127+
PADDLE_URI_LIST = 'subscription/users'
126128

127129
id = models.CharField(max_length=32, primary_key=True)
128130
subscriber = models.ForeignKey(
@@ -151,6 +153,51 @@ class Subscription(PaddleBaseModel):
151153
class Meta:
152154
ordering = ["created_at"]
153155

156+
@classmethod
157+
def api_list(cls):
158+
return api.retrieve(uri=cls.PADDLE_URI_LIST)
159+
160+
@classmethod
161+
def sync_from_paddle_data(cls, data):
162+
pk = data.get('subscription_id', None)
163+
# First, find and drop current sub with this data.
164+
cls.objects.filter(pk=pk).delete()
165+
kwargs = {}
166+
167+
try:
168+
kwargs['subscriber'] = settings.get_subscriber_model().objects.get(
169+
email=data["user_email"]
170+
)
171+
except settings.get_subscriber_model().DoesNotExist:
172+
pass
173+
174+
try:
175+
kwargs['plan'] = Plan.objects.get(pk=data.get("plan_id"))
176+
except Plan.DoesNotExist:
177+
print("Skipping, plan not found.")
178+
return
179+
180+
# Now, create object with this pk.
181+
sub = Subscription.objects.create(
182+
id=pk,
183+
cancel_url=data.get("cancel_url"),
184+
checkout_id="", # ???
185+
currency=data.get('last_payment').get("currency"),
186+
email=data.get("user_email"),
187+
event_time=timezone.now(), # ???
188+
marketing_consent=data.get('marketing_consent'),
189+
# next_bill_date can be null if user won't pay again...
190+
next_bill_date=data.get("next_payment", {}).get("date", timezone.now()),
191+
passthrough="", # ???
192+
quantity=data.get("last_payment", {}).get("amount", 0),
193+
source="", # ???
194+
status=data.get("state"),
195+
unit_price=0.00, # ???
196+
update_url = data.get('update_url'),
197+
**kwargs
198+
)
199+
return sub
200+
154201
@classmethod
155202
def _sanitize_webhook_payload(cls, payload):
156203
data = {}
@@ -160,10 +207,14 @@ def _sanitize_webhook_payload(cls, payload):
160207
# transform `user_id` to subscriber ref
161208
data["subscriber"] = None
162209
subscriber_id = payload.pop("user_id", None)
163-
if subscriber_id not in ["", None]:
164-
data["subscriber"], created = settings.get_subscriber_model().objects.get_or_create(
165-
email=payload["email"]
166-
)
210+
try:
211+
if subscriber_id not in ["", None]:
212+
data[
213+
"subscriber"], created = settings.get_subscriber_model().objects.get(
214+
email=payload["email"]
215+
)
216+
except settings.get_subscriber_model().DoesNotExist:
217+
pass
167218

168219
# transform `subscription_plan_id` to plan ref
169220
data["plan"] = None

0 commit comments

Comments
 (0)