@@ -17,37 +17,57 @@ def get_or_create(
1717 self ,
1818 email_address : str ,
1919 channel : Optional [str ] = None ,
20+ name : Optional [str ] = None ,
2021 ) -> Customer :
2122 """Get or create a customer."""
22- # Check if customer ID is cached
23+ # Check if customer ID is cached and email matches
2324 cached_customer_id = self ._client .state_manager .get_customer_id ()
24- if cached_customer_id :
25+ cached_email = self ._client .state_manager .get_customer_email ()
26+
27+ if cached_customer_id and cached_email == email_address :
28+ print (
29+ f"DEBUG: Using cached customer ID { cached_customer_id } "
30+ f"for email { email_address } "
31+ )
2532 return Customer (
2633 self ._client ,
2734 cached_customer_id ,
2835 email_address ,
2936 channel ,
3037 )
38+ elif cached_customer_id and cached_email != email_address :
39+ print (
40+ f"DEBUG: Email changed from { cached_email } to "
41+ f"{ email_address } , clearing cache"
42+ )
43+ self ._client .state_manager .clear_state ()
3144
3245 # Create or fetch customer
3346 response = self ._client .http_client ._make_request (
3447 "POST" ,
35- "/api/v1/customers " ,
48+ "/v3/customer " ,
3649 json_data = {
3750 "email_address" : email_address ,
3851 "channel" : channel ,
52+ "name" : name ,
3953 "app_slug" : self ._client .app_slug ,
4054 },
4155 headers = self ._client ._get_auth_headers (),
4256 )
4357
44- customer_id = response ["id" ]
58+ print (f"DEBUG: API Response: { response } " )
59+ customer_id = response ["customer" ]["id" ]
4560 self ._client .state_manager .set_customer_id (customer_id )
61+ self ._client .state_manager .set_customer_email (email_address )
4662
4763 # Store dynamic token if provided
4864 if "dynamic_token" in response :
4965 dynamic_token = response ["dynamic_token" ]
5066 self ._client .state_manager .set_dynamic_token (dynamic_token )
67+ elif "customer" in response and "serviceToken" in response ["customer" ]:
68+ service_token = response ["customer" ]["serviceToken" ]
69+ self ._client .state_manager .set_dynamic_token (service_token )
70+ print (f"DEBUG: Stored service token: { service_token [:20 ]} ..." )
5171
5272 response_data = response .copy ()
5373 response_data .pop ("email_address" , None )
@@ -71,37 +91,57 @@ async def get_or_create(
7191 self ,
7292 email_address : str ,
7393 channel : Optional [str ] = None ,
94+ name : Optional [str ] = None ,
7495 ) -> AsyncCustomer :
7596 """Get or create a customer."""
76- # Check if customer ID is cached
97+ # Check if customer ID is cached and email matches
7798 cached_customer_id = self ._client .state_manager .get_customer_id ()
78- if cached_customer_id :
99+ cached_email = self ._client .state_manager .get_customer_email ()
100+
101+ if cached_customer_id and cached_email == email_address :
102+ print (
103+ f"DEBUG: Using cached customer ID { cached_customer_id } "
104+ f"for email { email_address } "
105+ )
79106 return AsyncCustomer (
80107 self ._client ,
81108 cached_customer_id ,
82109 email_address ,
83110 channel ,
84111 )
112+ elif cached_customer_id and cached_email != email_address :
113+ print (
114+ f"DEBUG: Email changed from { cached_email } to "
115+ f"{ email_address } , clearing cache"
116+ )
117+ self ._client .state_manager .clear_state ()
85118
86119 # Create or fetch customer
87120 response = await self ._client .http_client ._make_request_async (
88121 "POST" ,
89- "/api/v1/customers " ,
122+ "/v3/customer " ,
90123 json_data = {
91124 "email_address" : email_address ,
92125 "channel" : channel ,
126+ "name" : name ,
93127 "app_slug" : self ._client .app_slug ,
94128 },
95129 headers = self ._client ._get_auth_headers (),
96130 )
97131
98- customer_id = response ["id" ]
132+ print (f"DEBUG: API Response: { response } " )
133+ customer_id = response ["customer" ]["id" ]
99134 self ._client .state_manager .set_customer_id (customer_id )
135+ self ._client .state_manager .set_customer_email (email_address )
100136
101137 # Store dynamic token if provided
102138 if "dynamic_token" in response :
103139 dynamic_token = response ["dynamic_token" ]
104140 self ._client .state_manager .set_dynamic_token (dynamic_token )
141+ elif "customer" in response and "serviceToken" in response ["customer" ]:
142+ service_token = response ["customer" ]["serviceToken" ]
143+ self ._client .state_manager .set_dynamic_token (service_token )
144+ print (f"DEBUG: Stored service token: { service_token [:20 ]} ..." )
105145
106146 response_data = response .copy ()
107147 response_data .pop ("email_address" , None )
0 commit comments