Skip to content

Commit

Permalink
Merge 570aed4 into 6738b9d
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo8990 committed Oct 1, 2019
2 parents 6738b9d + 570aed4 commit 68e8e8e
Show file tree
Hide file tree
Showing 24 changed files with 438 additions and 333 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ lint:
pycodestyle --ignore=E402 speid/ tests/

polish:
black -S -l 79 speid/*
isort -rc --atomic speid/*
black -S -l 79 speid/ tests/
isort -rc --atomic speid/ tests/

clean-pyc:
find . -name '__pycache__' -exec rm -r "{}" +
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## SPEID

[![Build Status](https://travis-ci.com/cuenca-mx/speid.svg?branch=master)](https://travis-ci.com/cuenca-mx/speid)
[![Coverage Status](https://coveralls.io/repos/github/cuenca-mx/speid/badge.svg?branch=master&t=1b4Ddg)](https://coveralls.io/github/cuenca-mx/speid?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/cuenca-mx/speid/badge.svg?branch=HEAD)](https://coveralls.io/github/cuenca-mx/speid?branch=HEAD)
[![](https://images.microbadger.com/badges/image/cuenca/speid:1.9.4.svg)](https://microbadger.com/images/cuenca/speid:1.9.4 "Get your own image badge on microbadger.com")
[![](https://images.microbadger.com/badges/version/cuenca/speid:1.9.4.svg)](https://microbadger.com/images/cuenca/speid:1.9.4 "Get your own version badge on microbadger.com")
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
Expand Down
2 changes: 1 addition & 1 deletion env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PYTHONPATH=/speid
FLASK_SKIP_DOTENV=1
AMPQ_ADDRESS=rabbitmq
AMPQ_ADDRESS_TYPE=host
SPEID_ENV=prod
SPEID_ENV=debug
FLASK_APP=speid
FLASK_ENV=development
STP_WSDL=tests/conf_files/stp_wsdl.wsdl
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Flask>=1.0,<1.1
stpmex>=1.0.0,<2.0.0
stpmex>=2.0.4,<3.0.0
boto3>=1.9,<2.0
celery>=4.2,<4.3
python-hosts>=0.4,<0.5
clabe>=0.2.1,<0.3
luhnmod10>=1.0.2
sentry-sdk>=0.5.3
sentry-sdk>=0.11.1
vcrpy>=2.0.1
flask-mongoengine>=0.9.5
blinker>=1.4
Expand Down
10 changes: 4 additions & 6 deletions speid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@

import boto3
import sentry_sdk
import stpmex
from flask import Flask
from flask_mongoengine import MongoEngine
from python_hosts import Hosts, HostsEntry
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.flask import FlaskIntegration
from stpmex import Client


class CJSONEncoder(json.JSONEncoder):

def default(self, o):
if isinstance(o, Enum):
encoded = o.name
Expand Down Expand Up @@ -74,14 +73,13 @@ def default(self, o):
with open(STP_PRIVATE_LOCATION) as fp:
private_key = fp.read()

stpmex.configure(
wsdl_path=STP_WSDL,
stpmex_client = Client(
empresa=STP_EMPRESA,
priv_key=private_key,
priv_key_passphrase=STP_KEY_PASSPHRASE,
prefijo=int(STP_PREFIJO),
demo=SPEID_ENV is not 'prod',
)

import speid.commands
import speid.models
import speid.views
import speid.commands
58 changes: 32 additions & 26 deletions speid/commands/spei.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from datetime import datetime

import click
import pandas

from speid import app
from speid import app, stpmex_client
from speid.helpers import callback_helper
from speid.models import Event, Request, Transaction
from speid.types import Estado, EventType

import pandas


@app.cli.group('speid')
def speid_group():
Expand All @@ -34,8 +33,9 @@ def callback_spei_transaction(transaction_id, transaction_status):
callback_helper.set_status_transaction(
transaction.speid_id, transaction.estado.name
)
transaction.events.append(Event(type=event_type,
metadata=str('Reversed by SPEID command')))
transaction.events.append(
Event(type=event_type, metadata=str('Reversed by SPEID command'))
)
transaction.save()
return transaction

Expand All @@ -54,27 +54,31 @@ def re_execute_transactions(speid_id):
order = transaction.get_order()
transaction.save()

order.monto = order.monto / 100

res = order.registra()
res = stpmex_client.registrar_orden(order)

if res is not None and res.id > 0:
transaction.stp_id = res.id
transaction.events.append(Event(type=EventType.completed,
metadata=str(res)))
transaction.events.append(
Event(type=EventType.completed, metadata=str(res))
)
else:
transaction.events.append(Event(type=EventType.error,
metadata=str(res)))
transaction.events.append(
Event(type=EventType.error, metadata=str(res))
)

transaction.save()


@speid_group.command()
@click.option('--transactions', default='transactions.csv',
help='CSV file with transactions')
@click.option(
'--transactions',
default='transactions.csv',
help='CSV file with transactions',
)
@click.option('--events', default='events.csv', help='CSV file with events')
@click.option('--requests', default='requests.csv',
help='CSV file with requests')
@click.option(
'--requests', default='requests.csv', help='CSV file with requests'
)
def migrate_from_csv(transactions, events, requests):
"""
Hace la migración de una base de datos Postgres a MongoDB, los datos deben
Expand All @@ -91,13 +95,14 @@ def migrate_from_csv(transactions, events, requests):
institucion_ordenante=lambda x: str(x),
institucion_beneficiaria=lambda x: str(x),
cuenta_ordenante=lambda x: str(x),
cuenta_beneficiario=lambda x: str(x)
))
cuenta_beneficiario=lambda x: str(x),
),
)
transactions_list = transactions_list.where(
(pandas.notnull(transactions_list)), None)
(pandas.notnull(transactions_list)), None
)
events_list = pandas.read_csv(events)
events_list = events_list.where(
(pandas.notnull(events_list)), None)
events_list = events_list.where((pandas.notnull(events_list)), None)
transactions = []
for _, t in transactions_list.iterrows():
t['stp_id'] = t['orden_id']
Expand All @@ -116,16 +121,17 @@ def migrate_from_csv(transactions, events, requests):
Event(
type=EventType[e['type']],
metadata=e['meta'],
created_at=datetime.strptime(e['created_at'],
'%Y-%m-%d %H:%M:%S.%f')
))
created_at=datetime.strptime(
e['created_at'], '%Y-%m-%d %H:%M:%S.%f'
),
)
)

transaction.id = None
transactions.append(transaction)

requests_list = pandas.read_csv(requests)
requests_list = requests_list.where(
(pandas.notnull(requests_list)), None)
requests_list = requests_list.where((pandas.notnull(requests_list)), None)
requests = []
for _, r in requests_list.iterrows():
r['method'] = r['method'].upper()
Expand Down
2 changes: 1 addition & 1 deletion speid/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from speid.types import EventType

from .helpers import date_now, EnumField
from .helpers import EnumField, date_now


class Event(Document):
Expand Down
8 changes: 2 additions & 6 deletions speid/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ def camel_to_snake(s):
return _underscorer2.sub(r'\1_\2', subbed).lower()


def snake_to_camel(s):
words = s.split('_')
return words[0] + "".join(r.title() for r in words[1:])


def handler(event):
"""
http://docs.mongoengine.org/guide/signals.html?highlight=update
Expand Down Expand Up @@ -144,7 +139,8 @@ def mongo_to_dict(obj, exclude_fields: list = None) -> Union[dict, None]:
return_data[field_name] = data
else:
return_data[field_name] = mongo_to_python_type(
obj._fields[field_name], data)
obj._fields[field_name], data
)

return return_data

Expand Down
2 changes: 1 addition & 1 deletion speid/models/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from speid.types import HttpRequestMethod

from .helpers import date_now, EnumField
from .helpers import EnumField, date_now


class Request(Document):
Expand Down
68 changes: 46 additions & 22 deletions speid/models/transaction.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from mongoengine import (DateTimeField, Document, IntField, ListField,
ReferenceField, StringField)
from stpmex import Orden
from stpmex.ordenes import ORDEN_FIELDNAMES

from speid import STP_EMPRESA
from speid.helpers import callback_helper
from speid.types import Estado, EventType

from .events import Event
from .helpers import (EnumField, date_now, mongo_to_dict, snake_to_camel,
updated_at)
from .helpers import EnumField, date_now, mongo_to_dict, updated_at


@updated_at.apply
Expand Down Expand Up @@ -110,27 +109,52 @@ def confirm_callback_transaction(self):
Event(type=EventType.completed, metadata=str(response))
)

def get_order(self):
trx_dict = self.to_dict()
order_dict = {
snake_to_camel(k): v
for k, v in trx_dict.items()
if snake_to_camel(k) in ORDEN_FIELDNAMES and (
trx_dict[k] is not None)
}
order = Orden(**order_dict)
order.fechaOperacion = None
order.institucionOperante = self.institucion_ordenante
order.institucionContraparte = self.institucion_beneficiaria
order.nombreBeneficiario = self.nombre_beneficiario[:38].strip()
order.nombreOrdenante = self.nombre_ordenante[:38].strip()
def get_order(self) -> Orden:
order = Orden(
monto=self.monto / 100.0,
conceptoPago=self.concepto_pago,
nombreBeneficiario=self.nombre_beneficiario,
cuentaBeneficiario=self.cuenta_beneficiario,
institucionContraparte=self.institucion_beneficiaria,
tipoCuentaBeneficiario=self.tipo_cuenta_beneficiario,
nombreOrdenante=self.nombre_ordenante,
cuentaOrdenante=self.cuenta_ordenante,
rfcCurpOrdenante=self.rfc_curp_ordenante,
tipoCuentaOrdenante=self.tipo_cuenta_ordenante,
iva=self.iva,
)

if self.institucion_ordenante:
order.institucionOperante = self.institucion_ordenante

if self.clave_rastreo:
order.claveRastreo = self.clave_rastreo

if self.referencia_numerica:
order.referenciaNumerica = self.referencia_numerica

if self.rfc_curp_beneficiario:
order.rfcCurpBeneficiario = self.rfc_curp_beneficiario

if self.medio_entrega:
order.medioEntrega = self.medio_entrega

if self.prioridad:
order.prioridad = self.prioridad

if self.tipo_pago:
order.tipoPago = self.tipo_pago

if self.topologia:
order.topologia = self.topologia

self.clave_rastreo = self.clave_rastreo or order.claveRastreo
self.tipo_cuenta_beneficiario = self.tipo_cuenta_beneficiario or (
order.tipoCuentaBeneficiario)
self.rfc_curp_beneficiario = self.rfc_curp_beneficiario or (
order.rfcCurpBeneficiario)
order.rfcCurpBeneficiario
)
self.referencia_numerica = self.referencia_numerica or (
order.referenciaNumerica)
self.empresa = self.empresa or order.empresa
order.referenciaNumerica
)
self.empresa = self.empresa or STP_EMPRESA

return order
2 changes: 1 addition & 1 deletion speid/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def make_celery(app):
celery_app = Celery(
app.import_name,
broker=app.config['CELERY_BROKER_URL'],
include=['speid.tasks.orders']
include=['speid.tasks.orders'],
)
celery_app.conf.update(app.config)
celery_app.conf.task_default_queue = os.environ['TASK_DEFAULT_QUEUE']
Expand Down
5 changes: 2 additions & 3 deletions speid/tasks/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mongoengine import DoesNotExist
from sentry_sdk import capture_exception

from speid import stpmex_client
from speid.exc import MalformedOrderException
from speid.models import Event, Transaction
from speid.tasks import celery
Expand Down Expand Up @@ -68,9 +69,7 @@ def execute(order_val):
transaction.save()

# Send order to STP
order.monto = order.monto / 100

res = order.registra()
res = stpmex_client.registrar_orden(order)

if res is not None and res.id > 0:
transaction.stp_id = res.id
Expand Down
2 changes: 1 addition & 1 deletion speid/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HttpRequestMethod(Enum):

class EventType(Enum):
created = 'CREATE' # When a transaction has been received
retry = 'RETRY' # When the transaction has been retry
retry = 'RETRY' # When the transaction has been retry
completed = 'COMPLETE' # The request was processed with no errors
error = 'ERROR' # Something happened when the response was obtained
received = 'RECEIVED' # When we get the response from a transaction made
Expand Down
9 changes: 5 additions & 4 deletions speid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def decorated(*args, **kwargs):
return make_response(jsonify(body), status_code)

endpoint = options.pop('endpoint', None)
app.add_url_rule(
rule, endpoint, decorated, methods=['GET'], **options)
app.add_url_rule(rule, endpoint, decorated, methods=['GET'], **options)
return view

return decorator
Expand All @@ -29,7 +28,8 @@ def decorated(*args, **kwargs):

endpoint = options.pop('endpoint', None)
app.add_url_rule(
rule, endpoint, decorated, methods=['PATCH'], **options)
rule, endpoint, decorated, methods=['PATCH'], **options
)
return view

return decorator
Expand All @@ -44,7 +44,8 @@ def decorated(*args, **kwargs):

endpoint = options.pop('endpoint', None)
app.add_url_rule(
rule, endpoint, decorated, methods=['POST'], **options)
rule, endpoint, decorated, methods=['POST'], **options
)
return view

return decorator

0 comments on commit 68e8e8e

Please sign in to comment.