diff --git a/padelclub_backend/settings.py b/padelclub_backend/settings.py index eeccd44..5520a72 100644 --- a/padelclub_backend/settings.py +++ b/padelclub_backend/settings.py @@ -157,8 +157,11 @@ SESSION_COOKIE_SECURE = True # Si vous utilisez HTTPS # Stripe Settings STRIPE_PUBLISHABLE_KEY = 'pk_test_51R4LrTPEZkECCx484C2KbmRpcO2ZkZb0NoNi8QJB4X3E5JFu3bvLk4JZQmz9grKbk6O40z3xI8DawHrGyUY0fOT600VEKC9ran' # Replace with your actual key STRIPE_SECRET_KEY = 'sk_test_51R4LrTPEZkECCx48PkSbEYarhts7J7XNYpS1mJgows5z5dcv38l0G2tImvhXCjzvMgUH9ML0vLMOEPeyUBtYVf5H00Qvz8t3rE' # Replace with your actual key -STRIPE_WEBHOOK_SECRET = 'your_webhook_secret' # Optional for later +STRIPE_WEBHOOK_SECRET = 'whsec_cbaa9c0c7b24041136e063a7d60fb674ec0646b2c4b821512c41a27634d7b1ba' # Optional for later STRIPE_CURRENCY = 'eur' # Set to your preferred currency +# STRIPE_PUBLISHABLE_KEY = os.environ.get('STRIPE_PUBLISHABLE_KEY', 'your_test_publishable_key') +# STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY', 'your_test_secret_key') +# STRIPE_WEBHOOK_SECRET = os.environ.get('STRIPE_WEBHOOK_SECRET', 'your_test_webhook_secret') # Add managers who should receive internal emails MANAGERS = [ diff --git a/shop/migrations/0016_order_webhook_processed.py b/shop/migrations/0016_order_webhook_processed.py new file mode 100644 index 0000000..06fe45d --- /dev/null +++ b/shop/migrations/0016_order_webhook_processed.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2025-03-21 05:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('shop', '0015_alter_product_image'), + ] + + operations = [ + migrations.AddField( + model_name='order', + name='webhook_processed', + field=models.BooleanField(default=False), + ), + ] diff --git a/shop/models.py b/shop/models.py index eebbe93..e656844 100644 --- a/shop/models.py +++ b/shop/models.py @@ -79,6 +79,7 @@ class Order(models.Model): ('PAID', 'Paid'), ('FAILED', 'Failed'), ]) + webhook_processed = models.BooleanField(default=False) def __str__(self): return f"Order #{self.id} - {self.status}" diff --git a/shop/signals.py b/shop/signals.py index 8aa490f..79c0b3e 100644 --- a/shop/signals.py +++ b/shop/signals.py @@ -46,17 +46,20 @@ def _send_order_email(instance, **kwargs): admin_url = f"{settings.SITE_URL}{reverse('admin:shop_order_change', args=[order_id])}" # Get customer info + customer_email = None if instance.user: customer_info = f"Utilisateur: {instance.user.email}" + customer_email = instance.user.email elif instance.guest_user: customer_info = f"Invité: {instance.guest_user.email} ({instance.guest_user.phone})" + customer_email = instance.guest_user.email else: customer_info = "Client inconnu" - # Construire les détails des articles - obtenir une requête fraîche des articles de la commande + # Build order item details items_list = "" if action != "DELETED": - # Utiliser une requête fraîche pour s'assurer d'avoir les données les plus récentes + # Use a fresh query to ensure we have the most recent data order_items = OrderItem.objects.filter(order_id=order_id).select_related('product', 'color', 'size') for item in order_items: color = item.color.name if item.color else "N/A" @@ -64,7 +67,7 @@ def _send_order_email(instance, **kwargs): item_line = f"- {item.quantity}x {item.product.title} (Couleur: {color}, Taille: {size}, Prix: {item.price}€)\n" items_list += item_line - # Composer l'email + # Compose the email if action == "CREATED": action_fr = "CRÉÉE" elif action == "UPDATED": @@ -74,7 +77,7 @@ def _send_order_email(instance, **kwargs): else: action_fr = action - # Traduire le statut actuel + # Translate current status status_fr_map = { "PENDING": "EN ATTENTE", "PAID": "PAYÉE", @@ -84,7 +87,7 @@ def _send_order_email(instance, **kwargs): } status_fr = status_fr_map.get(status, status) - # Traduire le statut de paiement + # Translate payment status payment_status_fr_map = { "UNPAID": "NON PAYÉE", "PAID": "PAYÉE", @@ -92,6 +95,7 @@ def _send_order_email(instance, **kwargs): } payment_status_fr = payment_status_fr_map.get(instance.payment_status, instance.payment_status) + # Send internal notification email subject = f"Commande #{order_id} {action_fr}: {status_fr}" message = f""" La commande #{order_id} a été {action_fr.lower()} @@ -110,7 +114,7 @@ Voir la commande dans le panneau d'administration: {admin_url} Ceci est un message automatique. Merci de ne pas répondre. """ - # Send email + # Send internal email recipient_list = [email for name, email in settings.MANAGERS] if not recipient_list: recipient_list = [settings.DEFAULT_FROM_EMAIL] @@ -122,3 +126,46 @@ Ceci est un message automatique. Merci de ne pas répondre. recipient_list=recipient_list, fail_silently=False, ) + + # Only send customer email for PAID status and if we have customer email + if status == OrderStatus.PAID and customer_email and instance.payment_status == "PAID": + # Generate customer-facing URLs + shop_url = f"{settings.SITE_URL}/shop" + contact_email = "support@padelclub.app" + + # Create a customer receipt email + customer_subject = f"Confirmation de votre commande #{order_id} - PadelClub" + customer_message = f""" +Bonjour, + +Nous vous remercions pour votre commande sur PadelClub ! + +Récapitulatif de votre commande #{order_id} du {instance.date_ordered.strftime('%d/%m/%Y')} : + +Statut: {status_fr} +Prix total: {total_price}€ + +Détail de votre commande : +{items_list} + +Nous nous occupons de préparer votre commande dans les plus brefs délais. + +Pour toute question concernant votre commande, n'hésitez pas à contacter notre service client : +{contact_email} + +Visitez notre boutique pour découvrir d'autres produits : +{shop_url} + +Merci de votre confiance et à bientôt sur PadelClub ! + +L'équipe PadelClub + """ + + # Send email to customer + send_mail( + subject=customer_subject, + message=customer_message, + from_email=settings.DEFAULT_FROM_EMAIL, + recipient_list=[customer_email], + fail_silently=False, + ) diff --git a/shop/templates/shop/cart.html b/shop/templates/shop/cart.html index b72c971..8db24d4 100644 --- a/shop/templates/shop/cart.html +++ b/shop/templates/shop/cart.html @@ -65,7 +65,7 @@ -{% if user.is_authenticated and cart_items %} +{% if user.is_authenticated and display_data.items %}