pretix-stripe2/pretix_stripe2/tasks.py

52 lines
1.4 KiB
Python
Raw Normal View History

2020-09-04 17:56:10 +10:00
import logging
from urllib.parse import urlsplit
import stripe
from django.conf import settings
from pretix.base.services.tasks import EventTask
from pretix.celery_app import app
from pretix.multidomain.urlreverse import get_event_domain
2020-09-04 17:58:25 +10:00
from pretix_stripe2.models import RegisteredStripe2ApplePayDomain
2020-09-04 17:56:10 +10:00
logger = logging.getLogger(__name__)
def get_domain_for_event(event):
domain = get_event_domain(event, fallback=True)
if not domain:
siteurlsplit = urlsplit(settings.SITE_URL)
return siteurlsplit.hostname
return domain
def get_stripe_account_key(prov):
if prov.settings.connect_user_id:
return prov.settings.connect_user_id
else:
return prov.settings.publishable_key
@app.task(base=EventTask, max_retries=5, default_retry_delay=1)
def stripe_verify_domain(event, domain):
2020-09-04 17:58:25 +10:00
from pretix_stripe2.payment import StripeCC
2020-09-04 17:56:10 +10:00
prov = StripeCC(event)
account = get_stripe_account_key(prov)
2020-09-04 17:58:25 +10:00
if RegisteredStripe2ApplePayDomain.objects.filter(account=account, domain=domain).exists():
2020-09-04 17:56:10 +10:00
return
try:
resp = stripe.ApplePayDomain.create(
domain_name=domain,
**prov.api_kwargs
)
except stripe.error.StripeError:
logger.exception('Could not verify domain with Stripe')
else:
if resp.livemode:
2020-09-04 17:58:25 +10:00
RegisteredStripe2ApplePayDomain.objects.create(
2020-09-04 17:56:10 +10:00
domain=domain,
account=account
)