U
    Vii                     @   sJ  d dl mZmZmZmZmZ d dlZd dlZeeddZ	dZ
dZdZdZi Zdd Ze	jd	d
dgddd Zdd Ze	jdd
dgddd Ze	jdd
dgddd Ze	ddd Ze	ddd Ze	jdd
gddd Ze	jd d
gdd!d" Ze	jd#d
gdd$d% Ze	jd&dgdd'd( Ze	jd)d
gdd*d+ ZdS ),    )Flaskrequestjsonifyrender_templateredirectNz./)template_folderZ7377724106160787ZXXXXXXc                 C   s   t jdrt jd}ttd< ttd< dtd< dt j d|  td< |td< | d	krntjd
td}d|j	 S | dkrtd= t
td< tjdtd}d|j	 S |S dS )Ncode	client_idclient_secretZauthorization_code
grant_typezhttps://z/access/Zredirect_urimercadolibre(https://api.mercadolibre.com/oauth/tokenjson#  --- MercadoLibre --- <br><br><br>mercadopago'https://api.mercadopago.com/oauth/token"  --- MercadoPago --- <br><br><br>zCode not provided.)r   argsget	CLIENT_IDcredentialsCLIENT_SECREThostrequestsposttextCLIENT_SECRET_MP)platformr   response r    /var/www/html/p0ch4t-app/app.pyaccess   s"    

r"   z/access/<string:platform>GETPOST)methodsc                 C   s   t | }|S N)r"   r   r   r    r    r!   access_to_code"   s    r(   c                 C   s   t jdr|t jd}ttd< ttd< dtd< |td< | dkrXtjdtd}d|j S | d	krxtjd
td}d|j S |S dS )Nrefresh_tokenr	   r
   r   r   r   r   r   r   r   r   zRefresh_token not provided.)	r   r   r   r   r   r   r   r   r   )r   r)   r   r    r    r!   refresh'   s    

r*   z/refresh/<string:platform>c                 C   s   t | }|S r&   )r*   r'   r    r    r!   refresh_access_token8   s    r+   z/notificationsc               	   C   s4   t jr0t j} tdd}||  W 5 Q R X dS dS )Nznotifications.txtazNotifications saved.zNo notifications received)r   r   openwrite)r   Znotificationr    r    r!   notifications_action=   s    r/   z"/wallet_connect/generate_agreementc                  C   sf   ddddd} t jdt | ddt id	}ztt|jd
 ddW S    td |j Y S X d S )Nz;https://oauth.pfelilpe.com/wallet_connect/confirm_agreementgQ	@zTest agreement)Zvalidation_amountdescription)Z
return_urlZagreement_datazChttps://api.mercadopago.com/v2/wallet_connect/agreements?client.id=AuthorizationBearer r   headersZagreement_urii.  )r   u.   [X] No se recibió el parámetro agreement_uri)	r   r   r   r   r   r   loadsr   printdatar   r    r    r!   generate_agreementF   s    r9   z!/wallet_connect/confirm_agreementc                  C   sx   t jdrpt jdrpdt jdi} tjdt jd d| ddt id}tjdt ddt id	 |jS d
S d S )NZ	agreementr   z9https://api.mercadopago.com/v2/wallet_connect/agreements/z/payer_tokenr1   r2   r3   zchttps://api.mercadopago.com/v2/wallet_connect/agreements/{request.args.get('agreement')}?client_id=)r4   z/You must provide agreement and code parameters.)	r   r   r   r   r   r   deleter   r   r7   r    r    r!   confirm_agreementP   s    (r;   z/mercadopagoc                   C   s   t dS )Nzmercadopago.html)r   r    r    r    r!   mercadopago_payY   s    r<   z/mercadopago/successc                   C   s   dS )NzPayment succesfully.r    r    r    r    r!   success_payment]   s    r=   z/mercadopago/errorc                   C   s   dS )NzError on payment.r    r    r    r    r!   error_paymenta   s    r>   z/mercadopago/api/paymentc               
   C   sv   dd l } | t}t }t|d|ddd|d|dd|d	id
}| |}|d }t	| |S )Nr   amounttoken   z
Blue shirtZpaymentMethodIdZissuerIdemailZcardholderEmail)Ztransaction_amountr@   Zinstallmentsr0   Zpayment_method_idZ	issuer_idZpayerr   )
r   ZSDKCLIENT_SECRET_MP_TESTr   get_jsonfloatr   paymentcreater6   )r   ZsdkZpayment_dataZpayment_responserF   r    r    r!   api_paymente   s"    
 rH   z/headersc                   C   s
   t tjS r&   )strr   r4   r    r    r    r!   r4   ~   s    r4   )flaskr   r   r   r   r   r   r   __name__appr   r   r   rC   r   r"   router(   r*   r+   r/   r9   r;   r<   r=   r>   rH   r4   r    r    r    r!   <module>   s:   



	




