Skip to content

Commit 2298205

Browse files
radekholy24PetrDlouhy
authored andcommitted
Replace the subscription logic with a wallet logic as per jazzband#217 (comment)
1 parent edff5b3 commit 2298205

File tree

3 files changed

+34
-64
lines changed

3 files changed

+34
-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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from django.core.exceptions import ImproperlyConfigured
1010
from django.utils.module_loading import import_string
1111

12+
from . import WalletStatus
13+
1214
if TYPE_CHECKING:
1315
from django.http import HttpRequest
1416

@@ -143,26 +145,22 @@ def get_return_url(
143145
return url + "?" + qs
144146
return url
145147

146-
def autocomplete_with_subscription(self, payment):
148+
def autocomplete_with_wallet(self, payment):
147149
"""
148-
Complete the payment with subscription
150+
Complete the payment with wallet
149151
150152
If the provider uses workflow such that the payments are initiated from
151153
implementer's side.
152154
The users of django-payments will create a payment and call
153-
Payment.autocomplete_with_subscription() right before the subscription end.
155+
Payment.autocomplete_with_wallet().
154156
155157
Throws RedirectNeeded if there is problem with the payment
156158
that needs to be solved by user.
157159
"""
158160
raise NotImplementedError
159161

160-
def cancel_subscription(self, subscription):
161-
"""
162-
Cancel subscription
163-
Used by providers, that use provider initiated cancellation workflow
164-
"""
165-
raise NotImplementedError
162+
def erase_wallet(self, wallet):
163+
wallet.status = WalletStatus.ERASED
166164

167165
def capture(self, payment, amount=None):
168166
raise NotImplementedError

payments/models.py

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from . import FraudStatus
1515
from . import PaymentStatus
1616
from . import PurchasedItem
17+
from . import WalletStatus
1718
from .core import provider_factory
1819

1920
logger = logging.getLogger(__name__)
@@ -43,58 +44,29 @@ def __setattr__(self, key, value):
4344
return None
4445

4546

46-
class BaseSubscription(models.Model):
47+
class BaseWallet(models.Model):
4748
token = models.CharField(
48-
_("subscription token/id"),
49-
help_text=_("Token/id used to identify subscription by provider"),
49+
_("wallet token/id"),
50+
help_text=_("Token/id used to identify wallet by provider"),
5051
max_length=255,
5152
default=None,
5253
null=True,
5354
blank=True,
5455
)
55-
payment_provider = models.CharField(
56-
_("payment provider"),
57-
help_text=_("Provider variant, that will be used for payment renewal"),
58-
max_length=255,
59-
default=None,
60-
null=True,
61-
blank=True,
56+
status = models.CharField(
57+
max_length=10, choices=WalletStatus.CHOICES, default=WalletStatus.PENDING
6258
)
63-
subscribtion_data = models.JSONField(
64-
_("subscription data"),
65-
help_text=_("Provider-specific data associated with subscription"),
59+
extra_data = models.JSONField(
60+
_("extra data"),
61+
help_text=_("Provider-specific data associated with wallet"),
6662
default=dict,
6763
)
6864

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

9971
class Meta:
10072
abstract = True
@@ -254,30 +226,18 @@ def get_payment_url(self) -> str:
254226
"""
255227
raise NotImplementedError
256228

257-
def get_subscription(self) -> BaseSubscription | None:
258-
"""
259-
Returns subscription object associated with this payment
260-
or None if the payment is not recurring
261-
"""
262-
return None
263-
264-
def is_recurring(self) -> bool:
265-
return self.get_subscription() is not None
266-
267-
def autocomplete_with_subscription(self):
229+
def autocomplete_with_wallet(self):
268230
"""
269-
Complete the payment with subscription
231+
Complete the payment with wallet
270232
271233
If the provider uses workflow such that the payments are initiated from
272234
implementer's side.
273-
Call this function right before the subscription end to
274-
make a new subscription payment.
275235
276236
Throws RedirectNeeded if there is problem with the payment
277237
that needs to be solved by user
278238
"""
279239
provider = provider_factory(self.variant)
280-
provider.autocomplete_with_subscription(self)
240+
provider.autocomplete_with_wallet(self)
281241

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

0 commit comments

Comments
 (0)