Fix Stripe handler: replace hardcoded SQLite '?' placeholders with db-type-aware '%s' for MySQL

Co-Authored-By: 's avatarClaude Sonnet 4.6 <noreply@anthropic.com>
parent 48b4f355
...@@ -73,10 +73,11 @@ class StripePaymentHandler: ...@@ -73,10 +73,11 @@ class StripePaymentHandler:
# Store payment method in database # Store payment method in database
with self.db._get_connection() as conn: with self.db._get_connection() as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" ph = '?' if self.db.db_type == 'sqlite' else '%s'
cursor.execute(f"""
INSERT INTO payment_methods INSERT INTO payment_methods
(user_id, type, identifier, is_default, is_active) (user_id, type, identifier, is_default, is_active)
VALUES (?, 'stripe', ?, 1, 1) VALUES ({ph}, 'stripe', {ph}, 1, 1)
""", (user_id, payment_method.id)) """, (user_id, payment_method.id))
conn.commit() conn.commit()
...@@ -98,36 +99,36 @@ class StripePaymentHandler: ...@@ -98,36 +99,36 @@ class StripePaymentHandler:
async def _get_or_create_customer(self, user_id: int) -> str: async def _get_or_create_customer(self, user_id: int) -> str:
"""Get existing Stripe customer or create new one""" """Get existing Stripe customer or create new one"""
# Check if customer exists in database ph = '?' if self.db.db_type == 'sqlite' else '%s'
with self.db._get_connection() as conn: with self.db._get_connection() as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute( cursor.execute(
"SELECT email, stripe_customer_id FROM users WHERE id = ?", f"SELECT email, stripe_customer_id FROM users WHERE id = {ph}",
(user_id,) (user_id,)
) )
user = cursor.fetchone() user = cursor.fetchone()
if not user: if not user:
raise ValueError(f"User {user_id} not found") raise ValueError(f"User {user_id} not found")
email = user[0] email = user[0]
stripe_customer_id = user[1] if len(user) > 1 else None stripe_customer_id = user[1] if len(user) > 1 else None
if stripe_customer_id: if stripe_customer_id:
return stripe_customer_id return stripe_customer_id
# Create new Stripe customer # Create new Stripe customer
customer = await asyncio.to_thread( customer = await asyncio.to_thread(
stripe.Customer.create, stripe.Customer.create,
email=email, email=email,
metadata={'user_id': user_id} metadata={'user_id': user_id}
) )
# Store customer ID # Store customer ID
with self.db._get_connection() as conn: with self.db._get_connection() as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute( cursor.execute(
"UPDATE users SET stripe_customer_id = ? WHERE id = ?", f"UPDATE users SET stripe_customer_id = {ph} WHERE id = {ph}",
(customer.id, user_id) (customer.id, user_id)
) )
conn.commit() conn.commit()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment