@@ -726,25 +726,65 @@ def update_billing_address(self, owner: Owner, name, billing_address):
726726 return None
727727
728728 try :
729- default_payment_method = stripe .Customer .retrieve (
730- owner .stripe_customer_id
731- ).invoice_settings .default_payment_method
729+ customer = stripe .Customer .retrieve (owner .stripe_customer_id )
730+ log .info ("Retrieved customer" , extra = dict (customer = customer ))
731+
732+ default_payment_method = customer .invoice_settings .default_payment_method
733+ log .info (
734+ "Retrieved default payment method" ,
735+ extra = dict (payment_method = default_payment_method ),
736+ )
732737
738+ if default_payment_method is None :
739+ log .warning (
740+ "Customer has no default payment method, skipping payment method update" ,
741+ extra = dict (
742+ stripe_customer_id = owner .stripe_customer_id ,
743+ ownerid = owner .ownerid ,
744+ ),
745+ )
746+ # Still update the customer address even if there's no payment method
747+ stripe .Customer .modify (
748+ owner .stripe_customer_id , address = billing_address
749+ )
750+ log .info (
751+ "Stripe successfully updated customer address" ,
752+ extra = dict (
753+ ownerid = owner .ownerid ,
754+ requesting_user_id = self .requesting_user .ownerid ,
755+ ),
756+ )
757+ return
758+
759+ log .info (
760+ "Modifying payment method billing details" ,
761+ extra = dict (stripe_customer_id = owner .stripe_customer_id ),
762+ )
733763 stripe .PaymentMethod .modify (
734764 default_payment_method ,
735765 billing_details = {"name" : name , "address" : billing_address },
736766 )
737767
768+ log .info (
769+ "Modifying customer address" ,
770+ extra = dict (stripe_customer_id = owner .stripe_customer_id ),
771+ )
738772 stripe .Customer .modify (owner .stripe_customer_id , address = billing_address )
739773 log .info (
740- f"Stripe successfully updated billing address for owner { owner .ownerid } by user #{ self .requesting_user .ownerid } "
774+ "Stripe successfully updated billing address" ,
775+ extra = dict (
776+ ownerid = owner .ownerid ,
777+ requesting_user_id = self .requesting_user .ownerid ,
778+ ),
741779 )
742- except Exception :
780+ except Exception as e :
743781 log .error (
744782 "Unable to update billing address for customer" ,
745783 extra = dict (
746784 customer_id = owner .stripe_customer_id ,
747785 subscription_id = owner .stripe_subscription_id ,
786+ error = str (e ),
787+ error_type = type (e ).__name__ ,
748788 ),
749789 )
750790
0 commit comments