|
| 1 | +import logging |
| 2 | + |
| 3 | +from celery import shared_task |
| 4 | + |
| 5 | +slack_logger = logging.getLogger("slack_logger") |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +@shared_task(ignore_result=True) |
| 10 | +def send_payment_completed_notifications(order_id: str) -> None: |
| 11 | + """결제 완료 시 알림톡 + 이메일 자동 발송 (비동기 Celery task). |
| 12 | +
|
| 13 | + - customer_info가 없는 주문은 발송을 건너뛰고 slack_logger로 누락 사실을 기록합니다. |
| 14 | + - 알림톡과 이메일은 독립적으로 시도되며, 한 채널 실패가 다른 채널 발송을 막지 않습니다. |
| 15 | + - 실제 외부 API 호출은 send_notification_to_recipient task에서 채널별로 처리됩니다. |
| 16 | + """ |
| 17 | + from django.conf import settings |
| 18 | + from shop.order.models import Order |
| 19 | + |
| 20 | + order = Order.objects.filter_active().select_related("customer_info").filter(id=order_id).first() |
| 21 | + |
| 22 | + if order is None: |
| 23 | + slack_logger.error("결제 완료 알림 발송 실패: 주문을 찾을 수 없습니다. order_id=%s", order_id) |
| 24 | + return |
| 25 | + |
| 26 | + customer_info = getattr(order, "customer_info", None) |
| 27 | + if customer_info is None: |
| 28 | + slack_logger.error( |
| 29 | + "결제 완료 알림 발송 누락: customer_info가 없는 주문입니다. order_id=%s", |
| 30 | + order_id, |
| 31 | + ) |
| 32 | + return |
| 33 | + |
| 34 | + # TODO: 알림톡/이메일 템플릿 변수 확정 후 context 업데이트 필요. |
| 35 | + # 템플릿에서 사용하는 #{변수명} / {{ 변수명 }} 목록에 맞게 값을 추가하세요. |
| 36 | + context = { |
| 37 | + "phone": customer_info.phone, |
| 38 | + "email": customer_info.email, |
| 39 | + } |
| 40 | + |
| 41 | + _send_alimtalk(order_id, customer_info.phone, context, settings) |
| 42 | + _send_email(order_id, customer_info.email, context, settings) |
| 43 | + |
| 44 | + |
| 45 | +def _send_alimtalk(order_id: str, recipient_phone: str, context: dict, settings) -> None: |
| 46 | + from notification.models import ( |
| 47 | + NHNCloudKakaoAlimTalkNotificationHistory, |
| 48 | + NHNCloudKakaoAlimTalkNotificationTemplate, |
| 49 | + ) |
| 50 | + |
| 51 | + try: |
| 52 | + template_code = settings.NOTIFICATION.payment_completed_alimtalk_template_code |
| 53 | + template = NHNCloudKakaoAlimTalkNotificationTemplate.objects.filter_active().filter(code=template_code).first() |
| 54 | + if template is None: |
| 55 | + slack_logger.error( |
| 56 | + "결제 완료 알림톡 발송 실패: 템플릿을 찾을 수 없습니다. template_code=%s order_id=%s", |
| 57 | + template_code, |
| 58 | + order_id, |
| 59 | + ) |
| 60 | + return |
| 61 | + |
| 62 | + history = NHNCloudKakaoAlimTalkNotificationHistory.objects.create_for_recipients( |
| 63 | + template=template, |
| 64 | + recipients=[{"recipient": recipient_phone, "context": context}], |
| 65 | + ) |
| 66 | + history.send() |
| 67 | + except Exception: |
| 68 | + slack_logger.exception( |
| 69 | + "결제 완료 알림톡 발송 중 예외 발생. order_id=%s", |
| 70 | + order_id, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def _send_email(order_id: str, recipient_email: str, context: dict, settings) -> None: |
| 75 | + from notification.models import EmailNotificationHistory, EmailNotificationTemplate |
| 76 | + |
| 77 | + try: |
| 78 | + template_code = settings.NOTIFICATION.payment_completed_email_template_code |
| 79 | + template = EmailNotificationTemplate.objects.filter_active().filter(code=template_code).first() |
| 80 | + if template is None: |
| 81 | + # 이메일 템플릿은 아직 미생성 상태일 수 있으므로 warning 수준으로 기록. |
| 82 | + logger.warning( |
| 83 | + "결제 완료 이메일 발송 건너뜀: 템플릿을 찾을 수 없습니다. template_code=%s order_id=%s", |
| 84 | + template_code, |
| 85 | + order_id, |
| 86 | + ) |
| 87 | + return |
| 88 | + |
| 89 | + history = EmailNotificationHistory.objects.create_for_recipients( |
| 90 | + template=template, |
| 91 | + recipients=[{"recipient": recipient_email, "context": context}], |
| 92 | + ) |
| 93 | + history.send() |
| 94 | + except Exception: |
| 95 | + slack_logger.exception( |
| 96 | + "결제 완료 이메일 발송 중 예외 발생. order_id=%s", |
| 97 | + order_id, |
| 98 | + ) |
0 commit comments