forked from uncloud/uncloud
[credit card] implement payment
This commit is contained in:
parent
e225bf1cc0
commit
1b06d8ee03
16 changed files with 290 additions and 64 deletions
|
|
@ -64,8 +64,8 @@ class Currency(models.TextChoices):
|
|||
Possible currencies to be billed
|
||||
"""
|
||||
CHF = 'CHF', _('Swiss Franc')
|
||||
EUR = 'EUR', _('Euro')
|
||||
USD = 'USD', _('US Dollar')
|
||||
# EUR = 'EUR', _('Euro')
|
||||
# USD = 'USD', _('US Dollar')
|
||||
|
||||
|
||||
def get_balance_for_user(user):
|
||||
|
|
@ -93,28 +93,30 @@ class StripeCustomer(models.Model):
|
|||
|
||||
|
||||
class StripeCreditCard(models.Model):
|
||||
owner = models.OneToOneField( get_user_model(),
|
||||
on_delete=models.CASCADE)
|
||||
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
||||
|
||||
card_name = models.CharField(null=False, max_length=128, default="My credit card")
|
||||
card_id = models.CharField(null=False, max_length=32)
|
||||
last4 = models.CharField(null=False, max_length=4)
|
||||
brand = models.CharField(null=False, max_length=64)
|
||||
expiry_date = models.DateField(null=False)
|
||||
active = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['owner'],
|
||||
condition=Q(active=True),
|
||||
name='one_active_card_per_user')
|
||||
]
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.card_name}: {self.brand} {self.last4} ({self.expiry_date})"
|
||||
|
||||
|
||||
###
|
||||
# Payments and Payment Methods.
|
||||
|
||||
class Payment(models.Model):
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE)
|
||||
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
||||
|
||||
amount = models.DecimalField(
|
||||
default=0.0,
|
||||
max_digits=AMOUNT_MAX_DIGITS,
|
||||
decimal_places=AMOUNT_DECIMALS,
|
||||
validators=[MinValueValidator(0)])
|
||||
|
|
@ -128,21 +130,18 @@ class Payment(models.Model):
|
|||
('unknown', 'Unknown')
|
||||
),
|
||||
default='unknown')
|
||||
timestamp = models.DateTimeField(editable=False, auto_now_add=True)
|
||||
|
||||
# We override save() in order to active products awaiting payment.
|
||||
def save(self, *args, **kwargs):
|
||||
# _state.adding is switched to false after super(...) call.
|
||||
being_created = self._state.adding
|
||||
timestamp = models.DateTimeField(default=timezone.now)
|
||||
|
||||
unpaid_bills_before_payment = Bill.get_unpaid_for(self.owner)
|
||||
super(Payment, self).save(*args, **kwargs) # Save payment in DB.
|
||||
unpaid_bills_after_payment = Bill.get_unpaid_for(self.owner)
|
||||
currency = models.CharField(max_length=32, choices=Currency.choices, default=Currency.CHF)
|
||||
|
||||
newly_paid_bills = list(
|
||||
set(unpaid_bills_before_payment) - set(unpaid_bills_after_payment))
|
||||
for bill in newly_paid_bills:
|
||||
bill.activate_products()
|
||||
external_reference = models.CharField(max_length=256, default="", null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.amount}{self.currency} from {self.owner} via {self.source} on {self.timestamp}"
|
||||
|
||||
###
|
||||
# Payments and Payment Methods.
|
||||
|
||||
|
||||
class PaymentMethod(models.Model):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue