a
    f)                     @   sr   d dl Z d dlZd dlZd dlmZmZ d dlZG dd dZedkrndZdZ	dZ
d	Zee	eee
Zee dS )
    N)datetime	timedeltac                   @   sX   e Zd Zedd Zedejej fddZedd Z	edd
dZ
edd Zd	S )ShopifyServicec              
   C   s   |dd}zHt j| |d}|jdkr2t|jW S td|j d|j  W d S W n2 t jy } ztd| W Y d }~d S d }~0 0 d S )Napplication/jsonzX-Shopify-Access-TokenzContent-Type)headers   Error: Received status code . Response Text: Error:)requestsgetstatus_codejsonloadstextprintRequestException)urlaccess_tokenr   responsee r   M/var/www/html/python-backend/backend/services/integrations/shopify_service.pysend_get_request   s    


zShopifyService.send_get_request   c                    s   d  fddt| D S )N c                 3   s   | ]}t  V  qd S )N)secretschoice).0_charsr   r   	<genexpr>!       z6ShopifyService.generate_random_code.<locals>.<genexpr>)joinrange)sizer"   r   r!   r   generate_random_code   s    z#ShopifyService.generate_random_codec           	   
   C   s   |dd}t  }dd|ii}d|  d| d}z^tj|t||d}|jd	ks\|jd
krntd|j |W S td|j d|j  W d S W n2 tj	y } ztd| W Y d }~d S d }~0 0 d S )Nr   r   discount_codecodehttps://z-.myshopify.com/admin/api/2023-10/price_rules/z/discount_codes.json)datar   r      	Response:r	   r
   r   )
r   r(   r   postr   dumpsr   r   r   r   )	
store_namer   price_rule_idr   r)   r,   r   r   r   r   r   r   create_single_use_discount_code$   s$    

z.ShopifyService.create_single_use_discount_coder   Nc
                 C   s  |dd}
|dkrdnd}t   d d d }t  tdd	  d d d }d
d| d|d|dkrp|n| dd||d|s|rdnd|s|rdnd|rdnd di}|r|g|d
 d< |r|g|d
 d< |r|dkr|sdt|i|d
 d< |r
|g|d
 d< |	r"|	dd|d
 d< d|  d}z\tj|||
d}|jd v rjtd!|j	 |
 d
 d" W S td#|j d$|j	  W d S W n4 tjy } ztd%| W Y d }~d S d }~0 0 d S )&Nr   r   percentZ
percentageZfixed_amountiZ   )daysZ
price_rulez	StoryIt: z (do not edit)-all   Z	line_itemZentitledeachZacross)title
value_typevalueZcustomer_selectionZusage_limit	starts_atends_attarget_typeZtarget_selectionZallocation_methodZallocation_limitZentitled_product_idsZentitled_collection_idsr   Zgreater_than_or_equal_toZprerequisite_subtotal_rangeprerequisite_collection_ids)prerequisite_quantityZentitled_quantityZ*prerequisite_to_entitlement_quantity_ratior+   z1.myshopify.com/admin/api/2023-10/price_rules.json)r   r   >   r   r-   r.   idr	   r
   r   )r   utcnow	isoformatr   strr   r/   r   r   r   r   r   )r1   r   
offer_nameamountdiscount_type
product_idcollection_idmin_purchase_amountprerequisite_collection_idrC   r   r=   r?   r@   r,   r   r   r   r   r   r   create_price_rule>   st    



z ShopifyService.create_price_rulec                 C   s  |dd}d|  d}t ||}d }|r|d D ].}t|d |dd}||kr4|} qdq4|r|d}	t|	 d|  d	|	 d
}
t |
|}|r|d D ]*}|d|	kr|dr|d}t|dt|d  }d|  d	|	 d}|dd}dddd||dddgdi}t|}tt| zbtj	|||d}|j
dkrztd t|jd d W   S td|j
 d|j  W  d S W q tjy } ztd| W Y d }~ d S d }~0 0 qntd|  d S ntd d S d S ) Nr   r   r+   z7.myshopify.com/admin/api/2023-10/orders.json?status=anyorderscontact_emailzNo email providedrD   z(.myshopify.com/admin/api/2023-10/orders/z/transactions.jsontransactionsorder_id	parent_idrI   d   z/refunds.jsonZrefundZUSDTzStoryit Offer CompletionZbogus)rT   rI   kindZgateway)currencynotifyZnoterR   )r   r,   r-   zRefund created successfully.r	   r
   r   z Could not find latest order for zCould not find order data)r   r   r   r   floatr   r0   rG   r   r/   r   r   r   r   )r1   emailcashback_percentager   r   Z
orders_urlZorders_dataorderrQ   rS   Ztransactions_urlZtransactions_datatransactionrT   Zcashback_amountr   Zrefund_dataZrefund_payloadr   r   r   r   r   send_cashback   s    



zShopifyService.send_cashback)r   NN)__name__
__module____qualname__staticmethodr   stringascii_lettersdigitsr(   r3   rO   r^   r   r   r   r   r   	   s   

	   _r   __main__zconall@storyit.uszstoryit-notifications-testZ&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   )r   r   rc   r   r   r   r   r_   rZ   r1   r   r[   r^   	refund_idr   r   r   r   r   <module>   s    |