o
    ] cJ                     @   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/docker_usevoice/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 z|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red|_|  qTtd tjj|jd}tjj|jd d}||_|  td t|jd }	|	jdkrtd tdddW S  ty }
 zt|
 tdddW  Y d }
~
S d }
~
ww 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_paymentold_paymentpaynew_subscriptionnew_plancanceleder   r   r   post   sH   







zUpgradePlanView.postN)__name__
__module____qualname__r:   r   r   r   r   r      s    r   )django.shortcutsr   django.viewsr   django.httpr   loggingdjango.confr   apps.subscriptions.modelsr   r   r   apps.subscriptions.utilsr	   	getLogger__file__r!   PAYPAL_CLIENTIDPAYPAL_SECRETIDr,   r   r   r   r   r   r   <module>   s    
