Skip to content

Commit

Permalink
remoção de conflitos na cobrança recorrente
Browse files Browse the repository at this point in the history
  • Loading branch information
edussilva committed Aug 17, 2018
2 parents 1a13e94 + 66ad317 commit f4f0356
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pagseguro/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
__version__ = '2.0.4'
__version__ = '2.1.0'
34 changes: 34 additions & 0 deletions pagseguro/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,40 @@ class PreApprovalAdmin(admin.ModelAdmin):
]


class PreApprovalPlanAdmin(admin.ModelAdmin):

list_display = (
'id', 'name', 'amount_per_payment', 'reference', 'period',
'redirect_code'
)
list_display_links = ('id', 'name')
search_fields = ['redirect_code', 'name', 'reference']
list_filter = ('charge', 'period')


class PreApprovalHistoryInline(admin.TabularInline):

list_display = ('id', 'pre_approval', 'status', 'date')
list_display_links = ('id', )
search_fields = ['pre_approval__code', ]
list_filter = ('status', 'date')
model = PreApprovalHistory
extra = 0


class PreApprovalAdmin(admin.ModelAdmin):

list_display = (
'code', 'tracker', 'reference', 'status', 'date', 'last_event_date'
)
list_display_links = ('code', )
search_fields = ['code', 'reference']
list_filter = ('status', 'date', 'last_event_date')
inlines = [
PreApprovalHistoryInline,
]


admin.site.register(Checkout, CheckoutAdmin)
admin.site.register(Transaction, TransactionAdmin)
admin.site.register(PreApprovalPlan, PreApprovalPlanAdmin)
Expand Down
34 changes: 31 additions & 3 deletions pagseguro/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ def __repr__(self):
class PagSeguroApi(object):
def __init__(self, checkout_url=None, redirect_url=None,
notification_url=None, transaction_url=None,
pagseguro_email=None, pagseguro_token=None, currency='BRL',
pagseguro_email=None, pagseguro_token=None,
pre_approval_url=None, pre_approval_request_url=None,
pre_approval_redirect_url=None, pre_approval_notification_url=None,
pre_approval_cancel_url=None, currency='BRL',
**kwargs):
self.checkout_url = checkout_url or CHECKOUT_URL
self.redirect_url = redirect_url or PAYMENT_URL
self.notification_url = notification_url or NOTIFICATION_URL
self.transaction_url = transaction_url or TRANSACTION_URL
self.pre_approval_url = pre_approval_url or PRE_APPROVAL_URL
self.pre_approval_request_url = pre_approval_request_url or PRE_APPROVAL_REQUEST_URL
self.pre_approval_redirect_url = pre_approval_redirect_url or PRE_APPROVAL_REDIRECT_URL
self.pre_approval_notification_url = pre_approval_notification_url or PRE_APPROVAL_NOTIFICATION_URL
self.pre_approval_cancel_url = pre_approval_cancel_url or PRE_APPROVAL_CANCEL_URL
self.pagseguro_email = pagseguro_email or PAGSEGURO_EMAIL
self.pagseguro_token = pagseguro_token or PAGSEGURO_TOKEN
self.currency = currency
Expand Down Expand Up @@ -156,7 +164,7 @@ def _get_notification(self, notification_id, url, notification_type,
notification_signal, notification_status):

response = requests.get(
self.notification_url + '/{}'.format(notification_id),
'{0}/{1}'.format(url, notification_id),
params={
'email': self.base_params['email'],
'token': self.base_params['token']
Expand All @@ -177,10 +185,14 @@ def _get_notification(self, notification_id, url, notification_type,

logger.debug(
'operation=api_get_notification, '
'notification_type={}, '
'notification_id={}, '
'response_body={}, '
'response_status={}'.format(
notification_id, response.text, response.status_code
notification_type,
notification_id,
response.text,
response.status_code
)
)
return response
Expand Down Expand Up @@ -268,6 +280,14 @@ def get_pre_approval(self, pre_approval_id):
'date': timezone.now()
}

logger.debug(
'operation=api_get_pre_approval, '
'pre_approval_id={}, '
'data={!r}, '
'response_status={}'.format(
pre_approval_id, data, response.status_code
)
)
return data


Expand Down Expand Up @@ -503,6 +523,10 @@ def create_plan(self, *args, **kwargs):
}
pre_approval_create_plan_error.send(sender=self, data=data)

logger.debug(
'operation=preapproval_api_create_plan, '
'data={!r}'.format(data)
)
return data

def pre_approval_cancel(self, pre_approval_code):
Expand Down Expand Up @@ -531,4 +555,8 @@ def pre_approval_cancel(self, pre_approval_code):
'date': timezone.now(),
}

logger.debug(
'operation=preapproval_api_pre_approval_cancel, '
'data={!r}'.format(data)
)
return data
54 changes: 34 additions & 20 deletions pagseguro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@
)


PRE_APPROVAL_PERIOD_CHOICES = (
('WEEKLY', 'Semanal'),
('MONTHLY', 'Mensal'),
('BIMONTHLY', '2 vezes ao mês'),
('TRIMONTHLY', '3 vezes por mês'),
('SEMIANNUALLY', 'A cada 6 meses'),
('YEARLY', 'Anualmente'),
)

PRE_APPROVAL_CHARGE_CHOICES = (
('auto', 'Automática'),
('manual', 'Manual'),
)

PRE_APPROVAL_STATUS_CHOICES = (
('PENDING', 'Aguardando processamento do pagamento'),
('ACTIVE', 'Ativa'),
('CANCELLED', 'Cancelada'),
('CANCELLED_BY_RECEIVER', 'Cancelada pelo Vendedor'),
('CANCELLED_BY_SENDER', 'Cancelada pelo Comprador'),
('EXPIRED', 'Expirada'),
)


@python_2_unicode_compatible
class Checkout(models.Model):
code = models.CharField(
Expand Down Expand Up @@ -84,6 +108,14 @@ class Meta:

@python_2_unicode_compatible
class Transaction(models.Model):
transaction_type = models.CharField(
'tipo',
default='1',
max_length=2,
db_index=True,
choices=TRANSACTION_TYPE,
help_text='Representa o tipo da transação recebida.'
)
code = models.CharField(
'código',
max_length=100,
Expand Down Expand Up @@ -157,30 +189,26 @@ class Meta:

@python_2_unicode_compatible
class PreApprovalPlan(models.Model):

charge = models.CharField(
'Cobrança',
max_length=20,
db_index=True,
choices=PRE_APPROVAL_CHARGE_CHOICES,
help_text='Indica se a assinatura será gerenciada pelo PagSeguro (automática) ou pelo Vendedor (manual)',
)

name = models.CharField(
'Nome',
max_length=100,
unique=True,
db_index=True,
help_text='Nome/Identificador da assinatura',
)

details = models.TextField(
'Detalhes',
max_length=255,
blank=True,
help_text='Detalhes/Descrição da assinatura',
)

amount_per_payment = models.DecimalField(
'Valor da cobrança',
max_digits=9,
Expand All @@ -189,7 +217,6 @@ class PreApprovalPlan(models.Model):
null=True,
help_text='Valor exato de cada cobrança',
)

max_amount_per_payment = models.DecimalField(
'Valor máximo de cada cobrança',
max_digits=9,
Expand All @@ -198,23 +225,20 @@ class PreApprovalPlan(models.Model):
null=True,
help_text='Valor máximo de cada cobrança',
)

period = models.CharField(
'Periodicidade',
max_length=20,
db_index=True,
choices=PRE_APPROVAL_PERIOD_CHOICES,
help_text='Periodicidade da cobrança',
)

final_date = models.DateTimeField(
'Data Final',
db_index=True,
blank=True,
null=True,
help_text='Fim da vigência da assinatura',
)

max_total_amount = models.DecimalField(
'Valor máximo de cada cobrança',
max_digits=9,
Expand All @@ -223,7 +247,6 @@ class PreApprovalPlan(models.Model):
null=True,
help_text='Valor máximo de cada cobrança',
)

reference = models.CharField(
'Referência',
max_length=200,
Expand All @@ -232,7 +255,6 @@ class PreApprovalPlan(models.Model):
null=True,
help_text='A referência passada na transação.'
)

redirect_code = models.CharField(
'código',
max_length=100,
Expand All @@ -248,51 +270,45 @@ class Meta:
verbose_name = 'Assinatura: Plano'
verbose_name_plural = 'Assinaturas: Planos'


@python_2_unicode_compatible
class PreApproval(models.Model):

code = models.CharField(
'código',
max_length=100,
unique=True,
db_index=True,
help_text='O código da transação.'
)

tracker = models.CharField(
'identificador público',
max_length=100,
unique=True,
db_index=True,
help_text='Código identificador público.'
)

reference = models.CharField(
'referência',
max_length=200,
db_index=True,
blank=True,
help_text='A referência passada na transação.'
)

status = models.CharField(
'Status',
max_length=20,
db_index=True,
choices=PRE_APPROVAL_STATUS_CHOICES,
help_text='Status atual da transação.'
)

date = models.DateTimeField(
'Data',
help_text='Data em que a transação foi criada.'
)

last_event_date = models.DateTimeField(
'Última alteração',
help_text='Data da última alteração na transação.'
)

content = models.TextField(
'Transação',
help_text='Transação no formato json.'
Expand All @@ -306,22 +322,20 @@ class Meta:
verbose_name = 'Assinatura: Transação'
verbose_name_plural = 'Assinaturas: Transações'


@python_2_unicode_compatible
class PreApprovalHistory(models.Model):

pre_approval = models.ForeignKey(
PreApproval,
on_delete=models.CASCADE,
verbose_name='Transação'
)

status = models.CharField(
'Status',
max_length=20,
choices=PRE_APPROVAL_STATUS_CHOICES,
help_text='Status da transação.'
)

date = models.DateTimeField(
'Data'
)
Expand Down
1 change: 1 addition & 0 deletions pagseguro/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def update_transaction(sender, transaction, **kwargs):
date=parse(trans.get('lastEventDate'))
)


def update_pre_approval(sender, transaction, **kwargs):
from pagseguro.models import (
PreApproval, PreApprovalHistory
Expand Down
5 changes: 4 additions & 1 deletion pagseguro/tests/test_signals_pre_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import responses
from django.test import TestCase
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.utils import timezone
from dateutil.parser import parse
from mock import patch
Expand Down
1 change: 0 additions & 1 deletion pagseguro/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def receive_notification(request):
response = pagseguro_api.get_notification(
notification_code, notification_type
)

if response.status_code == 200:
if six.PY2:
return HttpResponse(six.b('Notificação recebida com sucesso.'))
Expand Down

0 comments on commit f4f0356

Please sign in to comment.