Skip to content

Commit b6fad03

Browse files
committed
add common subscription methods
1 parent 463f137 commit b6fad03

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

payments/core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ def get_return_url(self, payment, extra_data=None):
119119
return url + "?" + qs
120120
return url
121121

122+
def auto_complete_with_subscription(self, payment):
123+
"""
124+
Complete the payment with subscription
125+
Used by providers, that use server initiated subscription workflow
126+
127+
Throws RedirectNeeded if there is problem with the payment that needs to be solved by user
128+
"""
129+
raise NotImplementedError()
130+
131+
def cancel_subscription(self, subscription):
132+
"""
133+
Cancel subscription
134+
Used by providers, that use provider initiated subscription workflow
135+
"""
136+
raise NotImplementedError()
137+
122138
def capture(self, payment, amount=None):
123139
raise NotImplementedError()
124140

payments/models.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import enum
12
import json
23
from typing import Iterable
4+
from typing import Optional
35
from typing import Union
46
from uuid import uuid4
57

@@ -36,6 +38,49 @@ def __setattr__(self, key, value):
3638
self._payment.extra_data = json.dumps(data)
3739

3840

41+
class BaseSubscription(models.Model):
42+
token = models.CharField(
43+
_("subscribtion token/id"),
44+
help_text=_("Token/id used to identify subscription by provider"),
45+
max_length=255,
46+
default=None,
47+
null=True,
48+
blank=True,
49+
)
50+
51+
class TimeUnit(enum.Enum):
52+
year = "year"
53+
month = "month"
54+
day = "day"
55+
56+
def get_token(self) -> str:
57+
return self.token
58+
59+
def set_data(self, token: str, **kwargs):
60+
"""
61+
Sets token and other values asociated with subscription
62+
Kwargs can contain provider-specific values
63+
"""
64+
self.token = token
65+
66+
def get_period(self) -> int:
67+
raise NotImplementedError()
68+
69+
def get_unit(self) -> TimeUnit:
70+
raise NotImplementedError()
71+
72+
def cancel(self):
73+
"""
74+
Cancel the subscription
75+
Used by providers, that use provider initiated subscription workflow
76+
"""
77+
provider = provider_factory(self.variant)
78+
provider.cancel_subscription(self)
79+
80+
class Meta:
81+
abstract = True
82+
83+
3984
class BasePayment(models.Model):
4085
"""
4186
Represents a single transaction. Each instance has one or more PaymentItem.
@@ -144,6 +189,33 @@ def get_success_url(self) -> str:
144189
def get_process_url(self) -> str:
145190
return reverse("process_payment", kwargs={"token": self.token})
146191

192+
def get_payment_url(self) -> str:
193+
"""
194+
Get the url the view that handles the payment (payment_details() in documentation)
195+
For now used only by PayU provider to redirect users back to CVV2 form
196+
"""
197+
raise NotImplementedError()
198+
199+
def get_subscrption(self) -> Optional[BaseSubscription]:
200+
"""
201+
Returns subscription object asociated with this payment
202+
or None if the payment is not recurring
203+
"""
204+
return None
205+
206+
def is_recurring(self) -> bool:
207+
return self.get_subscrption() is not None
208+
209+
def auto_complete_with_subscription(self):
210+
"""
211+
Complete the payment with subscription
212+
Used by providers, that use server initiated subscription workflow
213+
214+
Throws RedirectNeeded if there is problem with the payment that needs to be solved by user
215+
"""
216+
provider = provider_factory(self.variant)
217+
provider.auto_complete_with_subscription(self)
218+
147219
def capture(self, amount=None):
148220
if self.status != PaymentStatus.PREAUTH:
149221
raise ValueError("Only pre-authorized payments can be captured.")

0 commit comments

Comments
 (0)