Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.

Commit c423186

Browse files
committed
Replace the subscription logic with a wallet logic as per jazzband#217 (comment)
1 parent 4f9df42 commit c423186

File tree

3 files changed

+31
-64
lines changed

3 files changed

+31
-64
lines changed

payments/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ class FraudStatus:
6161
]
6262

6363

64+
class WalletStatus:
65+
PENDING = "pending"
66+
ACTIVE = "active"
67+
ERASED = "erased"
68+
69+
CHOICES = [
70+
(PENDING, pgettext_lazy("wallet status", "Pending")),
71+
(ACTIVE, pgettext_lazy("wallet status", "Active")),
72+
(ERASED, pgettext_lazy("wallet status", "Erased")),
73+
]
74+
75+
6476
class RedirectNeeded(Exception):
6577
pass
6678

payments/core.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,21 @@ def get_return_url(
143143
return url + "?" + qs
144144
return url
145145

146-
def autocomplete_with_subscription(self, payment):
146+
def autocomplete_with_wallet(self, payment):
147147
"""
148-
Complete the payment with subscription
148+
Complete the payment with wallet
149149
150150
If the provider uses workflow such that the payments are initiated from
151151
implementer's side.
152152
The users of django-payments will create a payment and call
153-
Payment.autocomplete_with_subscription() right before the subscription end.
153+
Payment.autocomplete_with_wallet().
154154
155155
Throws RedirectNeeded if there is problem with the payment
156156
that needs to be solved by user.
157157
"""
158158
raise NotImplementedError
159159

160-
def cancel_subscription(self, subscription):
161-
"""
162-
Cancel subscription
163-
Used by providers, that use provider initiated cancellation workflow
164-
"""
160+
def erase_wallet(self, wallet):
165161
raise NotImplementedError
166162

167163
def capture(self, payment, amount=None):

payments/models.py

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import enum
43
import json
54
from typing import Iterable
65
from uuid import uuid4
@@ -13,6 +12,7 @@
1312
from . import FraudStatus
1413
from . import PaymentStatus
1514
from . import PurchasedItem
15+
from . import WalletStatus
1616
from .core import provider_factory
1717

1818

@@ -40,58 +40,29 @@ def __setattr__(self, key, value):
4040
return None
4141

4242

43-
class BaseSubscription(models.Model):
43+
class BaseWallet(models.Model):
4444
token = models.CharField(
45-
_("subscription token/id"),
46-
help_text=_("Token/id used to identify subscription by provider"),
45+
_("wallet token/id"),
46+
help_text=_("Token/id used to identify wallet by provider"),
4747
max_length=255,
4848
default=None,
4949
null=True,
5050
blank=True,
5151
)
52-
payment_provider = models.CharField(
53-
_("payment provider"),
54-
help_text=_("Provider variant, that will be used for payment renewal"),
55-
max_length=255,
56-
default=None,
57-
null=True,
58-
blank=True,
52+
status = models.CharField(
53+
max_length=10, choices=WalletStatus.CHOICES, default=WalletStatus.PENDING
5954
)
60-
subscribtion_data = models.JSONField(
61-
_("subscription data"),
62-
help_text=_("Provider-specific data associated with subscription"),
55+
extra_data = models.JSONField(
56+
_("extra data"),
57+
help_text=_("Provider-specific data associated with wallet"),
6358
default=dict,
6459
)
6560

66-
class TimeUnit(enum.Enum):
67-
year = "year"
68-
month = "month"
69-
day = "day"
70-
71-
def set_recurrence(self, token: str, **kwargs):
72-
"""
73-
Sets token and other values associated with subscription recurrence
74-
Kwargs can contain provider-specific values
75-
"""
76-
self.token = token
77-
self.subscribtion_data = kwargs
78-
79-
def get_period(self) -> int:
80-
raise NotImplementedError
81-
82-
def get_unit(self) -> TimeUnit:
83-
raise NotImplementedError
84-
85-
def cancel(self):
61+
def payment_completed(self, payment):
8662
"""
87-
Cancel the subscription by provider
88-
Used by providers, that use provider initiated subscription workflow
89-
Implementer is responsible for cancelling the subscription model
90-
91-
Raises PaymentError if the cancellation didn't pass through
63+
Concrete implementation specific logic called whenever a payment is completed
64+
using this wallet.
9265
"""
93-
provider = provider_factory(self.variant)
94-
provider.cancel_subscription(self)
9566

9667
class Meta:
9768
abstract = True
@@ -251,30 +222,18 @@ def get_payment_url(self) -> str:
251222
"""
252223
raise NotImplementedError
253224

254-
def get_subscription(self) -> BaseSubscription | None:
255-
"""
256-
Returns subscription object associated with this payment
257-
or None if the payment is not recurring
258-
"""
259-
return None
260-
261-
def is_recurring(self) -> bool:
262-
return self.get_subscription() is not None
263-
264-
def autocomplete_with_subscription(self):
225+
def autocomplete_with_wallet(self):
265226
"""
266-
Complete the payment with subscription
227+
Complete the payment with wallet
267228
268229
If the provider uses workflow such that the payments are initiated from
269230
implementer's side.
270-
Call this function right before the subscription end to
271-
make a new subscription payment.
272231
273232
Throws RedirectNeeded if there is problem with the payment
274233
that needs to be solved by user
275234
"""
276235
provider = provider_factory(self.variant)
277-
provider.autocomplete_with_subscription(self)
236+
provider.autocomplete_with_wallet(self)
278237

279238
def capture(self, amount=None):
280239
"""Capture a pre-authorized payment.

0 commit comments

Comments
 (0)