Skip to content

Commit

Permalink
crea ticket desde carro, sin asignar numero de orden
Browse files Browse the repository at this point in the history
  • Loading branch information
manureta committed Jul 23, 2020
1 parent 63541f5 commit 92f8ebe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 15 deletions.
5 changes: 3 additions & 2 deletions pretix_mercadopago/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy

try:
Expand All @@ -10,10 +11,10 @@

class PluginMeli(PluginConfig):
name = 'pretix_mercadopago'
verbose_name = 'Mercado Pago Plugin'
verbose_name = 'MercadoPago Pretix plugin'

class PretixPluginMeta:
name = gettext_lazy('Mercado Pago Plugin')
name = gettext_lazy('MercadoPago Pretix plugin')
author = 'FOSS4G team'
description = gettext_lazy('Plugin para MercadoPago como medio de pago para las entradas.')
visible = True
Expand Down
23 changes: 13 additions & 10 deletions pretix_mercadopago/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pretix.helpers.urls import build_absolute_uri as build_global_uri
from pretix.multidomain.urlreverse import build_absolute_uri

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

SUPPORTED_CURRENCIES = ['ARS', 'BRL', 'CLP','MXN','COP','PEN','UYU']
# ARS Peso argentino.
Expand All @@ -39,7 +39,7 @@


class Mercadopago(BasePaymentProvider):
identifier = 'mercadopago'
identifier = 'pretix_mercadopago'
verbose_name = _('MercadoPago')
payment_form_fields = OrderedDict([
])
Expand Down Expand Up @@ -176,7 +176,7 @@ def is_allowed(self, request: HttpRequest, total: Decimal = None) -> bool:
return super().is_allowed(request, total) and self.event.currency in SUPPORTED_CURRENCIES

def init_api(self):
if self.settings.connect_client_id and not self.settings.secret:
if self.settings.get('client_id') and not self.settings.get('secret'):
mp = mercadopago.MP(self.settings.get('client_id'))
else:
mp = mercadopago.MP(self.settings.get('client_id'),self.settings.get('secret'))
Expand All @@ -193,7 +193,7 @@ def payment_form_render(self, request) -> str:
return template.render(ctx)

def checkout_prepare(self, request, cart):
self.init_api()
mp = self.init_api()
kwargs = {}
if request.resolver_match and 'cart_namespace' in request.resolver_match.kwargs:
kwargs['cart_namespace'] = request.resolver_match.kwargs['cart_namespace']
Expand Down Expand Up @@ -234,19 +234,19 @@ def checkout_prepare(self, request, cart):
"title": __('Order for %s') % str(request.event),
"quantity": 1,
"currency_id": request.event.currency,
"unit_price": 1 #self.format_price(cart['total'])
"unit_price": float(cart['total'])
}
]
}


client_id=self.settings.get('client_id')
# client_id=self.settings.get('client_id')

# mp = mercadopago.MP(client_id, client_secret)
mp = mercadopago.MP(client_id)
# mp = MP(client_id)

# Get the payment reported by the IPN. Glossary of attributes response in https://developers.mercadopago.com
paymentInfo = mp.payment.get(kwargs["id"])
# paymentInfo = mp.get_payment(kwargs["id"])

# Show payment information
#if paymentInfo["status"] == 200:
Expand All @@ -255,8 +255,9 @@ def checkout_prepare(self, request, cart):
# return None


preferenceResult = mp.create_preference(preference)
request.session['payment_mercadopago_order'] = None
return self._create_payment(request, mp)
return self._create_payment(request, preferenceResult)

@property
def abort_pending_allowed(self):
Expand Down Expand Up @@ -286,7 +287,9 @@ def _create_payment(self, request, payment):
messages.error(request, _('We had trouble communicating with MercadoPago' + str(payment["response"])))
logger.error('Error on creating payment: ' + str(payment["response"]))
except Exception as e:
messages.error(request, _('We had trouble communicating with MercadoPago ' + str(e) + str(payment)))
# pass
messages.error(request, _('We had trouble communicating with ' +
'MercadoPago ' + str(e) + str(payment["response"])))
logger.exception('Error on creating payment: ' + str(e))

def checkout_confirm_render(self, request) -> str:
Expand Down
73 changes: 72 additions & 1 deletion pretix_mercadopago/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,81 @@
import json
from collections import OrderedDict

from django import forms
# Register your receivers here
from django.dispatch import receiver

from pretix.base.signals import register_payment_providers
from pretix.base.forms import SecretKeySettingsField
from pretix.base.signals import (
logentry_display, register_global_settings, register_payment_providers,
requiredaction_display,
)


@receiver(register_payment_providers, dispatch_uid="payment_mercadopago")
def register_payment_provider(sender, **kwargs):
from .payment import Mercadopago
return Mercadopago



@receiver(signal=logentry_display, dispatch_uid="mercadopago_logentry_display")
def pretixcontrol_logentry_display(sender, logentry, **kwargs):
if logentry.action_type != 'pretix.plugins.mercadopago.event':
return

data = json.loads(logentry.data)
event_type = data.get('event_type')
text = None
plains = {
'PAYMENT.SALE.COMPLETED': _('Payment completed.'),
'PAYMENT.SALE.DENIED': _('Payment denied.'),
'PAYMENT.SALE.REFUNDED': _('Payment refunded.'),
'PAYMENT.SALE.REVERSED': _('Payment reversed.'),
}

if event_type in plains:
text = plains[event_type]
else:
text = event_type

if text:
return _('MercadoPago reported an event: {}').format(text)

@receiver(signal=requiredaction_display, dispatch_uid="mercadopago_requiredaction_display")
def pretixcontrol_action_display(sender, action, request, **kwargs):
if not action.action_type.startswith('pretix.plugins.mercadopago'):
return

data = json.loads(action.data)

# if action.action_type == 'pretix.plugins.paypal.refund':
# template = get_template('pretixplugins/paypal/action_refund.html')
# elif action.action_type == 'pretix.plugins.paypal.overpaid':
# template = get_template('pretixplugins/paypal/action_overpaid.html')
# elif action.action_type == 'pretix.plugins.paypal.double':
# template = get_template('pretixplugins/paypal/action_double.html')

ctx = {'data': data, 'event': sender, 'action': action}
return template.render(ctx, request)

@receiver(register_global_settings, dispatch_uid='mercadopago_global_settings')
def register_global_settings(sender, **kwargs):
return OrderedDict([
('payment_mercadopago_connect_client_id', forms.CharField(
label=_('MercadoPago Connect: Client ID'),
required=False,
)),
('payment_mercadopago_connect_secret_key', SecretKeySettingsField(
label=_('MercadoPago Connect: Secret key'),
required=False,
)),
('payment_mercadopago_connect_endpoint', forms.ChoiceField(
label=_('MercadoPago Connect Endpoint'),
initial='live',
choices=(
('live', 'Live'),
('sandbox', 'Sandbox'),
),
)),
])
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def run(self):
author='Manuel Retamozo',
author_email='[email protected]',
license='Apache',

install_requires=['mercadopago'],
packages=find_packages(exclude=['tests', 'tests.*']),
include_package_data=True,
cmdclass=cmdclass,
entry_points="""
[pretix.plugin]
pretix-mercadopago=pretix_mercadopago:PluginMeli
pretix.plugins.mercadopago=pretix_mercadopago:PretixPluginMeta
""",
)

0 comments on commit 92f8ebe

Please sign in to comment.