|
| 1 | +from django.template.loader import render_to_string |
| 2 | +from django.core.mail import EmailMessage |
| 3 | +from django.conf import settings |
| 4 | +import logging |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class PaymentEmailUtil: |
| 11 | + """Email utilities for payment-related notifications""" |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def _send_email(cls, subject, template_name, context, recipient): |
| 15 | + """Internal helper to render template and send email.""" |
| 16 | + try: |
| 17 | + message = render_to_string(template_name, context) |
| 18 | + email_message = EmailMessage(subject=subject, body=message, to=[recipient]) |
| 19 | + email_message.content_subtype = "html" |
| 20 | + email_message.send() |
| 21 | + logger.info(f"Email sent successfully to {recipient}: {subject}") |
| 22 | + except Exception as e: |
| 23 | + logger.error(f"Email sending failed for {recipient}: {e}", exc_info=True) |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def send_invoice_email(cls, invoice): |
| 27 | + """Send invoice notification to customer""" |
| 28 | + try: |
| 29 | + # Get the frontend URL from settings or use default |
| 30 | + frontend_url = getattr(settings, 'FRONTEND_URL', 'http://localhost:5173') |
| 31 | + invoice_url = f"{frontend_url}/invoices/{invoice.invoice_number}" |
| 32 | + |
| 33 | + context = { |
| 34 | + 'customer_name': invoice.customer_name, |
| 35 | + 'merchant_name': invoice.user.full_name, |
| 36 | + 'invoice_number': invoice.invoice_number, |
| 37 | + 'invoice_title': invoice.title, |
| 38 | + 'invoice_description': invoice.description, |
| 39 | + 'issue_date': invoice.issue_date.strftime('%B %d, %Y'), |
| 40 | + 'due_date': invoice.due_date.strftime('%B %d, %Y'), |
| 41 | + 'total_amount': f"{invoice.total_amount:,.2f}", |
| 42 | + 'currency_symbol': invoice.wallet.currency.symbol, |
| 43 | + 'invoice_url': invoice_url, |
| 44 | + 'notes': invoice.notes, |
| 45 | + 'current_year': datetime.now().year, |
| 46 | + } |
| 47 | + |
| 48 | + cls._send_email( |
| 49 | + subject=f"Invoice {invoice.invoice_number} from {invoice.user.full_name}", |
| 50 | + template_name="invoice-created.html", |
| 51 | + context=context, |
| 52 | + recipient=invoice.customer_email, |
| 53 | + ) |
| 54 | + except Exception as e: |
| 55 | + logger.error(f"Failed to send invoice email for {invoice.invoice_number}: {e}", exc_info=True) |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def send_payment_confirmation_email(cls, payment): |
| 59 | + """Send payment confirmation email to payer""" |
| 60 | + try: |
| 61 | + # Get the frontend URL from settings or use default |
| 62 | + frontend_url = getattr(settings, 'FRONTEND_URL', 'http://localhost:5173') |
| 63 | + transaction_url = f"{frontend_url}/transactions" |
| 64 | + |
| 65 | + # Determine payment description |
| 66 | + if payment.payment_link: |
| 67 | + payment_description = payment.payment_link.title |
| 68 | + elif payment.invoice: |
| 69 | + payment_description = f"Invoice {payment.invoice.invoice_number}" |
| 70 | + else: |
| 71 | + payment_description = "Payment" |
| 72 | + |
| 73 | + context = { |
| 74 | + 'payer_name': payment.payer_name, |
| 75 | + 'merchant_name': payment.merchant_user.full_name, |
| 76 | + 'reference': payment.reference, |
| 77 | + 'payment_date': payment.created_at.strftime('%B %d, %Y at %I:%M %p'), |
| 78 | + 'payment_description': payment_description, |
| 79 | + 'amount': f"{payment.amount:,.2f}", |
| 80 | + 'fee_amount': f"{payment.fee_amount:,.2f}", |
| 81 | + 'net_amount': f"{payment.net_amount:,.2f}", |
| 82 | + 'currency_symbol': payment.payer_wallet.currency.symbol, |
| 83 | + 'new_balance': f"{payment.payer_wallet.balance:,.2f}", |
| 84 | + 'invoice_number': payment.invoice.invoice_number if payment.invoice else None, |
| 85 | + 'transaction_url': transaction_url, |
| 86 | + 'current_year': datetime.now().year, |
| 87 | + 'terms_url': f"{frontend_url}/terms", |
| 88 | + 'privacy_url': f"{frontend_url}/privacy", |
| 89 | + } |
| 90 | + |
| 91 | + cls._send_email( |
| 92 | + subject=f"Payment Confirmation - {payment.reference}", |
| 93 | + template_name="payment-confirmation.html", |
| 94 | + context=context, |
| 95 | + recipient=payment.payer_email, |
| 96 | + ) |
| 97 | + except Exception as e: |
| 98 | + logger.error(f"Failed to send payment confirmation email for {payment.reference}: {e}", exc_info=True) |
0 commit comments