You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.5 KiB
141 lines
4.5 KiB
"""
|
|
Customer Service Module
|
|
|
|
This module contains customer-related functions including:
|
|
- Stripe customer ID lookup and creation
|
|
- MySQL customer billing queries
|
|
- Customer data retrieval
|
|
"""
|
|
|
|
import logging
|
|
import pymysql
|
|
from typing import Optional, Dict, Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def get_stripe_customer_id_from_mysql(splynx_id: int, mysql_config: dict) -> Optional[str]:
|
|
"""
|
|
Query MySQL database to get Stripe customer ID for a Splynx customer.
|
|
|
|
Args:
|
|
splynx_id: Customer ID in Splynx
|
|
mysql_config: MySQL connection configuration dictionary
|
|
|
|
Returns:
|
|
Stripe customer ID if found, None otherwise
|
|
"""
|
|
connection = None
|
|
try:
|
|
connection = pymysql.connect(
|
|
host=mysql_config['host'],
|
|
database=mysql_config['database'],
|
|
user=mysql_config['user'],
|
|
password=mysql_config['password'],
|
|
port=mysql_config['port'],
|
|
autocommit=False,
|
|
cursorclass=pymysql.cursors.DictCursor
|
|
)
|
|
|
|
query = """
|
|
SELECT field_1 AS stripe_customer_id
|
|
FROM payment_account_data
|
|
WHERE customer_id = %s
|
|
LIMIT 1
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
|
cursor.execute(query, (splynx_id,))
|
|
result = cursor.fetchone()
|
|
|
|
if result and result.get('stripe_customer_id'):
|
|
logger.info(f"Found Stripe customer ID for Splynx ID {splynx_id}: {result['stripe_customer_id']}")
|
|
return result['stripe_customer_id']
|
|
else:
|
|
logger.warning(f"No Stripe customer ID found for Splynx ID {splynx_id}")
|
|
return None
|
|
|
|
except pymysql.Error as e:
|
|
logger.error(f"MySQL Error while fetching Stripe customer ID: {e}")
|
|
return None
|
|
except Exception as e:
|
|
logger.error(f"Unexpected error while fetching Stripe customer ID: {e}")
|
|
return None
|
|
finally:
|
|
if connection:
|
|
connection.close()
|
|
|
|
|
|
def get_customer_from_splynx(splynx, splynx_id: int) -> Optional[Dict[str, Any]]:
|
|
"""
|
|
Get customer information from Splynx API.
|
|
|
|
Args:
|
|
splynx: Splynx API client instance
|
|
splynx_id: Customer ID in Splynx
|
|
|
|
Returns:
|
|
Customer data dictionary if found, None otherwise
|
|
"""
|
|
try:
|
|
customer_data = splynx.Customer(splynx_id)
|
|
if customer_data != 'unknown':
|
|
return customer_data
|
|
else:
|
|
logger.warning(f"Customer {splynx_id} not found in Splynx")
|
|
return None
|
|
except Exception as e:
|
|
logger.error(f"Error fetching customer {splynx_id} from Splynx: {e}")
|
|
return None
|
|
|
|
|
|
def get_stripe_customer_id_from_splynx(splynx, splynx_id: int) -> Optional[str]:
|
|
"""
|
|
Get Stripe customer ID from Splynx additional attributes.
|
|
|
|
Args:
|
|
splynx: Splynx API client instance
|
|
splynx_id: Customer ID in Splynx
|
|
|
|
Returns:
|
|
Stripe customer ID if found, None otherwise
|
|
"""
|
|
try:
|
|
customer_data = get_customer_from_splynx(splynx, splynx_id)
|
|
if customer_data:
|
|
# Check for Stripe customer ID in additional attributes
|
|
additional_attrs = customer_data.get('additional_attributes', {})
|
|
stripe_id = additional_attrs.get('stripe_customer_id')
|
|
if stripe_id:
|
|
logger.info(f"Found Stripe customer ID in Splynx attributes for customer {splynx_id}")
|
|
return stripe_id
|
|
return None
|
|
except Exception as e:
|
|
logger.error(f"Error fetching Stripe ID from Splynx for customer {splynx_id}: {e}")
|
|
return None
|
|
|
|
|
|
def create_stripe_customer(stripe_processor, email: str, name: str, splynx_id: int) -> Optional[str]:
|
|
"""
|
|
Create a new Stripe customer.
|
|
|
|
Args:
|
|
stripe_processor: StripePaymentProcessor instance
|
|
email: Customer email
|
|
name: Customer name
|
|
splynx_id: Customer ID in Splynx (for metadata)
|
|
|
|
Returns:
|
|
Stripe customer ID if created successfully, None otherwise
|
|
"""
|
|
try:
|
|
# This would use the Stripe API via stripe_processor
|
|
# For now, just log that this would need implementation
|
|
logger.info(f"Creating new Stripe customer for {email} (Splynx ID: {splynx_id})")
|
|
# Implementation would go here using stripe_processor
|
|
# customer = stripe_processor.create_customer(email=email, name=name, metadata={'splynx_id': splynx_id})
|
|
# return customer.id
|
|
return None
|
|
except Exception as e:
|
|
logger.error(f"Error creating Stripe customer: {e}")
|
|
return None
|
|
|