<s> from datetime import datetime <EOL> from django . dispatch import receiver <EOL> from django . db . models . signals import post_save <EOL> from django . contrib . auth . models import User <EOL> from django_stripe . shortcuts import stripe <EOL> from django_stripe . contrib . registration . backends import StripeSubscriptionBackend <EOL> from django_stripe . contrib . registration . signals import user_registered <EOL> from django_stripe . signals import ( upcoming_invoice_updated , invoice_ready , recurring_payment_failed , subscription_final_payment_attempt_failed , StripeWebhook ) <EOL> from . models import UserProfile <EOL> @ receiver ( post_save , sender = User , dispatch_uid = '<STR_LIT>' ) <EOL> def create_user_profile ( sender , instance , created , ** kwargs ) : <EOL> if created : <EOL> profile , new = UserProfile . objects . get_or_create ( user = instance ) <EOL> @ receiver ( user_registered , sender = StripeSubscriptionBackend , dispatch_uid = '<STR_LIT>' ) <EOL> def link_stripe_customer ( sender , user , request , customer , plan = None , ** kwargs ) : <EOL> user_profile = user . get_profile ( ) <EOL> user_profile . customer_id = customer . id <EOL> user_profile . card_last4 = customer . active_card . last_4 <EOL> user_profile . plan = plan <EOL> try : <EOL> user_profile . trial_end = datetime . utcfromtimestamp ( customer . subscription . trial_end ) <EOL> except AttributeError : <EOL> pass <EOL> user_profile . save ( ) <EOL> upcoming_invoice_updated . send ( sender = None , customer = customer ) <EOL> @ receiver ( invoice_ready , sender = StripeWebhook , dispatch_uid = '<STR_LIT>' ) <EOL> def invoice_user ( sender , customer , invoice , ** kwargs ) : <EOL> try : <EOL> user_profile = UserProfile . objects . get ( customer_id = customer ) <EOL> amount = int ( user_profile . collaborator_count * user_profile . get_price ( ) ) <EOL> if not user_profile . trialing and amount > <NUM_LIT:0> : <EOL> stripe . InvoiceItem . create ( customer = customer , <EOL> amount = amount * <NUM_LIT:100> , <EOL> currency = '<STR_LIT>' , <EOL> description = "<STR_LIT>" % user_profile . collaborator_count <EOL> ) <EOL> upcoming_invoice_updated . send ( sender = None , customer = customer ) <EOL> except UserProfile . DoesNotExist : <EOL> pass <EOL> @ receiver ( recurring_payment_failed , sender = StripeWebhook , dispatch_uid = '<STR_LIT>' ) <EOL> def update_payment_attempts ( sender , customer , attempt , payment , ** kwargs ) : <EOL> try : <EOL> user_profile = UserProfile . objects . get ( customer_id = customer ) <EOL> user_profile . payment_attempts = int ( attempt ) <EOL> user_profile . last_payment_attempt = datetime . utcfromtimestamp ( payment [ '<STR_LIT:time>' ] ) <EOL> user_profile . save ( ) <EOL> except UserProfile . DoesNotExist : <EOL> pass <EOL> @ receiver ( subscription_final_payment_attempt_failed , sender = StripeWebhook , dispatch_uid = '<STR_LIT>' ) <EOL> def lock_account ( sender , customer , subscription , ** kwargs ) : <EOL> try : <EOL> user = User . objects . get ( profile__customer_id = customer ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> except User . DoesNotExist : <EOL> pass </s>
