B
    CDbJ&                 @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ ejZejZG d	d
 d
e
ZG dd de
ZdS )    N)settings)redirect)	QueryDict)TemplateResponse)APIView)OrderPaymentRecord)get_http_schemac               @   s"   e Zd Zedd ZdddZdS )PaypalTransactionViewc          
   C   sn   dd l }t| }|  }|dttd |dddid|||d|||d||d	d
dgd}|S )Nr   Zsandbox)modeZ	client_idZclient_secretZsalepayment_methodZpaypalz7{}://{}/cloud/api/v1/internal/payment/paypal/status/{}/)Z
return_urlZ
cancel_url)totalcurrencyzSoftware Subscription)amountdescription)ZintentpayerZredirect_urlstransactions)paypalrestsdkr	   Zget_hostZ	configurePAYPAL_SANDBOX_CLIENT_IDPAYPAL_SANDBOX_SECRETPaymentformat)requestorder_numberorder_payment_priceorder_payment_currentr   ZschemeZhostpayment r   EG:\easytimepro\master/mysite/cloud\api\payment\paypal\paypal_views.py_initial_transaction   s$    
z*PaypalTransactionView._initial_transactionNc             C   s   t jj||jjdd}| s2d}t|dd|iS t jj|d}|j}|dkrbd}t|dd|iS |j	j
}| ||||}| rxJ|jD ] }	|	jdkrt|	j}
t|
S qW nd}t|j t|dd|iS d S )	N   )numberZ
company_idpayment_statusfalsez!cloud/payment/payment_status.htmlr"   )r!   r   approval_url)r   objectsfilteruserZcurrent_companyexistsr   getZpayment_priceZprice_strategyr   r   createZlinksZrelstrZhrefr   printerror)selfr   r   r   orderr"   r   r   r   linkr$   r   r   r   r)   >   s.    


zPaypalTransactionView.get)N)__name__
__module____qualname__staticmethodr   r)   r   r   r   r   r
      s   *r
   c               @   s"   e Zd Zedd ZdddZdS )PaypalStatusViewc       	      C   s   ddl }tjj| d}|j|}tjj|d }|rHtjj|d}nt }||_	d|_
| |_|d d |_t|d d d	 d
 |_|d d d	 d |_tj|d d|_|d |_|d |_|  |d|ird|_|  d}nd}|S )a3  
        :paypal return response
        {
            "id": "PAYID-L5EMUNQ1HW96653DP311344S",
            "intent": "sale",
            "state": "created",
            "cart": "66841884YU2554004",
            "payer": {
                "payment_method": "paypal",
                "status": "VERIFIED",
                "payer_info": {
                    "email": "robin.chen@zkteco.com",
                    "first_name": "Robin",
                    "last_name": "Chen",
                    "payer_id": "VU38LMFDVE6SC",
                    "shipping_address": {
                        "recipient_name": "Robin Chen",
                        "line1": "Flat no. 507 Wing A Raheja Residency",
                        "line2": "Film City Road",
                        "city": "Mumbai",
                        "state": "Maharashtra",
                        "postal_code": "400097",
                        "country_code": "IN"
                    },
                    "country_code": "IN",
                    "business_name": "Robin Chen's Test Store"
                }
            },
            "transactions": [
                {
                    "amount": {
                        "total": "200000.00",
                        "currency": "INR"
                    },
                    "payee": {
                        "merchant_id": "JNXPAJWENZTE4",
                        "email": "sb-htbhu2989778@personal.example.com"
                    },
                    "description": "Software Subscription",
                    "item_list": {
                        "shipping_address": {
                            "recipient_name": "Robin Chen",
                            "line1": "Flat no. 507 Wing A Raheja Residency",
                            "line2": "Film City Road",
                            "city": "Mumbai",
                            "state": "Maharashtra",
                            "postal_code": "400097",
                            "country_code": "IN"
                        }
                    },
                    "related_resources": []
                }
            ],
            "redirect_urls": {
                "return_url": "http://robin.localhost/cloud/api/v1/internal/payment/paypal/status/?paymentId=PAYID-L5EMUNQ1HW96653DP311344S",
                "cancel_url": "http://robin.localhost/cloud/api/v1/internal/payment/paypal/status/"
            },
            "create_time": "2020-08-28T09:11:18Z",
            "update_time": "2020-08-28T09:12:02Z",
            "links": [
                {
                    "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L5EMUNQ1HW96653DP311344S",
                    "rel": "self",
                    "method": "GET"
                },
                {
                    "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L5EMUNQ1HW96653DP311344S/execute",
                    "rel": "execute",
                    "method": "POST"
                },
                {
                    "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-66841884YU2554004",
                    "rel": "approval_url",
                    "method": "REDIRECT"
                }
            ]
        }
        :param order_number:
        :param payment_id:
        :param payer_id:
        :return: payment status
        r   N)r!   )r/      r   statusr   r   r   r   Zcreate_timez%Y-%m-%dT%H:%M:%SZidZcartpayer_id   truer#   )r   r   r%   r)   r   findr   r&   r(   r/   r   Ztxn_idr7   floatZ
tnx_amountr   datetimestrptimeZtxn_date
payment_idr9   ZsaveZexecuter"   )	r   r@   r9   r   r/   r   Zpayment_record_existsZpayment_recordr"   r   r   r   check_payment_statusb   s.    T

z%PaypalStatusView.check_payment_statusNc       	      C   sv   t jj|d}| s*d}t|dd|iS t|j}d|krDd}n|d }|d }| |||}t|dd|iS d S )N)r!   r#   z!cloud/payment/payment_status.htmlr"   Z	paymentIdZPayerID)	r   r%   r&   r(   r   r   dictZquery_paramsrA   )	r.   r   r   r   r/   r"   Zrequest_parameters_dictr@   r9   r   r   r   r)      s    zPaypalStatusView.get)N)r1   r2   r3   r4   rA   r)   r   r   r   r   r5   `   s   tr5   )r>   Zdjango.confr   Zdjango.shortcutsr   Zdjango.httpr   Zdjango.template.responser   Zrest_framework.viewsr   Zmysite.cloud.modelsr   r   Zmysite.base.utilsr	   r   r   r
   r5   r   r   r   r   <module>   s   N