def execute(order_val: dict): # Get version version = 0 if "version" in order_val: version = order_val['version'] if time_in_range(START_DOWNTIME, STOP_DOWNTIME, datetime.utcnow().time()): raise ScheduleError transaction = Transaction() try: input = factory.create(version, **order_val) transaction = input.transform() if not clabe.validate_clabe(transaction.cuenta_beneficiario) and ( not luhnmod10.valid(transaction.cuenta_beneficiario)): raise MalformedOrderException() except (MalformedOrderException, TypeError, ValueError): transaction.set_state(Estado.error) transaction.save() raise MalformedOrderException() try: prev_trx = Transaction.objects.get(speid_id=transaction.speid_id) # Si la transacción ya esta como succeeded termina # Puede suceder cuando se corre la misma tarea tiempo después # Y la transacción ya fue confirmada por stp assert prev_trx.estado != Estado.succeeded transaction = prev_trx transaction.events.append(Event(type=EventType.retry)) except DoesNotExist: transaction.events.append(Event(type=EventType.created)) transaction.save() pass except AssertionError: # Para evitar que se vuelva a mandar o regresar se manda la excepción raise ResendSuccessOrderException() if transaction.monto > MAX_AMOUNT: transaction.events.append(Event(type=EventType.error)) transaction.save() raise MalformedOrderException() now = datetime.utcnow() try: # Return transaction after 2 hours of creation assert (now - transaction.created_at) < timedelta(hours=2) transaction.create_order() except ( AssertionError, InvalidAccountType, InvalidAmount, InvalidInstitution, InvalidTrackingKey, PldRejected, ValidationError, ): transaction.set_state(Estado.failed) transaction.save()
def execute(order_val): # Get version version = 0 if "version" in order_val: version = order_val['version'] transaction = Transaction() try: input = factory.create(version, **order_val) transaction = input.transform() if not clabe.validate_clabe(transaction.cuenta_beneficiario) and ( not luhnmod10.valid(transaction.cuenta_beneficiario)): raise MalformedOrderException() except (MalformedOrderException, TypeError, ValueError): transaction.set_state(Estado.error) transaction.save() raise MalformedOrderException() try: prev_trx = Transaction.objects.get(speid_id=transaction.speid_id) transaction = prev_trx transaction.events.append(Event(type=EventType.retry)) except DoesNotExist: transaction.events.append(Event(type=EventType.created)) pass if transaction.monto > MAX_AMOUNT: transaction.events.append(Event(type=EventType.error)) transaction.save() raise MalformedOrderException() order = transaction.get_order() transaction.save() # Send order to STP order.monto = order.monto / 100 res = order.registra() if res is not None and res.id > 0: transaction.stp_id = res.id transaction.events.append( Event(type=EventType.completed, metadata=str(res))) else: transaction.events.append( Event(type=EventType.error, metadata=str(res))) transaction.save()