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

@ -2,6 +2,9 @@ import stripe
import stripe.error
import logging
from django.core.exceptions import ObjectDoesNotExist
import uncloud_pay.models
import uncloud.secrets
# Static stripe configuration used below.
@ -79,11 +82,24 @@ class CreditCard():
# Actual Stripe logic.
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
except ObjectDoesNotExist:
# No entry yet - making a new one.
customer_request = create_customer(user.username, user.email)
if customer_request['error'] == None:
mapping = uncloud_pay.models.StripeCustomer.objects.create(
owner=user,
stripe_id=customer_request['response_object']['id']
)
return mapping.stripe_id
else:
return None
@handle_stripe_error
def create_card(customer_id, credit_card):
# Test settings
credit_card.number = "5555555555554444"
return stripe.Customer.create_source(
customer_id,
card={
@ -95,7 +111,7 @@ def create_card(customer_id, credit_card):
@handle_stripe_error
def get_card(customer_id, card_id):
return stripe.Card.retrieve_source(customer_id, card_id)
return stripe.Customer.retrieve_source(customer_id, card_id)
@handle_stripe_error
def charge_customer(amount, source):