@@ -296,6 +296,12 @@ def customer_created(self, customer: stripe.Customer) -> None:
296296
297297 # handler for Stripe event customer.subscription.created
298298 def customer_subscription_created (self , subscription : stripe .Subscription ) -> None :
299+ log .info (
300+ "Customer subscription created" ,
301+ extra = dict (
302+ customer_id = subscription ["customer" ], subscription_id = subscription ["id" ]
303+ ),
304+ )
299305 sub_item_plan_id = subscription .plan .id
300306
301307 if not sub_item_plan_id :
@@ -332,11 +338,31 @@ def customer_subscription_created(self, subscription: stripe.Subscription) -> No
332338 quantity = subscription .quantity ,
333339 ),
334340 )
341+ # add the subscription_id and customer_id to the owner
335342 owner = Owner .objects .get (ownerid = subscription .metadata .get ("obo_organization" ))
336343 owner .stripe_subscription_id = subscription .id
337344 owner .stripe_customer_id = subscription .customer
338345 owner .save ()
339346
347+ # check if the subscription has a pending_update attribute, if so, don't upgrade the plan yet
348+ print ("subscription what are you" , subscription )
349+ # Check if subscription has a default payment method
350+ has_default_payment = subscription .default_payment_method is not None
351+
352+ # If no default payment, check for any pending verification methods
353+ if not has_default_payment :
354+ payment_methods = get_unverified_payment_methods (subscription .customer )
355+ if payment_methods :
356+ log .info (
357+ "Subscription has pending payment verification" ,
358+ extra = dict (
359+ subscription_id = subscription .id ,
360+ customer_id = subscription .customer ,
361+ payment_methods = payment_methods ,
362+ ),
363+ )
364+ return
365+
340366 plan_service = PlanService (current_org = owner )
341367 plan_service .expire_trial_when_upgrading ()
342368
@@ -356,6 +382,13 @@ def customer_subscription_created(self, subscription: stripe.Subscription) -> No
356382
357383 # handler for Stripe event customer.subscription.updated
358384 def customer_subscription_updated (self , subscription : stripe .Subscription ) -> None :
385+ log .info (
386+ "Customer subscription updated" ,
387+ extra = dict (
388+ customer_id = subscription ["customer" ], subscription_id = subscription ["id" ]
389+ ),
390+ )
391+
359392 owners : QuerySet [Owner ] = Owner .objects .filter (
360393 stripe_subscription_id = subscription .id ,
361394 stripe_customer_id = subscription .customer ,
@@ -371,6 +404,25 @@ def customer_subscription_updated(self, subscription: stripe.Subscription) -> No
371404 )
372405 return
373406
407+ # check if the subscription has a pending_update attribute, if so, don't upgrade the plan yet
408+ print ("subscription what are you" , subscription )
409+ # Check if subscription has a default payment method
410+ has_default_payment = subscription .default_payment_method is not None
411+
412+ # If no default payment, check for any pending verification methods
413+ if not has_default_payment :
414+ payment_methods = get_unverified_payment_methods (subscription .customer )
415+ if payment_methods :
416+ log .info (
417+ "Subscription has pending payment verification" ,
418+ extra = dict (
419+ subscription_id = subscription .id ,
420+ customer_id = subscription .customer ,
421+ payment_methods = payment_methods ,
422+ ),
423+ )
424+ return
425+
374426 indication_of_payment_failure = getattr (subscription , "pending_update" , None )
375427 if indication_of_payment_failure :
376428 # payment failed, raise this to user by setting as delinquent
0 commit comments