U
    9dJ                     @   s   d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
mZmZ d dlmZ eeZeejejdZd	d
 ZG dd deZdS )    )render)View)JsonResponseN)settings)SubscriptionPlanPayment)CancelSubscription)	CLIENT_IDCLIENT_SECRETc                 C   s   | j ddkS )NHTTP_X_REQUESTED_WITHXMLHttpRequest)METAget)request r   </var/www/html/UseVoiceDocker/src/apps/subscriptions/views.pyis_ajax   s    r   c                   @   s   e Zd Zdd ZdS )UpgradePlanViewc              
   O   s  t |rt|j z6|jjjj|jd kr@tdddW S td t	 }|jd |_
|jd |_|jj|_|jd |_d	|_|  td
 t	jj|jjd}|D ]"}|j|jd krd|_|  qtd tjj|jd}tjj|jd d}||_|  td t|jd }	|	jdkr<td tdddW S W n@ tk
r }
 z t|
 tddd W Y S d }
~
X Y nX d S )NPlanIDerrorz'You cannot upgrade to your current plan)statusmessagezCreate new PaymentOrderIDConversationIDSubscriptionIDTz Create new Payment success! )subscriptionFzTurn old payments off )user)plan_idzPlan Upgraded Suceess!   zSubscription not canceledOKz"Upgrade function is called successzSomething wrong happen)r   loggerinfoPOSTr   r   planr   r   r   transactionIDconversationIDsub_id	is_activesaveobjectsfilterr   r   r   cancelcancel_subscriptionstatus_coder   	Exception)selfr   argskwargsnew_paymentZold_paymentZpaynew_subscriptionZnew_planZcanceleder   r   r   post   s@    








zUpgradePlanView.postN)__name__
__module____qualname__r6   r   r   r   r   r      s   r   )django.shortcutsr   django.viewsr   django.httpr   loggingdjango.confr   Zapps.subscriptions.modelsr   r   r   apps.subscriptions.utilsr	   	getLogger__file__r!   PAYPAL_CLIENTIDPAYPAL_SECRETIDr,   r   r   r   r   r   r   <module>   s   
