Skip to content

Commit a6ae4bc

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

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
@@ -13,6 +13,7 @@
1313
from . import FraudStatus
1414
from . import PaymentStatus
1515
from . import PurchasedItem
16+
from . import WalletStatus
1617
from .core import provider_factory
1718

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

4445

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

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

9870
class Meta:
9971
abstract = True
@@ -253,30 +225,18 @@ def get_payment_url(self) -> str:
253225
"""
254226
raise NotImplementedError
255227

256-
def get_subscription(self) -> BaseSubscription | None:
257-
"""
258-
Returns subscription object associated with this payment
259-
or None if the payment is not recurring
260-
"""
261-
return None
262-
263-
def is_recurring(self) -> bool:
264-
return self.get_subscription() is not None
265-
266-
def autocomplete_with_subscription(self):
228+
def autocomplete_with_wallet(self):
267229
"""
268-
Complete the payment with subscription
230+
Complete the payment with wallet
269231
270232
If the provider uses workflow such that the payments are initiated from
271233
implementer's side.
272-
Call this function right before the subscription end to
273-
make a new subscription payment.
274234
275235
Throws RedirectNeeded if there is problem with the payment
276236
that needs to be solved by user
277237
"""
278238
provider = provider_factory(self.variant)
279-
provider.autocomplete_with_subscription(self)
239+
provider.autocomplete_with_wallet(self)
280240

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

0 commit comments

Comments
 (0)