o
    
i "                     @   s   d dl Z d dlmZmZ d dlZd dlmZ d dlmZ 	 dZdZ	ddd	d
ddddddddZ
d dl Z d dlmZ d dlmZmZ dd Zdd Z						dddZdS )    N)datetimetimezone)settings)loggerz*https://buy.itunes.apple.com/verifyReceiptz.https://sandbox.itunes.apple.com/verifyReceiptSuccessz9The App Store could not read the JSON object you providedz>The data in the receipt-data property was malformed or missingz&The receipt could not be authenticatedz-The shared secret you provided does not matchz-The receipt server is not currently availablez2This receipt is valid but subscription has expiredz"Sandbox receipt sent to productionz"Production receipt sent to sandboxzInternal data access errorzUser not found / no receipt)r   iR  i
R  iR  iR  iR  iR  R  iR  iR  iR  )Environment)VerificationExceptionSignedDataVerifierc               	   C   sd   d} g d}g }|D ]%}t j| |}t|d}||  W d    n1 s*w   Y  q
|S )Ncerts)zAppleRootCA-G3.cerzAppleRootCA-G2.cerzAppleRootCA.cerrb)ospathjoinopenappendread)dir_pathfilesbufsfpfh r   '/var/www/html/premium_crap/api/apple.pyload_apple_root_certs'   s   r   c              
      s   zK| t jdd  fdd}d}|t}|ddkr!d}|t}|dd	kr8td
t|dd |||||dd}td|  |W S  tyr } zt	|d td|  tj
ddd W Y d }~d S d }~ww )NF)zreceipt-datapasswordzexclude-old-transactionsc                    s   t j|  dd}| S )N   )jsontimeout)requestspostr   )urlresppayloadr   r   r!   Q   s   z"verify_apple_receipt.<locals>.post
Productionstatusr   Sandboxr   zverifyReceipt failed: Unknown)dataenvironmentr'   zverify_apple_receipt: z>>>>>>>>>>>>>>>>>>>>>>ERRORverify_apple_receiptT)exc_info)r   APPLE_SHARE_SECRETPROD_URLgetSANDBOX_URL	ExceptionAPPLE_STATUSr   infoprinterror)base64_receiptr!   r+   r*   er   r$   r   r,   I   s4   
r,   T'  c              
   C   s
  t d z|rt }|d}|d}t|dtj||d}	|	|}
|
jj	tj
kr-dnd}|dkrBt d t|dtj
||d}	d }|
jjrYt|dtj|d d}	|	|
jj}|rc|jrct|jnd }tt  d	 }||k}|jry|jnd |jr|jnd |jr|jnd tj|jd	 tjd
tj|jd	 tjd
|jr|jnd |jr|jnd d}dd||||
jjdW S | rtdtd ty } zt |d t d|  W Y d }~d S d }~w t!y } zt d|  t d|  W Y d }~d S d }~ww )Nz.>>>>>>>>>>>>>>>>>>>>>>in verify_apple_purchasebundleIdkeyIdT)root_certificatesenable_online_checksr+   	bundle_idapp_apple_idr&   r(   z#>>>>>>>>>>>>>>>>>>>>>>in productioni  )tz)originalTransactionIdappleTransactionId	productIdstartAtexpireAttransaction_reasonweb_order_line_item_idznotification-jws)okmethodr+   transactionisActivenotificationTypez7Legacy base64 receipt verification not implemented yet.z-Provide either base64Receipt or signedPayloadz/>>>>>>>>>>>>>>>>>>>>>>>>>>>>Verification ERROR z/Verification Exception>>>>>>>>>>>>>>>>>>>>>>>>>zGeneral Exception: z*General Exception>>>>>>>>>>>>>>>>>>>>>>>>>)"r   r4   r   r0   r
   r   SANDBOXverify_and_decode_notificationr*   r+   
PRODUCTIONsignedTransactionInfo$verify_and_decode_signed_transactionexpiresDateintr   now	timestamprA   transactionIdrC   fromtimestamppurchaseDater   utctransactionReasonwebOrderLineItemIdrL   valueNotImplementedError
ValueErrorr	   r5   r2   )r7   signed_payloadshared_secretexclude_old_transactions
timeout_ms
server_apirootsr>   r?   verifiernotifenv_strtxn
expires_ms
current_ms	is_activerJ   r8   r   r   r   verify_apple_purchaseo   s   




	
rl   )NNNTr9   N)r   r   r   r    django.confr   core.loggerr   r/   r1   r3   (appstoreserverlibrary.models.Environmentr   *appstoreserverlibrary.signed_data_verifierr	   r
   r   r,   rl   r   r   r   r   <module>   s@    "'