
     h\              	           d dl mZmZmZmZmZmZmZmZ d dl	m
Z d dlmZ d dlZd dlmZ d dlZd dlZd dlZ G d deeeee          Z G d deee          Z G d	 d
e          ZdS )    )ListFindCreateDeleteUpdateReplaceResourcePost)defaultN)	b64decodec                       e Zd ZdZdZddZdS )WebhookzExposes REST endpoints for creating and managing webhooks

    Usage::

        >>> web_profile = WebProfile.find("XP-3NWU-L5YK-X5EC-6KJM")
    z/v1/notifications/webhooks/Nc                     |pt                      }t          j        | j        t	          | d                   d          }t          | j                            |          |          S )zFGet the list of events types that are subscribed to a webhook
        idzevent-types)api)default_apiutiljoin_urlpathstrr	   r   get)selfr   urls      W/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/paypalrestsdk/notifications.pyget_event_typeszWebhook.get_event_types   sR     "[]]mDIs4:FFS))s3333    )N)__name__
__module____qualname____doc__r   r    r   r   r   r      s9          )D4 4 4 4 4 4r   r   c                       e Zd ZdZdZdZdZeegZd Zd Z	e
d             Ze
d             Zed	             Zed
             Zed             Ze
d             Zed             Zedd            ZdS )WebhookEventzGExposes REST endpoints for working with subscribed webhooks events
    z"/v1/notifications/webhooks-events/z*data/DigiCertHighAssuranceEVRootCA.crt.pemz3data/DigiCertSHA2ExtendedValidationServerCA.crt.pemc                 0    |                      di |           S )zMSpecify a received webhook event-id to resend the event notification
        resend)post)r   s    r   r%   zWebhookEvent.resend$   s     yy2t,,,r   c                 f    | j         }t          j        |          } || j        j                  }|S )zGet the resource sent via the webhook event, e.g. Authorization, conveniently
         wrapped in the corresponding paypalrestsdk class
        )resource_typer   
get_memberresource__dict__)r   webhook_resource_typeklassr*   s       r   get_resourcezWebhookEvent.get_resource)   s6     !% 2 5665/00r   c                 ^   t           j        d         dk    rKt          t          j        |                    d                              d                    dz            }n7t          t          j        |                    d                    dz            }| dz   |z   dz   |z   dz   |z   }|S )z<Get the input string to generate the HMAC signature
        r      utf-8l    |)sysversion_infor   binasciicrc32decodeencode)transmission_id	timestamp
webhook_id
event_bodydataexpected_sigs         r   _get_expected_sigzWebhookEvent._get_expected_sig2   s     A!##x~j&7&7&@&@&G&G&P&PQQT^^__DDx~j&7&7&@&@AAJNOOD&,y83>KcQTXXr   c                     ddl m} |                                 j                                                            d          rdS dS )z>Check that the common name in the certificate refers to paypalr   cryptoz.paypal.comTF)OpenSSLrB   get_subject
commonNamelowerendswith)certrB   s     r   _is_common_name_validz"WebhookEvent._is_common_name_valid=   sQ     	#"""""(..0099-HH 	45r   c                    ddl m} |                                }	 | j        D ]}t	          t
          j                            t
          j                            t                    |                    
                                }|                    |j        |          }|                    |           |S # t          $ r}t          |           Y d}~dS d}~ww xY w)z@Returns a certificate store with the trust chain loaded
        r   rA   N)rC   rB   	X509Store_WebhookEvent__cert_chain_pathopenosr   joindirname__file__readload_certificateFILETYPE_PEMadd_cert	Exceptionprint)clsrB   store	cert_pathcert_strrH   es          r   _get_certificate_storez#WebhookEvent._get_certificate_storeF   s     	#"""""  ""	 2 % %	RW__X-F-F	 R RSSXXZZ..v/BHMMt$$$$L 	 	 	!HHHHHHHHH	s   BB9 9
CCCc                     ddl m} |                                 }	 |                    ||          }|                                 dS # t
          $ r}t          |           Y d}~dS d}~ww xY w)zAVerify certificate using chain of trust shipped with sdk
        r   rA   TNF)rC   rB   r]   X509StoreContextverify_certificaterV   rW   )rX   rH   rB   rY   	store_ctxr\   s         r   _verify_certificate_chainz&WebhookEvent._verify_certificate_chainU   s     	#"""""**,,	//t<<I((***4 	 	 	!HHH55555	s   *A 
A,A''A,c                     |                      |          r+|                     |          r|                                sdS dS )zWVerify that certificate is unexpired, has valid common name and is trustworthy
        TF)rb   rI   has_expired)rX   rH   s     r   _verify_certificatez WebhookEvent._verify_certificatec   sM     ((.. 	33L3LT3R3R 	[_[k[k[m[m 	45r   c                    ddl m} 	 t          j        |           }|                    |j        t          |j                            }|S # t          j        j	        $ r,}t          d| z              t          |           Y d}~dS d}~ww xY w)zNFetches the paypal certificate used to sign the webhook event payload
        r   rA   z-Error retrieving PayPal certificate with url N)rC   rB   requestsr   rS   rT   r   text
exceptionsRequestExceptionrW   )cert_urlrB   rrH   r\   s        r   	_get_certzWebhookEvent._get_certl   s     	#"""""	X&&A**6+>AFLLDK"3 	 	 	AHLMMM!HHHHHHHHH	s   AA B!BBc                    ddl m} t                              ||||          }		 |                    |t          |          |	                    d          |           dS # t          $ r}
t          |
           Y d}
~
dS d}
~
ww xY w)zzVerify that the webhook payload received is from PayPal,
        unaltered and targeted towards correct recipient
        r   rA   r1   TNF)	rC   rB   r#   r?   verifyr   r8   rV   rW   )rX   r9   r:   r;   r<   rH   
actual_sig	auth_algorB   r>   r\   s              r   _verify_signaturezWebhookEvent._verify_signaturey   s    
 	#"""""#55oyR\^hii	MM$	* 5 5|7J7J77S7SU^___4 	 	 	!HHH55555	s   8A 
B)A>>Bsha256c           	      N   ddd}	 |dk    r||                                 vr||         }n'# t          $ r}	t          d           Y d}	~	dS d}	~	ww xY wt                              |          }
t                              |
          ot                              |||||
||          S )z'Verify certificate and payload
        sha256WithRSAEncryptionsha1WithRSAEncryption)SHA256withRSASHA1withRSArs   z;Authorization algorithm mapping not found in verify method.NF)valuesKeyErrorrW   r#   rm   re   rr   )rX   r9   r:   r;   r<   rk   rp   rq   _WebhookEvent__auth_algo_mapr\   rH   s              r   ro   zWebhookEvent.verify   s    
 72
 
	H$$/:P:P:R:R)R)R+I6	 	 	 	OPPP55555	 %%h////55  j,:X:XYhjsu  BL  NR  T^  `i  ;j  ;j  	js   $, 
AAAN)rs   )r   r   r   r    r   _WebhookEvent__root_cert_path%_WebhookEvent__intermediate_cert_pathrL   r%   r.   staticmethodr?   rI   classmethodr]   rb   re   rm   rr   ro   r!   r   r   r#   r#      s<        /DCT)+CD- - -
     \   \   [   [   [ 
 
 \
   [ j j j [j j jr   r#   c                       e Zd ZdZdZdS )WebhookEventTypezIExposes REST endpoint for listing available event types for webhooks
    z'/v1/notifications/webhooks-event-types/N)r   r   r   r    r   r!   r   r   r   r      s         4DDDr   r   )paypalrestsdk.resourcer   r   r   r   r   r   r	   r
   paypalrestsdk.apir   r   paypalrestsdk.utilr   r5   base64r   rg   rN   r3   r   r#   r   r!   r   r   <module>r      s@   ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 4 4 4 4 4 4 ! ! ! ! ! !         				 



4 4 4 4 4fdD&' 4 4 4"zj zj zj zj zj4t zj zj zjz5 5 5 5 5t 5 5 5 5 5r   