@@ -296,6 +296,12 @@ def customer_created(self, customer: stripe.Customer) -> None:
296
296
297
297
# handler for Stripe event customer.subscription.created
298
298
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
+ )
299
305
sub_item_plan_id = subscription .plan .id
300
306
301
307
if not sub_item_plan_id :
@@ -332,11 +338,31 @@ def customer_subscription_created(self, subscription: stripe.Subscription) -> No
332
338
quantity = subscription .quantity ,
333
339
),
334
340
)
341
+ # add the subscription_id and customer_id to the owner
335
342
owner = Owner .objects .get (ownerid = subscription .metadata .get ("obo_organization" ))
336
343
owner .stripe_subscription_id = subscription .id
337
344
owner .stripe_customer_id = subscription .customer
338
345
owner .save ()
339
346
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
+
340
366
plan_service = PlanService (current_org = owner )
341
367
plan_service .expire_trial_when_upgrading ()
342
368
@@ -356,6 +382,13 @@ def customer_subscription_created(self, subscription: stripe.Subscription) -> No
356
382
357
383
# handler for Stripe event customer.subscription.updated
358
384
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
+
359
392
owners : QuerySet [Owner ] = Owner .objects .filter (
360
393
stripe_subscription_id = subscription .id ,
361
394
stripe_customer_id = subscription .customer ,
@@ -371,6 +404,25 @@ def customer_subscription_updated(self, subscription: stripe.Subscription) -> No
371
404
)
372
405
return
373
406
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
+
374
426
indication_of_payment_failure = getattr (subscription , "pending_update" , None )
375
427
if indication_of_payment_failure :
376
428
# payment failed, raise this to user by setting as delinquent
0 commit comments