Properly wire stripe card to payment methods

This commit is contained in:
fnux 2020-03-03 16:55:56 +01:00
commit 94a39ed81d
6 changed files with 117 additions and 43 deletions

View file

@ -7,6 +7,7 @@ from django.utils import timezone
from math import ceil
from datetime import timedelta
from calendar import monthrange
import uncloud_pay.stripe
from decimal import Decimal
import uuid
@ -68,6 +69,20 @@ class PaymentMethod(models.Model):
# Only used for "Stripe" source
stripe_card_id = models.CharField(max_length=32, blank=True, null=True)
@property
def stripe_card_last4(self):
if self.source == 'stripe':
card_request = uncloud_pay.stripe.get_card(
StripeCustomer.objects.get(owner=self.owner).stripe_id,
self.stripe_card_id)
if card_request['error'] == None:
return card_request['response_object']['last4']
else:
return None
else:
return None
def charge(self, amount):
if amount > 0: # Make sure we don't charge negative amount by errors...
if self.source == 'stripe':
@ -85,6 +100,11 @@ class PaymentMethod(models.Model):
class Meta:
unique_together = [['owner', 'primary']]
class StripeCustomer(models.Model):
owner = models.OneToOneField( get_user_model(),
primary_key=True,
on_delete=models.CASCADE)
stripe_id = models.CharField(max_length=32)
###
# Bills & Payments.