Stripe checkout #104411

Merged
Anna Sirota merged 61 commits from stripe into main 2024-06-17 18:08:41 +02:00
2 changed files with 39 additions and 39 deletions
Showing only changes of commit 33e3e06b83 - Show all commits

View File

@ -3,6 +3,7 @@ from typing import Set
import logging import logging
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Q from django.db.models import Q
from django.dispatch import receiver from django.dispatch import receiver
import alphabetic_timestamp as ats import alphabetic_timestamp as ats
@ -29,6 +30,44 @@ def timebased_order_number():
return ats.base36.now(time_unit=ats.TimeUnit.milliseconds).upper() return ats.base36.now(time_unit=ats.TimeUnit.milliseconds).upper()
@receiver(django_signals.post_save, sender=User)
def create_customer(sender, instance: User, created, raw, **kwargs):
"""Create Customer on User creation."""
from looper.models import Customer
if raw:
return
if not created:
return
try:
customer = instance.customer
except Customer.DoesNotExist:
pass
else:
logger.debug(
'Newly created User %d already has a Customer %d, not creating new one',
instance.pk,
customer.pk,
)
billing_address = customer.billing_address
logger.info('Creating new billing address due to creation of user %s', instance.pk)
if not billing_address.pk:
billing_address.email = instance.email
billing_address.full_name = instance.full_name
billing_address.save()
return
logger.info('Creating new Customer due to creation of user %s', instance.pk)
with transaction.atomic():
customer = Customer.objects.create(user=instance)
billing_address = customer.billing_address
billing_address.email = instance.email
billing_address.full_name = instance.full_name
billing_address.save()
@receiver(django_signals.pre_save, sender=Order) @receiver(django_signals.pre_save, sender=Order)
def _set_order_number(sender, instance: Order, **kwargs): def _set_order_number(sender, instance: Order, **kwargs):
if instance.pk or instance.number or instance.is_legacy: if instance.pk or instance.number or instance.is_legacy:

View File

@ -4,7 +4,6 @@ import logging
from actstream.models import Action from actstream.models import Action
from anymail.signals import tracking from anymail.signals import tracking
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models.signals import pre_save, post_save from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.dateparse import parse_datetime from django.utils.dateparse import parse_datetime
@ -69,44 +68,6 @@ def _sync_is_subscribed_to_newsletter(sender: object, instance: User, **kwargs):
tasks.handle_is_subscribed_to_newsletter(pk=instance.pk) tasks.handle_is_subscribed_to_newsletter(pk=instance.pk)
@receiver(post_save, sender=User)
def create_customer(sender, instance: User, created, raw, **kwargs):
"""Create Customer on User creation."""
from looper.models import Customer
if raw:
return
if not created:
return
try:
customer = instance.customer
except Customer.DoesNotExist:
pass
else:
logger.debug(
'Newly created User %d already has a Customer %d, not creating new one',
instance.pk,
customer.pk,
)
billing_address = customer.billing_address
logger.info('Creating new billing address due to creation of user %s', instance.pk)
if not billing_address.pk:
billing_address.email = instance.email
billing_address.full_name = instance.full_name
billing_address.save()
return
logger.info('Creating new Customer due to creation of user %s', instance.pk)
with transaction.atomic():
customer = Customer.objects.create(user=instance)
billing_address = customer.billing_address
billing_address.email = instance.email
billing_address.full_name = instance.full_name
billing_address.save()
@receiver(tracking) @receiver(tracking)
def _handle_mailgun_tracking_event(sender, event, esp_name, **kwargs): def _handle_mailgun_tracking_event(sender, event, esp_name, **kwargs):
event_type = event.event_type event_type = event.event_type