2020-02-21 09:41:22 +00:00
|
|
|
import stripe
|
2020-03-02 21:26:40 +00:00
|
|
|
import stripe.error
|
|
|
|
import logging
|
2020-12-28 22:35:34 +00:00
|
|
|
import datetime
|
2020-02-21 09:41:22 +00:00
|
|
|
|
2020-12-29 00:43:33 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
2020-05-01 22:16:29 +00:00
|
|
|
from django.conf import settings
|
2020-12-28 22:35:34 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-03-03 15:55:56 +00:00
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
from .models import StripeCustomer, StripeCreditCard
|
2020-03-02 21:26:40 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-03-02 21:26:40 +00:00
|
|
|
CURRENCY = 'chf'
|
|
|
|
|
2020-05-01 22:16:29 +00:00
|
|
|
stripe.api_key = settings.STRIPE_KEY
|
2020-03-02 21:26:40 +00:00
|
|
|
|
2020-02-21 09:41:22 +00:00
|
|
|
def handle_stripe_error(f):
|
|
|
|
def handle_problems(*args, **kwargs):
|
|
|
|
response = {
|
|
|
|
'paid': False,
|
|
|
|
'response_object': None,
|
|
|
|
'error': None
|
|
|
|
}
|
|
|
|
|
2020-03-05 10:03:47 +00:00
|
|
|
common_message = "Currently it is not possible to make payments. Please try agin later."
|
2020-02-21 09:41:22 +00:00
|
|
|
try:
|
|
|
|
response_object = f(*args, **kwargs)
|
2020-03-05 10:03:47 +00:00
|
|
|
return response_object
|
2020-02-21 09:41:22 +00:00
|
|
|
except stripe.error.CardError as e:
|
|
|
|
# Since it's a decline, stripe.error.CardError will be caught
|
|
|
|
body = e.json_body
|
|
|
|
logging.error(str(e))
|
2020-03-05 10:03:47 +00:00
|
|
|
|
|
|
|
raise e # For error handling.
|
2020-02-21 09:41:22 +00:00
|
|
|
except stripe.error.RateLimitError:
|
2020-03-05 10:03:47 +00:00
|
|
|
logging.error("Too many requests made to the API too quickly.")
|
|
|
|
raise Exception(common_message)
|
2020-02-21 09:41:22 +00:00
|
|
|
except stripe.error.InvalidRequestError as e:
|
|
|
|
logging.error(str(e))
|
2020-03-05 10:03:47 +00:00
|
|
|
raise Exception('Invalid parameters.')
|
2020-02-21 09:41:22 +00:00
|
|
|
except stripe.error.AuthenticationError as e:
|
|
|
|
# Authentication with Stripe's API failed
|
|
|
|
# (maybe you changed API keys recently)
|
|
|
|
logging.error(str(e))
|
2020-03-05 10:03:47 +00:00
|
|
|
raise Exception(common_message)
|
2020-02-21 09:41:22 +00:00
|
|
|
except stripe.error.APIConnectionError as e:
|
|
|
|
logging.error(str(e))
|
2020-03-05 10:03:47 +00:00
|
|
|
raise Exception(common_message)
|
2020-02-21 09:41:22 +00:00
|
|
|
except stripe.error.StripeError as e:
|
2020-03-05 10:03:47 +00:00
|
|
|
# XXX: maybe send email
|
2020-02-21 09:41:22 +00:00
|
|
|
logging.error(str(e))
|
2020-03-05 10:03:47 +00:00
|
|
|
raise Exception(common_message)
|
2020-02-21 09:41:22 +00:00
|
|
|
|
|
|
|
return handle_problems
|
2020-03-02 21:26:40 +00:00
|
|
|
|
2020-03-05 09:23:34 +00:00
|
|
|
def public_api_key():
|
2020-05-01 22:16:29 +00:00
|
|
|
return settings.STRIPE_PUBLIC_KEY
|
2020-03-05 09:23:34 +00:00
|
|
|
|
2020-03-03 15:55:56 +00:00
|
|
|
def get_customer_id_for(user):
|
|
|
|
try:
|
|
|
|
# .get() raise if there is no matching entry.
|
2020-12-28 22:35:34 +00:00
|
|
|
return StripeCustomer.objects.get(owner=user).stripe_id
|
2020-03-03 15:55:56 +00:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
# No entry yet - making a new one.
|
2020-03-05 10:03:47 +00:00
|
|
|
try:
|
|
|
|
customer = create_customer(user.username, user.email)
|
2020-12-28 22:35:34 +00:00
|
|
|
uncloud_stripe_mapping = StripeCustomer.objects.create(
|
2020-03-05 10:28:20 +00:00
|
|
|
owner=user, stripe_id=customer.id)
|
2020-03-05 10:03:47 +00:00
|
|
|
return uncloud_stripe_mapping.stripe_id
|
|
|
|
except Exception as e:
|
2020-03-03 15:55:56 +00:00
|
|
|
return None
|
|
|
|
|
2020-03-02 21:26:40 +00:00
|
|
|
@handle_stripe_error
|
2020-03-05 09:23:34 +00:00
|
|
|
def create_setup_intent(customer_id):
|
|
|
|
return stripe.SetupIntent.create(customer=customer_id)
|
|
|
|
|
|
|
|
@handle_stripe_error
|
|
|
|
def get_setup_intent(setup_intent_id):
|
|
|
|
return stripe.SetupIntent.retrieve(setup_intent_id)
|
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
@handle_stripe_error
|
2020-03-05 09:23:34 +00:00
|
|
|
def get_payment_method(payment_method_id):
|
|
|
|
return stripe.PaymentMethod.retrieve(payment_method_id)
|
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
@handle_stripe_error
|
|
|
|
def get_card_from_payment(user, payment_method_id):
|
|
|
|
payment_method = stripe.PaymentMethod.retrieve(payment_method_id)
|
|
|
|
if payment_method:
|
|
|
|
if 'card' in payment_method:
|
|
|
|
sync_cards_for_user(user)
|
|
|
|
return payment_method['card']
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@handle_stripe_error
|
2021-08-12 10:28:19 +00:00
|
|
|
def attach_payment_method(payment_method_id, user):
|
|
|
|
customer_id = get_customer_id_for(user)
|
|
|
|
ret = stripe.PaymentMethod.attach(payment_method_id, customer=customer_id)
|
|
|
|
sync_cards_for_user(user)
|
|
|
|
return ret
|
2021-07-19 14:36:10 +00:00
|
|
|
|
2020-03-02 21:26:40 +00:00
|
|
|
@handle_stripe_error
|
|
|
|
def create_customer(name, email):
|
|
|
|
return stripe.Customer.create(name=name, email=email)
|
|
|
|
|
|
|
|
@handle_stripe_error
|
|
|
|
def get_customer(customer_id):
|
|
|
|
return stripe.Customer.retrieve(customer_id)
|
2020-12-26 10:22:51 +00:00
|
|
|
|
|
|
|
@handle_stripe_error
|
|
|
|
def get_customer_cards(customer_id):
|
|
|
|
|
|
|
|
cards = []
|
|
|
|
stripe_cards = stripe.PaymentMethod.list(
|
|
|
|
customer=customer_id,
|
|
|
|
type="card",
|
|
|
|
)
|
|
|
|
|
|
|
|
for stripe_card in stripe_cards["data"]:
|
|
|
|
card = {}
|
|
|
|
card['brand'] = stripe_card["card"]["brand"]
|
|
|
|
card['last4'] = stripe_card["card"]["last4"]
|
|
|
|
card['month'] = stripe_card["card"]["exp_month"]
|
|
|
|
card['year'] = stripe_card["card"]["exp_year"]
|
2020-12-28 22:35:34 +00:00
|
|
|
card['id'] = stripe_card["id"]
|
2020-12-26 10:22:51 +00:00
|
|
|
|
|
|
|
cards.append(card)
|
|
|
|
|
|
|
|
return cards
|
2020-12-28 22:35:34 +00:00
|
|
|
|
2021-08-12 10:28:19 +00:00
|
|
|
@handle_stripe_error
|
|
|
|
def delete_card(card_id):
|
|
|
|
return stripe.PaymentMethod.detach(card_id)
|
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
def sync_cards_for_user(user):
|
|
|
|
customer_id = get_customer_id_for(user)
|
|
|
|
cards = get_customer_cards(customer_id)
|
|
|
|
|
2020-12-29 00:43:33 +00:00
|
|
|
active_cards = StripeCreditCard.objects.filter(owner=user,
|
|
|
|
active=True)
|
|
|
|
|
|
|
|
if len(active_cards) > 0:
|
|
|
|
has_active_card = True
|
|
|
|
else:
|
|
|
|
has_active_card = False
|
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
for card in cards:
|
2020-12-29 00:43:33 +00:00
|
|
|
active = False
|
|
|
|
|
|
|
|
if not has_active_card:
|
|
|
|
active = True
|
|
|
|
has_active_card = True
|
|
|
|
|
2020-12-28 22:35:34 +00:00
|
|
|
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'],
|
2020-12-29 00:43:33 +00:00
|
|
|
1),
|
|
|
|
'active': active
|
2020-12-28 22:35:34 +00:00
|
|
|
}
|
|
|
|
)
|
2020-12-29 00:43:33 +00:00
|
|
|
|
|
|
|
@handle_stripe_error
|
2021-07-19 14:36:10 +00:00
|
|
|
def charge_customer(user, amount, currency='CHF', card=False):
|
2020-12-29 00:43:33 +00:00
|
|
|
# Amount is in CHF but stripes requires smallest possible unit.
|
|
|
|
# https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
|
|
|
|
# FIXME: might need to be adjusted for other currencies
|
|
|
|
|
|
|
|
if currency == 'CHF':
|
|
|
|
adjusted_amount = int(amount * 100)
|
|
|
|
else:
|
|
|
|
return Exception("Programming error: unsupported currency")
|
|
|
|
|
|
|
|
try:
|
2021-07-19 14:36:10 +00:00
|
|
|
card = card or StripeCreditCard.objects.get(owner=user,
|
2020-12-29 00:43:33 +00:00
|
|
|
active=True)
|
|
|
|
|
|
|
|
except StripeCreditCard.DoesNotExist:
|
|
|
|
raise ValidationError("No active credit card - cannot create payment")
|
|
|
|
|
|
|
|
customer_id = get_customer_id_for(user)
|
2021-07-19 14:36:10 +00:00
|
|
|
|
2020-12-29 00:43:33 +00:00
|
|
|
return stripe.PaymentIntent.create(
|
|
|
|
amount=adjusted_amount,
|
|
|
|
currency=currency,
|
|
|
|
customer=customer_id,
|
|
|
|
payment_method=card.card_id,
|
|
|
|
off_session=True,
|
|
|
|
confirm=True,
|
|
|
|
)
|
2021-07-19 14:36:10 +00:00
|
|
|
|
|
|
|
@handle_stripe_error
|
|
|
|
def get_payment_intent(user, amount, currency='CHF', card=False):
|
|
|
|
# Amount is in CHF but stripes requires smallest possible unit.
|
|
|
|
# https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
|
|
|
|
# FIXME: might need to be adjusted for other currencies
|
|
|
|
|
|
|
|
if currency == 'CHF':
|
|
|
|
adjusted_amount = int(amount * 100)
|
|
|
|
else:
|
|
|
|
return Exception("Programming error: unsupported currency")
|
|
|
|
|
|
|
|
try:
|
|
|
|
card = card or StripeCreditCard.objects.get(owner=user,
|
|
|
|
active=True)
|
|
|
|
|
|
|
|
except StripeCreditCard.DoesNotExist:
|
|
|
|
raise ValidationError("No active credit card - cannot create payment")
|
|
|
|
|
|
|
|
customer_id = get_customer_id_for(user)
|
|
|
|
|
|
|
|
return stripe.PaymentIntent.create(
|
|
|
|
amount=adjusted_amount,
|
|
|
|
currency=currency,
|
|
|
|
customer=customer_id,
|
|
|
|
payment_method=card.card_id,
|
|
|
|
setup_future_usage='off_session',
|
|
|
|
confirm=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
@handle_stripe_error
|
|
|
|
def get_or_create_tax_id_for_user(stripe_customer_id, vat_number,
|
|
|
|
type="eu_vat", country=""):
|
|
|
|
def compare_vat_numbers(vat1, vat2):
|
|
|
|
_vat1 = vat1.replace(" ", "").replace(".", "").replace("-","")
|
|
|
|
_vat2 = vat2.replace(" ", "").replace(".", "").replace("-","")
|
|
|
|
return True if _vat1 == _vat2 else False
|
|
|
|
|
|
|
|
tax_ids_list = stripe.Customer.list_tax_ids(
|
|
|
|
stripe_customer_id,
|
|
|
|
limit=100,
|
|
|
|
)
|
|
|
|
for tax_id_obj in tax_ids_list.data:
|
|
|
|
if compare_vat_numbers(tax_id_obj.value, vat_number):
|
|
|
|
return tax_id_obj
|
|
|
|
else:
|
|
|
|
logger.debug(
|
|
|
|
"{val1} is not equal to {val2} or {con1} not same as "
|
|
|
|
"{con2}".format(val1=tax_id_obj.value, val2=vat_number,
|
|
|
|
con1=tax_id_obj.country.lower(),
|
|
|
|
con2=country.lower().strip()))
|
|
|
|
logger.debug(
|
|
|
|
"tax id obj does not exist for {val}. Creating a new one".format(
|
|
|
|
val=vat_number
|
|
|
|
))
|
|
|
|
tax_id_obj = stripe.Customer.create_tax_id(
|
|
|
|
stripe_customer_id,
|
|
|
|
type=type,
|
|
|
|
value=vat_number,
|
|
|
|
)
|
|
|
|
return tax_id_obj
|