
    gEi
                         d Z ddlZddlZddlmZmZ ddlmZ ddlZ ej                  e
      Z G d d      Z e       Zy)uD   
Service pour l'intégration avec l'API de paiement Paygate Global.
    N)DictAny)settingsc                   8    e Zd Zd Zdededededeeef   f
dZy)	PaymentServicec                 f    t         j                  | _        d| _        t         j                  | _        y )Nz$https://paygateglobal.com/api/v1/pay)r   PAYGATE_API_TOKEN	api_tokenpaygate_url	NGROK_URL
return_url)selfs    D/home/www/lebenam.kofcorporation.com/app/services/payment_service.py__init__zPaymentService.__init__   s$    !33A",,    
identifieramountphone_numbernetworkreturnc           	      2  K   | j                   s t        j                  d       t        d      d}| j                   |||||d}	 t	        j
                         4 d{   }|j                  | j                  |d       d{   }|j                          |j                         }	t        j                  d|	        |	cddd      d{    S 7 u7 Q7 	# 1 d{  7  sw Y   yxY w# t        j                  $ rB}
t        j                  d	|
j                  j                  d
|
        t        d|
       d}
~
wt        j                  $ r}
t        j                  d|
j                   j"                   d|
j                  j                  d
|
j                   j$                          t        d|
j                   j$                         d}
~
wt        j&                  $ r. t        j                  dj$                          t        d      w xY ww)u  
        Effectue un paiement via l'API Paygate Global.

        Args:
            identifier (str): Identifiant unique pour la transaction.
            amount (float): Montant à payer.
            phone_number (str): Numéro de téléphone de l'utilisateur.
            network (str): Réseau mobile (ex: "MTN", "MOOV").

        Returns:
            Dict[str, Any]: La réponse JSON de l'API Paygate Global.
        z6PAYGATE_API_TOKEN is not set in environment variables.z$PAYGATE_API_TOKEN is not configured.u"   Paiement de prescription médicale)
auth_tokenr   r   descriptionr   r   N<   )datatimeoutzPaygate Global API response: z#An error occurred while requesting z: z,Erreur de connexion au service de paiement: zError response z while requesting zErreur de l'API de paiement: z/Failed to decode JSON from Paygate Global API: u'   Réponse invalide de l'API de paiement.)r
   loggererror
ValueErrorhttpxAsyncClientpostr   raise_for_statusjsoninfoRequestErrorrequesturlConnectionErrorHTTPStatusErrorresponsestatus_codetextJSONDecodeError)r   r   r   r   r   r   paramsclientr+   resultexcs              r   pay_with_paygatezPaymentService.pay_with_paygate   s    & ~~LLQRCDD: ..(&$
	H((*  f!'T-=-=FTV!WW))+!;F8DE  W    !! 	XLL>s{{>QQSTWSXYZ!$PQTPU"VWW$$ 	RLL?3<<+C+C*DDVWZWbWbWfWfViiklolxlxl}l}k~  A<S\\=N=N<OPQQ## 	HLLJ8==/Z[FGG	Hs   AHC. CC. !"CC=CC. CC. HC. CC. C+C" C+'C. *H+C. .H=D>>HA<GAHHN)	__name__
__module____qualname__r   strfloatr   r   r3    r   r   r   r      sJ    -
2H2H 2H 	2H
 2H 
c3h2Hr   r   )__doc__r    r$   typingr   r   app.core.configr   logging	getLoggerr4   r   r   payment_servicer9   r   r   <module>r@      sD       $ 			8	$8H 8Ht !"r   