
    Jim(                     *   S SK J r   S SKJr  S SKJr  SSKJr  SSKJr  S SK	Jr  S SK
r
S SKrS S	KJr  S SKrS SKrS SKrS SKrS SKr\R&                  \l        \R*                  \l         " S
 S5      r " S S\5      r " S S\5      r " S S\5      rg)    )datetime)View)JsonResponse   )SessionPurchaseOrder)SessionPurchaseOrderSerializer)serializersN)settingsc                   &    \ rS rSrS rS rS rSrg) ApplySessionPurchaseOrderPayment   c                 N    S[        [        U5      S-  5      SUS.S.SS.nU/nU$ )Nusdd   Total)namedescription)currencyunit_amount_decimalproduct_datar   )
price_dataquantity)roundfloat)selftotalr   	priceData	lineItemss        a/Users/waleedhassanin/Documents/GitHub/snow_flake_back_end_deploy/ai_prompt_api/payments/views.pyconvertToLineItems3ApplySessionPurchaseOrderPayment.convertToLineItems   sH      %*/u1C*E&&1$ 
	 
	     c                 v   SnX#-  nU R                  Xa5      n[        UU[        R                  " 5       [        R                  " 5       SSSSSS9	nUR                  b  [
        R                  " SS/05      eS n	UR                  5         UR                  n	[        R                  R                  R                  S/US	US
-   [        U	5      -   US
-   [        U	5      -   S9n
U
R                  Ul        U
R                  Ul        UR                  5         [!        U5      n[#        U
R                  UR                  S.SS9$ )Nr   pending F)	r   productDescriptioncreatedDateupdatedDatestatus	sessionId
sessionUrlisFileDownloadedemailerrorzError creating purchase ordercardpaymentz&sessionpurchaseorderid=)payment_method_types
line_itemsmodesuccess_url
cancel_url)urlidsafe)r    r   r   nowr7   r	   ValidationErrorsavestripecheckoutSessioncreatestrr*   r6   r+   r   r   )r   keyNamepricer   
successURL	cancelURLr   r   sessionPurchaseOrdersessionIdPurchaseOrderIdcheckoutSessionsers               r   pay$ApplySessionPurchaseOrderPayment.pay)   sA    ++E:	3!(",,.",,.$
 
  "".--w9X8Y.Z[[#' !!##7#:#:  //118828'0!*(25O(OPSTlPm(m'03M'McRjNk'k 9  *9););&*9*=*='!!#,-AB?#6#6<P<S<STZ_``r"   c                     g)NT )r   s    r   expire'ApplySessionPurchaseOrderPayment.expireP   s    r"   rM   N)__name__
__module____qualname____firstlineno__r    rJ   rN   __static_attributes__rM   r"   r   r   r      s    &%aNr"   r   c                       \ rS rSrS rSrg)-CreateStripeCheckoutSessionPurchaserOrderViewS   c                    S nS nS nUR                   R                  SS 5      nUc  [        R                  " SS/05      eU[        R
                  :w  a  [        R                  " SS/05      e[        R                  " UR                  5      nUR                  5       S:  a  SU;   a$  US   nUS;  a  [        R                  " SS/05      eS	U;   a$  US	   nUS::  a  [        R                  " SS
/05      eSU;   a$  US   nUS::  a  [        R                  " SS/05      e[        5       n	U	R                  XEU[        R                  [        R                  5      $ [        R                  " SS/05      e)NapiKeyr.   apiKey header is requiredapiKey header is invalidr   rB   )starterpropremiumzInvalid KeyNamerC   zPrice must be greater than 0r   zQuantity must be greater than 0zRequest body is empty)headersgetr	   r;   r
   
SECRET_KEYjsonloadsbody__len__r   rJ   &PAYMENT_SUCCESS_PURCHASE_ORDER_WEB_URL%PAYMENT_CANCEL_PURCHASE_ORDER_WEB_URL)
r   requestargskwargsrB   rC   r   rY   jsonDataapplyPayments
             r   post2CreateStripeCheckoutSessionPurchaserOrderView.postT   s   $$Xd3>--w9T8U.VWWX(((--w9S8T.UVV::gll+!H$"9-"==%55wAR@S6TUU(" )A:%55wA_@`6abbX%#J/q=%55wAb@c6dee;=L##G(8;j;jks  lZ  lZ  [  [--w9P8Q.RSSr"   rM   NrP   rQ   rR   rS   rm   rT   rM   r"   r   rV   rV   S   s     Tr"   rV   c                       \ rS rSrS rSrg),ExpireStripeCheckoutSessionPurchaseOrderViewv   c                 N    [        5       nUR                  5       n[        SS0SS9$ )Nr6   r%   Fr8   )r   rN   r   )r   rh   ri   rj   rl   	isSuccesss         r   rm   1ExpireStripeCheckoutSessionPurchaseOrderView.postw   s)    79 '')	U2KU33r"   rM   Nro   rM   r"   r   rq   rq   v   s    4r"   rq   c                   ,    \ rS rSrS rS rS rS rSrg)SendFileView}   c                     [         R                  " SU5      nU(       a  UR                  S5      $ [         R                  " SU5      nU(       a  UR                  S5      $ [        S5      e)z2Extract the file ID from a Google Drive share URL.z/d/([a-zA-Z0-9_-]+)r   zid=([a-zA-Z0-9_-]+)z"Could not extract file ID from URL)researchgroup
ValueError)r   	drive_urlmatchs      r   extract_file_idSendFileView.extract_file_id~   sR    		0)<;;q>!		0)<;;q>!=>>r"   c                    U R                  U5      nSU 3n[        R                  " 5       nUR                  USS9nUR                  R                  5        H0  u  pgUR                  S5      (       d  M  UR                  USU0SS9n  O   SUR                  R                  SS	5      ;   aJ  [        R                  " S
UR                  5      nU(       a"  UR                  USUR                  S5      0SS9nUR                  5         SU 3n	UR                  R                  SS	5      n
[        R                  " SU
5      nU(       a  UR                  S5      n	XR                  4$ )zHDownload a publicly shared Google Drive file. Returns (filename, bytes).z/https://drive.google.com/uc?export=download&id=T)streamdownload_warningconfirm)paramsr   z	text/htmlzContent-Typer%   z name="confirm"\s+value="([^"]+)"r   file_zContent-Dispositionzfilename="([^"]+)")r   requestsr?   r`   cookiesitems
startswithr_   rz   r{   textr|   raise_for_statuscontent)r   r~   file_idr6   sessionresponsekeyvaluer   filenamecds              r   download_from_drive SendFileView.download_from_drive   sA   &&y1?yI""$;;s4;0 #**002JC~~011";;sIu3Ed;S 3 (**..~rBBIIA8==QE";;sIu{{1~3NW[;\!!# 7)$!!"7<		/4{{1~H))))r"   c           
      X   U R                  U5      u  p4 [        R                  R                  [        R
                  R                  SS5      U/SU 3SU S3U[        R                  " U5      R                  5       S./S.5      ng! [         a  n[        S	U 35         S nAg
S nAff = f)N
EMAIL_FROMz&Your File <onboarding@snowflakeco.com>zFile from Google Drive: z8<h1>Your attachment is ready</h1><p>Please find <strong>z</strong> attached.</p>)r   r   )fromtosubjecthtmlattachmentsTzError: F)r   resendEmailssendosenvironr`   base64	b64encodedecode	Exceptionprint)r   r~   to_emailr   
file_bytesresultes          r   getFileByUrlSendFileView.getFileByUrl   s    #77	B	!]]''

|5]^j5hZ@RS[R\\st %-#)#3#3J#?#F#F#H ) F  	!GA3-  	!s   A1B 
B)B$$B)c                 f   UR                   R                  SS 5      nUc  [        R                  " SS/05      eU[        R
                  :w  a  [        R                  " SS/05      e[        R                  " UR                  5      nS nSnUR                  5       S:  Ga  SU;   a  US   nO[        R                  " SS/05      eS	U;   a  US	   nO[        R                  " SS
/05      e[        R                  R                  US9nUR                  5       S:  Gas  US   n	[        R                  R                  R!                  U	R"                  5      n
U
R$                  S:X  Ga  U	R&                  S:X  a  S nU	R(                  S:X  a  [        R*                  nO[U	R(                  S:X  a  [        R,                  nO:U	R(                  S:X  a  [        R.                  nO[        R                  " SS/05      eUb[  U R1                  X5      nUS:X  a+  SU	l        Xyl        U	R5                  5         [7        SXgS.5      $ [        R                  " SS/05      e[        R                  " SS/05      e[7        SS0SS9$ [        R                  " SS/05      e[        R                  " SS/05      e[7        SS0SS9$ )NrY   r.   rZ   r[   zwhassanin@gmail.comr   r7   z"id is required in the request bodyr-   z%email is required in the request body)r7   paidFr\   r]   r^   zInvalid Pack SelectionTsent)sendFileStatusr7   r-   zFile already SentzFile Path Not Foundmessagez File has already been downloadedr8   zNo Payments madez5SessionPurchaseOrder with the given ID does not existz(File downloaded and emailed successfully)r_   r`   r	   r;   r
   ra   rb   rc   rd   re   r   objectsfilterr=   r>   r?   retriever*   payment_statusr,   r&   STARTERPACKPROPACKPREMIUMPACKr   r-   r<   r   )r   rh   ri   rj   rY   rk   sessionPurchaseOrderIdr-   dataListselectedr   filePathisSents                r   rm   SendFileView.post   s   $$Xd3>--w9T8U.VWWX(((--w9S8T.UVV::gll+!%%!x)1$&!117=a<b2cdd(" )!117=d<e2fgg+33::>T:UH!A%#A; //11::8;M;MN))V300E9#'#66)C'/';';H%88EA'/'7'7H%88IE'/';';H"-"="=wIaHb>c"dd#/%)%6%6x%FF%~<@ 916 ('3fRh4w'x x&1&A&A7M`LaBb&c c"-"="=wI^H_>`"aa+Y7Y,Z`eff%55wAS@T6UVV!117=t<u2vwwY'QRX]^^r"   rM   N)	rP   rQ   rR   rS   r   r   r   rm   rT   rM   r"   r   rw   rw   }   s    ?*>4:_r"   rw   )r   django.viewsr   django.httpr   modelsr   r	   r   rest_frameworkrb   r=   django.confr
   r   r   rz   r   r   STRIPE_SECRET_KEYapi_keyRESEND_API_KEYr   rV   rq   rw   rM   r"   r   <module>r      s      $ ( 7 &     	  	  ++((< <|!TD !TF44 4~_4 ~_r"   