
    Di                        d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZmZ  ej                  d      ZddZd	dd
d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)z
Payrix transaction submission utilities.

Submits a sale/charge to Payrix via `/txns` using a previously saved
Payrix payment method token, and returns a normalized transaction object.

Pure function style: no FastAPI request objects.
    )annotationsN)AnyDictOptional)payrix_request)(map_payrix_payload_to_transaction_status!transaction_status_enum_to_stringpayrixc                (    |D ]  }|| v s| |   c S  y )N )bodykeysks      e/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/src/apps/_lib/utils/payrix_transactions.py_extract_body_fieldr      s&     97N     cardT)payment_method_typemerchant_idcapturec           
       K   |j                         dk(  rdnd}| ||||dd}t        ddd||t        |      nd	t        |      d
       d	{   }	|	j                         }
t	        |
ddd      }t        |
      }t        |      }||t	        |
d      xs | t	        |
d      xs ||
d}t        j                  dd||t        |      ndt        |      |t        |             |S d       |S 7 w)a  
    Submit a payment transaction to Payrix `/txns` using a saved token.

    Args:
        amount: Transaction amount (as provided to Payrix)
        currency: ISO currency (e.g., "usd")
        customer_id: Internal customer id in our system
        payrix_token: The Payrix payment method token stored earlier
        payment_method_type: Payment method type ("card" or "ach")
        merchant_id: Optional internal merchant id for logging context
        capture: Whether to capture immediately (sale)

    Returns:
        Normalized dict: {transaction_id, status, amount, currency, raw_response}

    Raises:
        PayrixApiError: on non-2xx response, with status_code, error body, request_id
    r            )amountcurrencytokenr   typeorigin
create_txnPOSTz/txnsNstored)	operationmethodpathjsonr   customer_idr   idtxnIdtransactionIdr   r   )transaction_idstatusr   r   raw_responsezOprovider=payrix op=%s status=%s merchant_id=%s customer_id=%s transaction_id=%s )	lowerr   strr&   r   r   r	   loggerinfo)r   r   r'   payrix_tokenr   r   r   payrix_typepayloadrespr   txn_idinternal_statusnormalized_status
normalizeds                  r   submit_payrix_charger;      s-    < +002f<!!K G  (3(?C$T$ D  99;D tWoFF>tDO9/J !#%dH5?'j9EX"J KKY'3KK)F  02 Cs   AC2C0B"C2)r   Dict[str, Any]r   r0   returnr   )r   zfloat | intr   r0   r'   intr3   r0   r   r0   r   zOptional[int]r   boolr=   r<   )__doc__
__future__r   loggingtypingr   r   r   src.core.payrix.httpr   src.core.payrix.status_mapperr   r	   	getLoggerr1   r   r;   r   r   r   <module>rG      s    #  & & /
 
		8	$  &!%KK K 	K
 K K K K Kr   