Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Installation should generally be done via pip::

pip install django-mercadopago

If you'll be using the asynchronous processing, celery is required. You'll need
to set up celery itself for your project as well, obviously::

pip install django-mercadopago[celery]

Configuration
-------------

Expand Down Expand Up @@ -75,15 +80,19 @@ AUTOPROCESS

**Required**

If set to ``True``, notifications will be processed as soon as they are
received. Otherwise, it's up to the developer to process them.
If set to ``'SYNC'``, notifications will be processed as soon as they are
received. If set to ``'ASYNC'``, notifications will be processed asynchronously.
Celery is expected to be configured and running for this to works.

If this variable is set to ``False``, notifications will not be processed and
it will be up to the developer to call their ``process()`` method.

A signal is always fired when a notification has been created, and a common
pattern if not auto-processing is to have a celery task to process them::
pattern if not auto-processing is to use your own queue mechanism::

@receiver(notification_received)
def process_notification(sender, **kwargs):
tasks.process_notification.delay(notification=sender)
add_to_some_queue(notification=sender)

SUCCESS_URL
~~~~~~~~~~~
Expand Down
8 changes: 8 additions & 0 deletions django_mercadopago/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ class MercadoPagoConfig(AppConfig):
name = 'django_mercadopago'
label = 'mp'
verbose_name = 'MercadoPago'

def ready(self):
from django_mercadopago import models, signals

signals.notification_received.connect(
signals.process_new_notification,
sender=models.Notification,
)
23 changes: 19 additions & 4 deletions django_mercadopago/signals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from django.dispatch import Signal
import logging

from django.conf import settings
from django.dispatch import Signal

payment_received = Signal(
providing_args=['payment']
)

payment_received = Signal(providing_args=['payment'])
notification_received = Signal()

logger = logging.getLogger(__name__)


def process_new_notification(sender, instance, **kwargs):
# XXX: what if a user is being redireected and we expect the status to be
# updated?

if settings.MERCADOPAGO['autoprocess'] == 'SYNC':
instance.process()
elif settings.MERCADOPAGO['autoprocess'] == 'ASYNC':
from django_mercadopago import tasks
tasks.process_notification.delay(instance.pk)
else:
logger.debug('Notification not auto-processed.')
9 changes: 9 additions & 0 deletions django_mercadopago/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from celery import shared_task

from django_mercadopago import models


@shared_task
def process_notification(notification_id):
notification = models.Notification.objects.get(pk=notification_id)
notification.process()
2 changes: 0 additions & 2 deletions django_mercadopago/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def _create_notification(reference, topic, resource_id):
},
)

if settings.MERCADOPAGO['autoprocess']:
notification.process()
signals.notification_received.send(sender=notification)

return notification, created
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
setup_requires=['setuptools_scm'],
extras_require={
'fixtures': ['factory-boy'],
'celery-task': ['celery'],
},
classifiers=[
'Development Status :: 4 - Beta',
Expand Down