
     h4                         d dl mZ d dlZd dlZd dlZd dl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 d dlmZmZ  ej        e          Z G d de          Zdad Zd	dZeZdS )
    )divisionN)
exceptions)__version____endpoint_map__c                   6   e Zd Z ej                    rdnej        Z ej                    rdnej        Z	de
j        d ej                    deZdededZdd	Zd
 Zd ZddZd ZddZddZd ZddZd Zd ZddZddZddZddZddZddZ dS )Api Nz	requests z	; python z; zPayPalSDK/PayPal-Python-SDK z ()c                    t          j        |pi |          }|                    dd          | _        | j        dk    r)| j        dk    rt	          j        dd| j        z  d          |                    d|                                           | _        |                    d| j                  | _        |d	         | _	        |d
         | _
        |                    dd          | _        d| _        d| _        |                    di           }d|v r|d         t          j        d<   |                    d          r|d         dd| _        || _        dS )zCreate API object

        Usage::

            >>> import paypalrestsdk
            >>> api = paypalrestsdk.Api(mode="sandbox", client_id='CLIENT_ID', client_secret='CLIENT_SECRET',
             ssl_options={"cert": "/path/to/server.pem"})
        modesandboxlivezConfiguration Mode InvalidzReceived: %szRequired: live or sandboxendpointtoken_endpoint	client_idclient_secretproxiesNssl_optionscertREQUESTS_CA_BUNDLEtokenBearer)access_token
token_type)util
merge_dictgetr   r   InvalidConfigdefault_endpointr   r   r   r   r   
token_hashtoken_request_atosenvironoptions)selfr$   kwargsr   s       M/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/paypalrestsdk/api.py__init__zApi.__init__   sV    B77JJvy11	949	#9#9*+G[_[dIe  hC  D  D  D

:t/D/D/F/FGG$jj)94=II,#O4zz)T22 $jj33[  /:6/BBJ+,::g 	I &wxI IDO     c                 4    t          j        | j                  S N)r   r   r   r%   s    r'   r   zApi.default_endpointA   s    #DI...r)   c                     | j         d| j        }t          j        |                    d                                        d                              dd          S )z4Find basic auth, and returns base64 encoded
        :utf-8
r	   )r   r   base64	b64encodeencodedecodereplace)r%   credentialss     r'   
basic_authzApi.basic_authD   sY     "&1C1CD 2 27 ; ;<<CCGLLTTUY[]^^^r)   c                    d}d}|d|z   }n*|d|z   }n"|                                   | j        | j        S |                     t          j        | j        |          d|t          j        d|                                 z  dd	| j        d
|pi                     }|,|*t          j	        
                                | _        || _        |S )aN  Generate new token by making a POST request

            1. By using client credentials if validate_token_hash finds
            token to be invalid. This is useful during web flow so that an already
            authenticated user is not reprompted for login
            2. Exchange authorization_code from mobile device for a long living
            refresh token that can be used to charge user who has consented to future
            payments
            3. Exchange refresh_token for the user for a access_token of type Bearer
            which can be passed in to charge user

        z/v1/oauth2/tokenzgrant_type=client_credentialsNz^grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=z'grant_type=refresh_token&refresh_token=POSTzBasic %sz!application/x-www-form-urlencodedapplication/jsonAuthorizationzContent-TypeAcceptz
User-Agentdataheaders)validate_token_hashr    	http_callr   join_urlr   r   r7   
user_agentdatetimenowr!   )r%   authorization_coderefresh_tokenr@   pathpayloadr   s          r'   get_token_hashzApi.get_token_hashJ   s     "1)v"#GG &?-OGG $$&&&*&M$-t44fO",t/@/@"@ C,DO% % }"	      %7%?$,$5$9$9$;$;D!#DOr)   c                 B   | j         r| j        r| j                            d          tt          j                                        | j         z
  }|j        |j        |j        dz  dz  z   dz  z   dz  }|| j                            d          k    rd| _        dS dS dS dS dS )zDChecks if token duration has expired and if so resets token
        
expires_inN   i  i@B )r!   r    r   rE   rF   microsecondssecondsdays)r%   deltadurations      r'   rA   zApi.validate_token_hashv   s       	'T_ 	'9L9L\9Z9Z9f%))++d.CCE"emej2o6L&LPW%WW[bcH$/--l;;;;"&	' 	' 	' 	'9f9f <;r)   c                 B    |                      |||pi           d         S )z6Wraps get_token_hash for getting access token
        r@   r   )rK   )r%   rG   rH   r@   s       r'   get_access_tokenzApi.get_access_token   s+     ""#5}gmY["\\]kllr)   c                 l    |t          j        d          |                     ||pi           d         S )zJExchange authorization code for refresh token for future payments
        NzAuthorization code needed to get new refresh token.             Refer to https://developer.paypal.com/docs/integration/mobile/make-future-payment/#get-an-auth-coderU   rH   )r   MissingConfigrK   )r%   rG   r@   s      r'   get_refresh_tokenzApi.get_refresh_token   sK     %* ,q r r r""#5w}""MMo^^r)   c                 |    | j         r2| j         dk     r)t                              d| j        z   dz              dS dS dS )zs
        Check that merchant server has PCI compliant version of TLS
        Print warning if it does not.
        )   r   r[   r   r   zWARNING: openssl version z detected. Per PCI Security Council mandate                 (https://github.com/paypal/TLS-update), you MUST update to the latest security library.N)ssl_version_infologwarningssl_versionr,   s    r'   _check_openssl_versionzApi._check_openssl_version   sl    
   	jT%:_%L%LKK+d.>> Bi ij j j j j	j 	j%L%Lr)   c                 H   t          j        |                     ||pi           |pi           }|                    d          r#t                              d|d         z             |                                  	 |                     ||t          j	        |          |          S # t          j        $ r%}dt          j        |j                  icY d}~S d}~wt          j        $ r9}| j        r+| j        r$d| _        |                     ||||          cY d}~S |d}~ww xY w)aD  Make HTTP call, formats response and does error handling. Uses http_call method in API class.

        Usage::

            >>> api.request("https://api.sandbox.paypal.com/v1/payments/payment?count=10", "GET", {})
            >>> api.request("https://api.sandbox.paypal.com/v1/payments/payment", "POST", "{}", {} )

        rH   r@   zPayPal-Request-IdzPayPal-Request-Id: %sr>   errorN)r   r   r@   r   r]   infor`   rB   jsondumpsr   
BadRequestloadscontentUnauthorizedAccessr    r   request)r%   urlmethodbodyr@   rH   http_headersrc   s           r'   rk   zApi.request   s]    LL}gmLLLgmY[] ] /00 	:HH,"#679 : : : 	##%%%	>>#vDJt4D4Dl>[[[ $ 	8 	8 	8TZ667777777 , 	 	 	 4> "&||Cw????????	s6   <*B' 'D!6CD!D!(,DD!DD!c                 n   t                               d|d|           | j                                        dk    r|                    di           }|                    di           }t                               d| j        z              t                               dt          |          dt          |                     nt                               d	           t          j                                        }t          j
        ||fd
| j        i|}t          j                                        |z
  }t                               d|j        |j        |j        |j        fz             |j                            d          }	|	rt                               d|	z             | j                                        dk    rDt                               dt          |j                  dt          |j                             |                     ||j                            d                    S )z6Makes a http call. Logs response information.
        zRequest[z]: r   r@   r?   zLevel: zRequest: 
Headers: z
Body: zNNot logging full request/response headers and body in live mode for compliancer   z#Response[%d]: %s, Duration: %s.%ss.zPayPal-Debug-Idzdebug_id: %sz	Headers: r/   )r]   rd   r   lowerr   debugstrrE   rF   requestsrk   r   status_codereasonrP   rO   r@   ri   handle_responser4   )
r%   rl   rm   r&   request_headersrequest_body
start_timeresponserS   debug_ids
             r'   rB   zApi.http_call   s)    	fffcc23339??&&$jjB77O!::fb11LIIi$)+,,,IIIO$$$$c,&7&7&79 : : : : HH`b b b &**,,
#C9 9!%9179 9$((**Z76 (/83CXEZ:\ \ 	] 	] 	] #''(9:: 	1IInx/0009??&&IIIH$%%%%s8+;'<'<'<> ? ? ? ##Hh.>.E.Eg.N.NOOOr)   c                    |j         }|dv rt          j        ||          d|cxk    rdk    rn n|rt          j        |          ni S |dk    rt          j        ||          |dk    rt          j        ||          |dk    rt          j        ||          |dk    rt          j        ||          |dk    rt          j	        ||          |d	k    rt          j
        ||          |d
k    rt          j        ||          |dk    rt          j        ||          d|cxk    rdk    rn nt          j        ||          d|cxk    rdk    rn nt          j        ||          t          j        ||d          )zValidate HTTP response
        )i-  i.  i/  i3     i+  i  i  i  i  i  i  i  i  i  i  iW  z'Unknown response code: #{response.code})ru   r   Redirectionre   rh   rg   rj   ForbiddenAccessResourceNotFoundMethodNotAllowedResourceConflictResourceGoneResourceInvalidClientErrorServerErrorConnectionError)r%   r{   ri   statuss       r'   rw   zApi.handle_response   s    %)))(7;;;F!!!!c!!!!!*194:g&&&r9s]]'':::s]]/'BBBs]],Xw???s]]-h@@@s]]-h@@@s]]-h@@@s]])(G<<<s]],Xw???F!!!!c!!!!!(7;;;F!!!!c!!!!!(7;;;,'#LN N Nr)   c                 n    |                      ||pi           }|d         d|d         dd| j        dS )zDefault HTTP headers
        rb   r    r   r:   r;   )rK   rD   )r%   rH   r@   r    s       r'   r@   zApi.headers   sX     ((}gmY[(\\
 *4L)A)A)A:nC]C]^.(/	
 
 	
r)   c                 h    |                      t          j        | j        |          d|pi |          S )zMake GET request

        Usage::

            >>> api.get("v1/payments/payment?count=1")
            >>> api.get("v1/payments/payment/PAY-1234")
        GETr@   rH   rk   r   rC   r   r%   actionr@   rH   s       r'   r   zApi.get  s4     ||DM$-@@%QXQ^\^n{||||r)   c                 n    |                      t          j        | j        |          d|pi |pi |          S )zMake POST request

        Usage::

            >>> api.post("v1/payments/payment", { 'indent': 'sale' })
            >>> api.post("v1/payments/payment/PAY-1234/execute", { 'payer_id': '1234' })

        r9   rn   r@   rH   r   r%   r   paramsr@   rH   s        r'   postzApi.post  sH     ||DM$-@@&v|Y[elerpr  CP|  Q  Q  	Qr)   c                 n    |                      t          j        | j        |          d|pi |pi |          S )zMake PUT request

        Usage::

            >>> api.put("v1/invoicing/invoices/INV2-RUVR-ADWQ", { 'id': 'INV2-RUVR-ADWQ', 'status': 'DRAFT'})
        PUTr   r   r   s        r'   putzApi.put  sH     ||DM$-@@%flXZdkdqoq  BO|  P  P  	Pr)   c                 n    |                      t          j        | j        |          d|pi |pi |          S )zMake PATCH request

        Usage::

            >>> api.patch("v1/payments/billing-plans/P-5VH69258TN786403SVUHBM6A", { 'op': 'replace', 'path': '/merchant-preferences'})
        PATCHr   r   r   s        r'   patchz	Api.patch"  sJ     ||DM$-@@'PVP\Z\fmfsqs  DQ|  R  R  	Rr)   c                 h    |                      t          j        | j        |          d|pi |          S )zMake DELETE request
        DELETEr   r   r   s       r'   deletez
Api.delete+  s4     ||DM$-@@(T[Ta_aq~|r)   r+   )NNN)NN)!__name__
__module____qualname__r   older_than_27sslOPENSSL_VERSIONr_   OPENSSL_VERSION_INFOr\   rt   r   platformpython_versionlibrary_detailsrD   r(   r   r7   rK   rA   rV   rY   r`   rk   rB   rw   r@   r   r   r   r   r    r)   r'   r   r      s        +*,,E""#2EK1t133Qtt9Q5h57777FOO 	___&J" " " "H/ / /_ _ _* * * *X' ' 'm m m m
_ _ _ _j j j       DP P P>N N N@

 

 

 

} } } }	Q 	Q 	Q 	QP P P PR R R R@ @ @ @ @ @r)   r   c                     t           x	 t          j        d         } t          j        d         }n"# t          $ r t	          j        d          w xY wt          t          j                            dd          | |          a t           S )zoReturns default api object and if not present creates a new one
    By default points to developer sandbox
    NPAYPAL_CLIENT_IDPAYPAL_CLIENT_SECRETzRequired PAYPAL_CLIENT_ID and PAYPAL_CLIENT_SECRET.                 Refer https://github.com/paypal/rest-api-sdk-python#configurationPAYPAL_MODEr   )r   r   r   )__api__r"   r#   KeyErrorr   rX   r   r   )r   r   s     r'   defaultr   3  s    
 	T
#56IJ'=>MM 	T 	T 	T* ,S T T T	T 2:>>9& &1:-Y Y YNs	   $. Ac                 .    t          | pi fi |at          S )z;Create new default api object with given configuration
    )r   r   )r$   configs     r'   
set_configr   E  s"     '-R**6**GNr)   r+   )
__future__r   r1   rE   rt   re   loggingr"   r   r   paypalrestsdk.utilr   paypalrestsdkr   paypalrestsdk.configr   r   	getLoggerr   r]   objectr   r   r   r   	configurer   r)   r'   <module>r      s              				  



 ! ! ! ! ! ! $ $ $ $ $ $ > > > > > > > >g!![@ [@ [@ [@ [@& [@ [@ [@z   $    			r)   