Skip to content

Commit

Permalink
changes reconciliation endpoint + reuse reconciliation process from w…
Browse files Browse the repository at this point in the history
…orkers
  • Loading branch information
felipao-mx committed Dec 13, 2023
1 parent 689ed19 commit a64cfa9
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 191 deletions.
67 changes: 17 additions & 50 deletions speid/commands/spei.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import datetime as dt

import click
import pytz
from mongoengine import DoesNotExist
from stpmex.business_days import get_next_business_day

from speid import app
from speid.helpers.callback_helper import set_status_transaction
from speid.helpers.transaction_helper import (
process_incoming_transaction,
stp_model_to_dict,
)
from speid.models import Event, Transaction
from speid.models.transaction import (
REFUNDS_PAYMENTS_TYPES,
STP_VALID_DEPOSITS_STATUSES,
)
from speid.processors import stpmex_client
from speid.tasks.transactions import apply_stp_deposit
from speid.types import Estado, EventType


Expand Down Expand Up @@ -67,51 +57,28 @@ def re_execute_transactions(speid_id):
def reconciliate_deposits(
fecha_operacion: dt.datetime, claves_rastreo: str
) -> None:
claves_rastreo_set = set(claves_rastreo.split(','))
not_processed = []

claves_rastreo_filter = set(claves_rastreo.split(','))
mex_query_date = dt.datetime.utcnow().astimezone(
pytz.timezone('America/Mexico_City')
)

if fecha_operacion.date() < get_next_business_day(mex_query_date):
recibidas = stpmex_client.ordenes.consulta_recibidas(fecha_operacion)
else:
recibidas = stpmex_client.ordenes.consulta_recibidas()

no_procesadas = []
for recibida in recibidas:
if recibida.claveRastreo not in claves_rastreo_filter:
continue
# Se ignora los tipos pago devolución debido a que
# el estado de estas operaciones se envían
# al webhook `POST /orden_events`
if recibida.tipoPago in REFUNDS_PAYMENTS_TYPES:
no_procesadas.append(recibida.claveRastreo)
continue

if recibida.estado not in STP_VALID_DEPOSITS_STATUSES:
no_procesadas.append(recibida.claveRastreo)
continue

for clave_rastreo in claves_rastreo_set:
try:
Transaction.objects.get(
clave_rastreo=recibida.claveRastreo,
fecha_operacion=recibida.fechaOperacion,
transaction = Transaction.objects.get(
clave_rastreo=clave_rastreo,
fecha_operacion=fecha_operacion.date(),
)
except DoesNotExist:
# Para reutilizar la lógica actual para abonar depósitos se
# hace una conversión del modelo de respuesta de
# la función `consulta_recibidas` al modelo del evento que envía
# STP por el webhook en `POST /ordenes`
stp_request = stp_model_to_dict(recibida)
click.echo(f'Depósito procesado: {recibida.claveRastreo}')
process_incoming_transaction(
stp_request, event_type=EventType.reconciled
)
transaction = None

if transaction:
not_processed.append(clave_rastreo)
else:
no_procesadas.append(recibida.claveRastreo)
processed_clave_rastreo = apply_stp_deposit(
clave_rastreo, fecha_operacion.date()
)
if not processed_clave_rastreo:
not_processed.append(clave_rastreo)

click.echo(f'No procesadas: {no_procesadas}')
click.echo(f'No procesadas: {not_processed}')


if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions speid/tasks/transactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime as dt
from typing import Dict, List
from typing import Dict, List, Optional

import cep
import pytz
Expand Down Expand Up @@ -175,7 +175,7 @@ def check_deposits_status(deposit: Dict) -> None:
return


def apply_stp_deposit(clave_rastreo, fecha_operacion) -> None:
def apply_stp_deposit(clave_rastreo, fecha_operacion) -> Optional[str]:
"""Busca una transaccion en el API de STP y la aplica si no existe"""
recibida = stpmex_client.ordenes_v2.consulta_clave_rastreo_recibida(
clave_rastreo=clave_rastreo,
Expand All @@ -185,9 +185,10 @@ def apply_stp_deposit(clave_rastreo, fecha_operacion) -> None:
recibida.tipoPago in REFUNDS_PAYMENTS_TYPES
or recibida.estado not in STP_VALID_DEPOSITS_STATUSES
):
return
return None
stp_request = stp_model_to_dict(recibida)
process_incoming_transaction(stp_request, event_type=EventType.reconciled)
return clave_rastreo


@celery.task
Expand Down
125 changes: 125 additions & 0 deletions tests/commands/cassettes/test_reconciliate_deposits_historic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
interactions:
- request:
body: '{"empresa": "TAMIZI", "claveRastreo": "PruebaDevolucion1", "tipoOrden":
"R", "fechaOperacion": "20231106", "firma": "UosxA4L6RMJ8UevJw7mYleOCYd+rTKiTRaCv07cbyVnVu0JWBjGuDwaFyTEc3rQzK0VDZrcZEmqhL1d4iuK/tdrd0zmdEpT8HbDGM+frs6KBmVrqYxHDAJZ9FBn/698lHl99aB0BAu46EFoH7nzVqRNj2prAYRRiUfIJbPBB350="}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '291'
Content-Type:
- application/json
User-Agent:
- stpmex-python/3.15.0
method: POST
uri: https://efws-dev.stpmex.com/efws/API/consultaOrden
response:
body:
string: '{"estado":0,"mensaje":"Datos consultados correctamente","respuesta":{"idEF":14643667,"claveRastreo":"PruebaDevolucion1","conceptoPago":"Pago
de prueba","cuentaBeneficiario":"646180157018877012","cuentaOrdenante":"646180117200000019","empresa":"TAMIZI","estado":"TCL","fechaOperacion":20231106,"institucionContraparte":90646,"institucionOperante":90646,"medioEntrega":3,"monto":0.01,"nombreBeneficiario":"Beneficiario","nombreOrdenante":"AXEL
SERNA MANRIQUEZ","nombreCep":null,"rfcCep":"TCU200828RX8","sello":null,"rfcCurpBeneficiario":"ND","referenciaNumerica":1234567,"rfcCurpOrdenante":"HESJ871014NZ5","tipoCuentaBeneficiario":40,"tipoCuentaOrdenante":40,"tipoPago":1,"tsCaptura":1699305268142,"tsLiquidacion":1699305273297,"causaDevolucion":16,"urlCEP":null}}'
headers:
Connection:
- keep-alive
Content-Type:
- application/json
Date:
- Wed, 13 Dec 2023 00:31:42 GMT
Server:
- nginx
Transfer-Encoding:
- chunked
Vary:
- Origin
- Access-Control-Request-Method
- Access-Control-Request-Headers
status:
code: 200
message: OK
- request:
body: '{"empresa": "TAMIZI", "claveRastreo": "PruebaLiquidacion2", "tipoOrden":
"R", "fechaOperacion": "20231106", "firma": "C+iJtjBXyXYMHY02POBYFRcQjtpOSa3fIKPACRaaDg22UQWZDJvkmZ5Qv6S1TMHlr9lIM5jkShl3sHfHp6pgKdQBKBJDNWgKycVxVar168HaSckHiQq2cy4q7/hJo7io4Czpt850bvvB2n1mLetPAr09Xu3rCWTyHKRo2nqhKC0="}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '292'
Content-Type:
- application/json
User-Agent:
- stpmex-python/3.15.0
method: POST
uri: https://efws-dev.stpmex.com/efws/API/consultaOrden
response:
body:
string: '{"estado":0,"mensaje":"Datos consultados correctamente","respuesta":{"idEF":14643670,"claveRastreo":"PruebaLiquidacion2","conceptoPago":"Pago
de prueba","cuentaBeneficiario":"646180157093203384","cuentaOrdenante":"646180117200000019","empresa":"TAMIZI","estado":"TLQ","fechaOperacion":20231106,"institucionContraparte":90646,"institucionOperante":90646,"medioEntrega":3,"monto":1.00,"nombreBeneficiario":"Beneficiario","nombreOrdenante":"AXEL
SERNA MANRIQUEZ","nombreCep":null,"rfcCep":"LOHF890619HCSPRL05","sello":null,"rfcCurpBeneficiario":"ND","referenciaNumerica":1234567,"rfcCurpOrdenante":"HESJ871014NZ5","tipoCuentaBeneficiario":40,"tipoCuentaOrdenante":40,"tipoPago":1,"tsCaptura":1699305380368,"tsLiquidacion":1699305385288,"causaDevolucion":null,"urlCEP":"https://www.banxico.org.mx/cep/go?i=90646&s=20210302&d=XfVFFhWLrRXzfkDf%2BBz644fa0iR1jPHxDJtE%2B%2FuQQzT0ZTpftV6i4L4Dl4%2BiXeZW6vhv%2BTzU6WTgYv2VRQ9BddyoZrePWh1xRQ6XXbUxuHM%3D"}}'
headers:
Connection:
- keep-alive
Content-Type:
- application/json
Date:
- Wed, 13 Dec 2023 00:31:43 GMT
Server:
- nginx
Transfer-Encoding:
- chunked
Vary:
- Origin
- Access-Control-Request-Method
- Access-Control-Request-Headers
status:
code: 200
message: OK
- request:
body: '{"empresa": "TAMIZI", "claveRastreo": "PruebaDevolucion1", "tipoOrden":
"R", "fechaOperacion": "20231106", "firma": "UosxA4L6RMJ8UevJw7mYleOCYd+rTKiTRaCv07cbyVnVu0JWBjGuDwaFyTEc3rQzK0VDZrcZEmqhL1d4iuK/tdrd0zmdEpT8HbDGM+frs6KBmVrqYxHDAJZ9FBn/698lHl99aB0BAu46EFoH7nzVqRNj2prAYRRiUfIJbPBB350="}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '291'
Content-Type:
- application/json
User-Agent:
- stpmex-python/3.15.0
method: POST
uri: https://efws-dev.stpmex.com/efws/API/consultaOrden
response:
body:
string: '{"estado":0,"mensaje":"Datos consultados correctamente","respuesta":{"idEF":14643667,"claveRastreo":"PruebaDevolucion1","conceptoPago":"Pago
de prueba","cuentaBeneficiario":"646180157018877012","cuentaOrdenante":"646180117200000019","empresa":"TAMIZI","estado":"TCL","fechaOperacion":20231106,"institucionContraparte":90646,"institucionOperante":90646,"medioEntrega":3,"monto":0.01,"nombreBeneficiario":"Beneficiario","nombreOrdenante":"AXEL
SERNA MANRIQUEZ","nombreCep":null,"rfcCep":"TCU200828RX8","sello":null,"rfcCurpBeneficiario":"ND","referenciaNumerica":1234567,"rfcCurpOrdenante":"HESJ871014NZ5","tipoCuentaBeneficiario":40,"tipoCuentaOrdenante":40,"tipoPago":1,"tsCaptura":1699305268142,"tsLiquidacion":1699305273297,"causaDevolucion":16,"urlCEP":null}}'
headers:
Connection:
- keep-alive
Content-Type:
- application/json
Date:
- Wed, 13 Dec 2023 00:31:44 GMT
Server:
- nginx
Transfer-Encoding:
- chunked
Vary:
- Origin
- Access-Control-Request-Method
- Access-Control-Request-Headers
status:
code: 200
message: OK
version: 1
Loading

0 comments on commit a64cfa9

Please sign in to comment.