Skip to content

Commit 249c784

Browse files
radekholy24PetrDlouhy
authored andcommitted
Replace the subscription logic with a wallet logic as per #217 (comment)
1 parent 49dd519 commit 249c784

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
@@ -17,6 +17,7 @@
1717
from . import FraudStatus
1818
from . import PaymentStatus
1919
from . import PurchasedItem
20+
from . import WalletStatus
2021
from .core import provider_factory
2122

2223
logger = logging.getLogger(__name__)
@@ -46,58 +47,29 @@ def __setattr__(self, key, value):
4647
return None
4748

4849

49-
class BaseSubscription(models.Model):
50+
class BaseWallet(models.Model):
5051
token = models.CharField(
51-
_("subscription token/id"),
52-
help_text=_("Token/id used to identify subscription by provider"),
52+
_("wallet token/id"),
53+
help_text=_("Token/id used to identify wallet by provider"),
5354
max_length=255,
5455
default=None,
5556
null=True,
5657
blank=True,
5758
)
58-
payment_provider = models.CharField(
59-
_("payment provider"),
60-
help_text=_("Provider variant, that will be used for payment renewal"),
61-
max_length=255,
62-
default=None,
63-
null=True,
64-
blank=True,
59+
status = models.CharField(
60+
max_length=10, choices=WalletStatus.CHOICES, default=WalletStatus.PENDING
6561
)
66-
subscribtion_data = models.JSONField(
67-
_("subscription data"),
68-
help_text=_("Provider-specific data associated with subscription"),
62+
extra_data = models.JSONField(
63+
_("extra data"),
64+
help_text=_("Provider-specific data associated with wallet"),
6965
default=dict,
7066
)
7167

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

10274
class Meta:
10375
abstract = True
@@ -257,30 +229,18 @@ def get_payment_url(self) -> str:
257229
"""
258230
raise NotImplementedError
259231

260-
def get_subscription(self) -> Optional[BaseSubscription]:
261-
"""
262-
Returns subscription object associated with this payment
263-
or None if the payment is not recurring
264-
"""
265-
return None
266-
267-
def is_recurring(self) -> bool:
268-
return self.get_subscription() is not None
269-
270-
def autocomplete_with_subscription(self):
232+
def autocomplete_with_wallet(self):
271233
"""
272-
Complete the payment with subscription
234+
Complete the payment with wallet
273235
274236
If the provider uses workflow such that the payments are initiated from
275237
implementer's side.
276-
Call this function right before the subscription end to
277-
make a new subscription payment.
278238
279239
Throws RedirectNeeded if there is problem with the payment
280240
that needs to be solved by user
281241
"""
282242
provider = provider_factory(self.variant)
283-
provider.autocomplete_with_subscription(self)
243+
provider.autocomplete_with_wallet(self)
284244

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

0 commit comments

Comments
 (0)