<s> import datetime <EOL> from django . db . models import Q <EOL> from django . utils import timezone <EOL> from django . utils . encoding import smart_str <EOL> import stripe <EOL> from . . import hooks <EOL> from . . import models <EOL> from . . import utils <EOL> def cancel ( subscription , at_period_end = True ) : <EOL> """<STR_LIT>""" <EOL> sub = subscription . stripe_subscription . delete ( at_period_end = at_period_end ) <EOL> sync_subscription_from_stripe_data ( subscription . customer , sub ) <EOL> def create ( customer , plan , quantity = None , trial_days = None , token = None , coupon = None ) : <EOL> """<STR_LIT>""" <EOL> quantity = hooks . hookset . adjust_subscription_quantity ( customer = customer , plan = plan , quantity = quantity ) <EOL> cu = customer . stripe_customer <EOL> subscription_params = { } <EOL> if trial_days : <EOL> subscription_params [ "<STR_LIT>" ] = datetime . datetime . utcnow ( ) + datetime . timedelta ( days = trial_days ) <EOL> if token : <EOL> subscription_params [ "<STR_LIT:source>" ] = token <EOL> subscription_params [ "<STR_LIT>" ] = plan <EOL> subscription_params [ "<STR_LIT>" ] = quantity <EOL> subscription_params [ "<STR_LIT>" ] = coupon <EOL> resp = cu . subscriptions . create ( ** subscription_params ) <EOL> return resp <EOL> def has_active_subscription ( customer ) : <EOL> """<STR_LIT>""" <EOL> return models . Subscription . objects . filter ( <EOL> customer = customer <EOL> ) . filter ( <EOL> Q ( ended_at__isnull = True ) | Q ( ended_at__gt = timezone . now ( ) ) <EOL> ) . exists ( ) <EOL> def is_period_current ( subscription ) : <EOL> """<STR_LIT>""" <EOL> return subscription . current_period_end > timezone . now ( ) <EOL> def is_status_current ( subscription ) : <EOL> """<STR_LIT>""" <EOL> return subscription . status in [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def is_valid ( subscription ) : <EOL> """<STR_LIT>""" <EOL> if not is_status_current ( subscription ) : <EOL> return False <EOL> if subscription . cancel_at_period_end and not is_period_current ( subscription ) : <EOL> return False <EOL> return True <EOL> def retrieve ( customer , sub_id ) : <EOL> """<STR_LIT>""" <EOL> if not sub_id : <EOL> return <EOL> try : <EOL> return customer . stripe_customer . subscriptions . retrieve ( sub_id ) <EOL> except stripe . InvalidRequestError as e : <EOL> if smart_str ( e ) . find ( "<STR_LIT>" ) == - <NUM_LIT:1> : <EOL> raise <EOL> def sync_subscription_from_stripe_data ( customer , subscription ) : <EOL> """<STR_LIT>""" <EOL> defaults = dict ( <EOL> customer = customer , <EOL> application_fee_percent = subscription [ "<STR_LIT>" ] , <EOL> cancel_at_period_end = subscription [ "<STR_LIT>" ] , <EOL> canceled_at = utils . convert_tstamp ( subscription [ "<STR_LIT>" ] ) , <EOL> current_period_start = utils . convert_tstamp ( subscription [ "<STR_LIT>" ] ) , <EOL> current_period_end = utils . convert_tstamp ( subscription [ "<STR_LIT>" ] ) , <EOL> ended_at = utils . convert_tstamp ( subscription [ "<STR_LIT>" ] ) , <EOL> plan = models . Plan . objects . get ( stripe_id = subscription [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] ) , <EOL> quantity = subscription [ "<STR_LIT>" ] , <EOL> start = utils . convert_tstamp ( subscription [ "<STR_LIT:start>" ] ) , <EOL> status = subscription [ "<STR_LIT:status>" ] , <EOL> trial_start = utils . convert_tstamp ( subscription [ "<STR_LIT>" ] ) if subscription [ "<STR_LIT>" ] else None , <EOL> trial_end = utils . convert_tstamp ( subscription [ "<STR_LIT>" ] ) if subscription [ "<STR_LIT>" ] else None <EOL> ) <EOL> sub , created = models . Subscription . objects . get_or_create ( <EOL> stripe_id = subscription [ "<STR_LIT:id>" ] , <EOL> defaults = defaults <EOL> ) <EOL> sub = utils . update_with_defaults ( sub , defaults , created ) <EOL> return sub <EOL> def update ( subscription , plan = None , quantity = None , prorate = True , coupon = None , charge_immediately = False ) : <EOL> """<STR_LIT>""" <EOL> stripe_subscription = subscription . stripe_subscription <EOL> if plan : <EOL> stripe_subscription . plan = plan <EOL> if quantity : <EOL> stripe_subscription . quantity = quantity <EOL> if not prorate : <EOL> stripe_subscription . prorate = False <EOL> if coupon : <EOL> stripe_subscription . coupon = coupon <EOL> sub = stripe_subscription . save ( ) <EOL> customer = models . Customer . objects . get ( pk = subscription . customer . pk ) <EOL> sync_subscription_from_stripe_data ( customer , sub ) </s>
