Skip to content

Instantly share code, notes, and snippets.

@dhruvbaldawa
Created January 31, 2018 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhruvbaldawa/ca43367b85719566263b3304814646f3 to your computer and use it in GitHub Desktop.
Save dhruvbaldawa/ca43367b85719566263b3304814646f3 to your computer and use it in GitHub Desktop.
class Payment(models.Model):
STATE_CHOICES = (
('started', 'Started'),
('captured', 'Captured'),
('completed', 'Completed'),
)
state = models.CharField(choices=STATE_CHOICES, default='started', max_length=16)
def can_capture(self):
return self.state == 'started'
def can_complete(self):
return self.state == 'captured'
def capture(self):
if self.can_capture():
self.contact_payment_gateway()
self.state = 'captured'
self.save()
else:
raise ValueError()
def complete(self):
if self.can_complete():
self.state = 'completed'
self.save()
self.notify_user()
else:
raise ValueError()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment