forked from uncloud/uncloud
implement credit card listing
This commit is contained in:
parent
e2c4a19049
commit
e225bf1cc0
12 changed files with 183 additions and 63 deletions
|
|
@ -1,11 +1,13 @@
|
|||
import stripe
|
||||
import stripe.error
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
import uncloud_pay.models
|
||||
from .models import StripeCustomer, StripeCreditCard
|
||||
|
||||
CURRENCY = 'chf'
|
||||
|
||||
|
|
@ -56,12 +58,12 @@ def public_api_key():
|
|||
def get_customer_id_for(user):
|
||||
try:
|
||||
# .get() raise if there is no matching entry.
|
||||
return uncloud_pay.models.StripeCustomer.objects.get(owner=user).stripe_id
|
||||
return StripeCustomer.objects.get(owner=user).stripe_id
|
||||
except ObjectDoesNotExist:
|
||||
# No entry yet - making a new one.
|
||||
try:
|
||||
customer = create_customer(user.username, user.email)
|
||||
uncloud_stripe_mapping = uncloud_pay.models.StripeCustomer.objects.create(
|
||||
uncloud_stripe_mapping = StripeCustomer.objects.create(
|
||||
owner=user, stripe_id=customer.id)
|
||||
return uncloud_stripe_mapping.stripe_id
|
||||
except Exception as e:
|
||||
|
|
@ -109,6 +111,7 @@ def get_customer_cards(customer_id):
|
|||
customer=customer_id,
|
||||
type="card",
|
||||
)
|
||||
print(stripe_cards["data"])
|
||||
|
||||
for stripe_card in stripe_cards["data"]:
|
||||
card = {}
|
||||
|
|
@ -116,8 +119,24 @@ def get_customer_cards(customer_id):
|
|||
card['last4'] = stripe_card["card"]["last4"]
|
||||
card['month'] = stripe_card["card"]["exp_month"]
|
||||
card['year'] = stripe_card["card"]["exp_year"]
|
||||
card['id'] = stripe_card["card"]["id"]
|
||||
card['id'] = stripe_card["id"]
|
||||
|
||||
cards.append(card)
|
||||
|
||||
return cards
|
||||
|
||||
def sync_cards_for_user(user):
|
||||
customer_id = get_customer_id_for(user)
|
||||
cards = get_customer_cards(customer_id)
|
||||
|
||||
for card in cards:
|
||||
StripeCreditCard.objects.get_or_create(card_id=card['id'],
|
||||
owner = user,
|
||||
defaults = {
|
||||
'last4': card['last4'],
|
||||
'brand': card['brand'],
|
||||
'expiry_date': datetime.date(card['year'],
|
||||
card['month'],
|
||||
1)
|
||||
}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue