Skip to content

Commit

Permalink
add common subscription methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Oct 23, 2021
1 parent 463f137 commit b6fad03
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
16 changes: 16 additions & 0 deletions payments/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ def get_return_url(self, payment, extra_data=None):
return url + "?" + qs
return url

def auto_complete_with_subscription(self, payment):
"""
Complete the payment with subscription
Used by providers, that use server initiated subscription workflow
Throws RedirectNeeded if there is problem with the payment that needs to be solved by user
"""
raise NotImplementedError()

def cancel_subscription(self, subscription):
"""
Cancel subscription
Used by providers, that use provider initiated subscription workflow
"""
raise NotImplementedError()

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

Expand Down
72 changes: 72 additions & 0 deletions payments/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import enum
import json
from typing import Iterable
from typing import Optional
from typing import Union
from uuid import uuid4

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


class BaseSubscription(models.Model):
token = models.CharField(
_("subscribtion token/id"),
help_text=_("Token/id used to identify subscription by provider"),
max_length=255,
default=None,
null=True,
blank=True,
)

class TimeUnit(enum.Enum):
year = "year"
month = "month"
day = "day"

def get_token(self) -> str:
return self.token

def set_data(self, token: str, **kwargs):
"""
Sets token and other values asociated with subscription
Kwargs can contain provider-specific values
"""
self.token = token

def get_period(self) -> int:
raise NotImplementedError()

def get_unit(self) -> TimeUnit:
raise NotImplementedError()

def cancel(self):
"""
Cancel the subscription
Used by providers, that use provider initiated subscription workflow
"""
provider = provider_factory(self.variant)
provider.cancel_subscription(self)

class Meta:
abstract = True


class BasePayment(models.Model):
"""
Represents a single transaction. Each instance has one or more PaymentItem.
Expand Down Expand Up @@ -144,6 +189,33 @@ def get_success_url(self) -> str:
def get_process_url(self) -> str:
return reverse("process_payment", kwargs={"token": self.token})

def get_payment_url(self) -> str:
"""
Get the url the view that handles the payment (payment_details() in documentation)
For now used only by PayU provider to redirect users back to CVV2 form
"""
raise NotImplementedError()

def get_subscrption(self) -> Optional[BaseSubscription]:
"""
Returns subscription object asociated with this payment
or None if the payment is not recurring
"""
return None

def is_recurring(self) -> bool:
return self.get_subscrption() is not None

def auto_complete_with_subscription(self):
"""
Complete the payment with subscription
Used by providers, that use server initiated subscription workflow
Throws RedirectNeeded if there is problem with the payment that needs to be solved by user
"""
provider = provider_factory(self.variant)
provider.auto_complete_with_subscription(self)

def capture(self, amount=None):
if self.status != PaymentStatus.PREAUTH:
raise ValueError("Only pre-authorized payments can be captured.")
Expand Down

0 comments on commit b6fad03

Please sign in to comment.