o
    .yh	5                     @   sh   d dl Z d dlmZ d dlmZmZ ddlmZmZm	Z	 ddl
mZ d dlZeeZG dd dZdS )	    N)settings)TemplateContext   )SMSLogEmailLogNotificationTemplate)EmailServicec                   @   s@   e Zd Zdd Z			dddZdddZd	d
 ZdddZdS )NotificationServicec                 C   s   ddl m} | }|dd| _|dd| _|dd| _|dd| _|dd	| _|d
d| _|dd| _	t
 | _d S )Nr   )get_sms_settingsapi_url api_key
partner_id
short_codemethodPOSTphone_parameterphonemessage_parametermessage)settings.utilsr   getsms_api_urlsms_api_keysms_partner_idsms_short_code
sms_methodsms_phone_parametersms_message_parameterr	   email_service)selfr   sms_settings r#   6/var/www/html/optinet_system/notifications/services.py__init__   s   zNotificationService.__init__Nr   c                 C   s0   | j j||||p	d|	||
|||||d\}}|S )z&Send email using the new email servicegeneral)recipient_emailsubjectcontent
email_typerecipient_namesent_bycontext_datacustomer_id
payment_id
invoice_idtemplate_typehtml_content)r    
send_email)r!   emailr(   r   r1   r.   r/   r0   r,   r+   r-   r2   success	error_msgr#   r#   r$   r3      s   
zNotificationService.send_emailc	                 C   s  z;t | dddd}|drd|dd  }n!|dr*|dd }n|d	r6|dd }n	|ds?d| }|drJt|d
krQtd| d}	|rjz
tjj|dd}	W n
 tj	yi   Y nw t
jj||||	||||dd	}
| jr| jsd|
_d|
_|
  W dS |rt|dk rd|
_d| |
_|
  W dS d| ji}| jr| jnd}t |||< | jr| jnd}t |||< | jr| j|d< | jr| j|d< z3| j dkrtj| j|dd}n	tj| j|dd}| }d|v rd|d< td| j  td |  td!|j d"|j  |jd#krz| }t|t rd$|v r|d$}|d%krOd&|
_|
  td'|  W W W dS |d(d)}|d*d}d+| d,| }|rm|d-| 7 }d|
_||
_|
  t!d.| d,|  W W W dS d/|v sd0|v r|d0d/ksd/|v r|d|d/d)}d|
_d1| |
_|
  t!d.| d,|  W W W dS W n
 ty   Y nw d&|
_|
  td'|  W W dS d|
_d2|j d,|j |
_|
  t!d.| d3|j d-|j  W W dS  tj"j#y< } z$d|
_d4t | |
_|
  t!d5| d,t |  W Y d}~W dS d}~ww  t$yZ } zt!d6t |  W Y d}~dS d}~ww )7zSend SMS using TextSMS API r   -0254r   Nz+254+   z.Invalid phone number format after processing: Tr1   	is_activepending)	recipient_phoner+   r   template_usedr.   r/   r0   r,   statusfailedzSMS API not configuredF
   zInvalid phone number format: apikeymobiler   	partnerID	shortcodeGET   )paramstimeout)datarL   z***masked***zSMS API Request to zSMS API Parameters: zSMS API Response (z):    zresponse-codei  sentzSMS sent successfully to zresponse-descriptionzUnknown errorerrorsz
API Error z: z - zSMS failed to errorrB   zAPI Error: zHTTP z: HTTP zNetwork error: zSMS API network error for zSMS service error: )%strstripreplace
startswithlen
ValueErrorr   objectsr   DoesNotExistr   creater   r   rB   error_messagesaver   r   r   r   r   upperrequestspostcopyloggerinfostatus_codetextjson
isinstancedictrQ   
exceptionsRequestException	Exception)r!   phone_numberr   r1   r.   r/   r0   r,   r+   rA   sms_log
api_paramsphone_param_namemessage_param_nameresponsedebug_paramsresponse_dataresponse_coder6   rP   
full_errorer#   r#   r$   send_sms.   s   











 zNotificationService.send_smsc              
   C   sZ   zt |}t|}||W S  ty, } ztdt|  |W  Y d}~S d}~ww )z!Render template with context datazError rendering template: N)r   r   renderrj   ra   rQ   rR   )r!   template_contentr-   templatecontextru   r#   r#   r$   render_template   s   z#NotificationService.render_templatepayment_receivedc           
      C   s  z|j |j|j| |j|jr|jjnd|jdd}z&t	j
jd| dd}| |j|}| j|j|d| |j|j|j d W n# t	jye   d|j d	|j  d
}| j|j||j|j|j d Y nw |dkrs| j| W dS z%t	j
jd| dd}| j|j|j|jd| |j|j|j |d W W dS  t	jy   | j|jd|j d|j  d|j d|j|j|j d Y W dS w  ty }	 ztdt|	  W Y d}	~	dS d}	~	ww )z+Send payment notification via SMS and emailr   z%Y-%m-%d %H:%M)customer_namer.   amountpayment_methodr/   invoice_numberdatesms_Tr=   )rk   r   r1   r.   r/   r+   zPayment of KES z received for z. Thank you!)rk   r   r.   r/   r+   r|   email_)r4   r(   r   r1   r.   r/   r+   r-   zPayment Confirmation - zDear z,

Your payment of KES z has been received.

Thank you!)r4   r(   r   r.   r/   r+   z$Error sending payment notification: N)	full_namer.   r~   get_payment_method_displayr/   invoicer   
created_atstrftimer   rX   r   r{   r)   rv   r   idrY   r    send_payment_confirmation_emailr3   r4   r(   rj   ra   rQ   rR   )
r!   customerpaymentnotification_typer-   sms_templatesms_messagedefault_messageemail_templateru   r#   r#   r$   send_payment_notification   s   




"z-NotificationService.send_payment_notification)NNNNNr   NN)NNNNNr   )r|   )__name__
__module____qualname__r%   r3   rv   r{   r   r#   r#   r#   r$   r
      s    

 
r
   )r^   django.confr   django.templater   r   modelsr   r   r   r    r	   logging	getLoggerr   ra   r
   r#   r#   r#   r$   <module>   s   
