o
    pj                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZmZ ddlmZ ddlmZ h d	ZdddZdddZdddZdS )zBNative Stripe webhook verification and platform event translation.    )annotationsN)Any)funcselect)BillingWebhookErrorSIGNATURE_TOLERANCE_SECONDS)get_control_plane_session)BillingProductMapping>   customer.subscription.createdcustomer.subscription.deletedcustomer.subscription.updatedraw_bodybytessignature_headerstrreturnNonec           	   
     s   t d}|stdi }|dD ]}| d\}}}|r)||g | qz
t|d d }W n t	t
tfyC   tdd w tt | tkrRtdt|d	t|d
d |  tj  t fdd|dg D s{tdd S )NSTRIPE_WEBHOOK_SECRETz Stripe webhook is not configured,=tr   zInvalid Stripe-Signature headerz6Stripe webhook timestamp is outside the allowed windowzutf-8ascii   .c                 3  s    | ]	}t  |V  qd S )N)hmaccompare_digest).0	candidateexpected D/var/www/html/flask_server/bridge_platform/billing/stripe_adapter.py	<genexpr>-   s    z*verify_stripe_signature.<locals>.<genexpr>v1z Invalid Stripe webhook signature)osgetenvr   splitstrip	partition
setdefaultappendintKeyError
IndexError
ValueErrorabstimer   r   newencoder   hashlibsha256	hexdigestanyget)	r   r   secretvaluespartkey	separatorvalue	timestampr   r   r    verify_stripe_signature   s4   

r>   payloaddict[str, Any]c                 C  s$  t | dpd}|tvr| dd|pd i dS | dp!i dp&i }t|ts0td	|d
r7dnt |dp>d}|dpFi }|dpMi dpRg }|ret|d tre|d dpdi ni }| dd| |d|d|d|d||d|d|ddddS )Ntype idzstripe.ignored.unknown)rC   rA   datarE   objectzStripe event object is invalidz.deletedcanceledstatusmetadataitemsr   pricezsubscription.	tenant_idcustomercurrent_period_startcurrent_period_endcancel_at_period_endF)rL   customer_idsubscription_idprice_idrH   rN   rO   rP   )r   r6   SUPPORTED_STRIPE_EVENTS
isinstancedictr   endswith)r?   stripe_typestripe_objectrH   rI   rJ   rK   r   r   r    translate_stripe_event1   s6   
 " 
rZ   c                  C  s   t dd} t dd}t dd}t "}t|tttj	
tjdktjd }W d    n1 s:w   Y  d| dsJ|drLd	nd
t| t|t|t|t| o]||dk|dd
S )NSTRIPE_SECRET_KEYrB   STRIPE_PUBLISHABLE_KEYr   stripeTsk_test_pk_test_testunconfiguredr   F)
providermodesecret_key_configuredpublishable_key_configuredwebhook_secret_configuredready_for_webhooksready_for_checkoutprices_configuredconfigured_price_countlive_payments_enabled)r#   r$   r   r*   executer   r   countr	   
mapping_idwhererb   	is_activeis_
scalar_one
startswithbool)
secret_keypublishable_keywebhook_secretsessionprice_countr   r   r    stripe_readinessP   s0   

rz   )r   r   r   r   r   r   )r?   r@   r   r@   )r   r@   )__doc__
__future__r   r2   r   r#   r/   typingr   
sqlalchemyr   r    bridge_platform.billing.webhooksr   r   config.control_planer   bridge_platform.tenants.modelsr	   rT   r>   rZ   rz   r   r   r   r    <module>   s    

