Skip to content

Commit

Permalink
formateo
Browse files Browse the repository at this point in the history
  • Loading branch information
manureta committed Jul 30, 2020
1 parent bde6ec9 commit 76cacd0
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions pretix_mercadopago/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from decimal import Decimal

import mercadopago

from django import forms
from django.contrib import messages
from django.http import HttpRequest
Expand All @@ -19,7 +19,7 @@

logger = logging.getLogger('pretix.plugins.mercadopago')

SUPPORTED_CURRENCIES = ['ARS', 'BRL', 'CLP','MXN','COP','PEN','UYU']
SUPPORTED_CURRENCIES = ['ARS', 'BRL', 'CLP', 'MXN', 'COP', 'PEN', 'UYU']
# ARS Peso argentino.
# BRL Real brasilero.
# CLP Peso chileno.
Expand All @@ -30,6 +30,7 @@

LOCAL_ONLY_CURRENCIES = ['ARS']


class Mercadopago(BasePaymentProvider):
identifier = 'pretix_mercadopago'
verbose_name = _('MercadoPago')
Expand All @@ -55,16 +56,15 @@ def test_mode_message(self):
# No Refunds #
####################################################################

def payment_partial_refund_supported(payment: OrderPayment):
def payment_partial_refund_supported(self, payment: OrderPayment):
return False

def payment_refund_supported(payment: OrderPayment):
def payment_refund_supported(self, payment: OrderPayment):
return False

def execute_refund(self, refund: OrderRefund):
raise PaymentException(_('Refunding is not supported.'))


####################################################################
# Plugin Settings #
####################################################################
Expand Down Expand Up @@ -121,6 +121,7 @@ def settings_form_fields(self):

d.move_to_end('_enabled', False)
return d

def settings_content_render(self, request):
settings_content = ""
if self.settings.connect_client_id and not self.settings.secret:
Expand Down Expand Up @@ -194,7 +195,7 @@ def payment_form_render(self, request) -> str:
# When the user selects this provider
# as their preferred payment method,
# they will be shown the HTML you return from this method.
return "You will be redirected to MercadoPago now.";
return "You will be redirected to MercadoPago now."

def payment_is_valid_session(self, request):
# This is called at the time the user tries to place the order.
Expand All @@ -207,45 +208,46 @@ def execute_payment(self, request: HttpRequest, payment_obj: OrderPayment):
# this method will be called to complete the payment process.
mp = self.init_api()
preference = {
"items": [
"items": [
{
"title": __('Order {slug}-{code}').format(slug=self.event.slug.upper(),
code=payment_obj.order.code),
"quantity": 1,
"unit_price": float(payment_obj.amount),
"currency_id": payment_obj.order.event.currency
"title": __('Order {slug}-{code}').format(slug=self.event.slug.upper(),
code=payment_obj.order.code),
"quantity": 1,
"unit_price": float(payment_obj.amount),
"currency_id": payment_obj.order.event.currency
}
],
"auto_return": 'approved', #solo para las ordenes aprobadas, all
"back_urls": {
"failure":
build_absolute_uri(request.event,
'plugins:pretix_mercadopago:abort'),
"pending": "", "success":
build_absolute_uri(request.event,
'plugins:pretix_mercadopago:return')
},
"external_reference": str(payment_obj.order.code)
}
],
"auto_return": 'approved', # solo para las ordenes aprobadas, all
"back_urls": {
"failure":
build_absolute_uri(request.event,
'plugins:pretix_mercadopago:abort'),
"pending": "", "success":
build_absolute_uri(request.event,
'plugins:pretix_mercadopago:return')
},
"external_reference": str(payment_obj.order.code)
}

# Get the payment reported by the IPN.
# Glossary of attributes response in https://developers.mercadopago.com
# paymentInfo = mp.get_payment(kwargs["id"])
# Show payment information
#if paymentInfo["status"] == 200:
# if paymentInfo["status"] == 200:
# return paymentInfo["response"]
#else:
# else:
# return None

payment = mp.create_preference(preference)
payment_obj.payment_provider = payment
request.session['payment_mercadopago_preferece_id'] = str(payment['response']['id'])
request.session['payment_mercadopago_collector_id'] = str(payment['response']['collector_id'])
request.session['payment_mercadopago_order'] = payment_obj.order.pk
request.session['payment_mercadopago_payment'] = payment_obj.pk

try:
if payment:
if payment["status"] not in ( 200,201) : #ate not in ('created', 'approved', 'pending'):
if payment["status"] not in (200, 201): # ate not in ('created', 'approved', 'pending'):
messages.error(request, _('We had trouble communicating with MercadoPago' + str(payment["response"]["message"])))
logger.error('Invalid payment state: ' + str(payment["response"]))
return
Expand All @@ -254,7 +256,7 @@ def execute_payment(self, request: HttpRequest, payment_obj: OrderPayment):
link = payment["response"]["init_point"]
else:
link = payment["response"]["sandbox_init_point"]
# messages.error(request, _('Debug ' + str(link) ))
# messages.error(request, _('Debug ' + str(link) ))
# return str(link)
# if link.method == "REDIRECT" and link.rel == "approval_url":
# if request.session.get('iframe_session', False):
Expand Down Expand Up @@ -317,4 +319,4 @@ def get_connect_url(self, request):
request.session['payment_mercadopago_oauth_event'] = request.event.pk

self.init_api()
return Tokeninfo.authorize_url({'scope': 'openid profile email'})
return Tokeninfo.authorize_url({'scope': 'openid profile email'})

0 comments on commit 76cacd0

Please sign in to comment.