|
| 1 | +# Copyright 2025 - TODAY, Cristiano Mafra Junior <cristiano.mafra@escodoo.com.br> |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +from odoo import _, fields, models |
| 8 | +from odoo.exceptions import UserError |
| 9 | + |
| 10 | +from ..constants.atm_averba import ATM_AVERBA_URL |
| 11 | + |
| 12 | + |
| 13 | +class ResCompany(models.Model): |
| 14 | + |
| 15 | + _inherit = "res.company" |
| 16 | + |
| 17 | + atm_averba_environment = fields.Selection( |
| 18 | + selection=[("1", "Production"), ("2", "Homologation")], |
| 19 | + string="Environment", |
| 20 | + default="2", |
| 21 | + ) |
| 22 | + atm_averba_user = fields.Char(string="User") |
| 23 | + atm_averba_user_password = fields.Char(string="User Password") |
| 24 | + atm_code = fields.Char() |
| 25 | + atm_token = fields.Char() |
| 26 | + |
| 27 | + def get_atm_averba_environment(self): |
| 28 | + self.ensure_one() |
| 29 | + return { |
| 30 | + "url": ATM_AVERBA_URL |
| 31 | + if self.atm_averba_environment == "1" |
| 32 | + else ATM_AVERBA_URL, |
| 33 | + "usuario": self.atm_averba_user, |
| 34 | + "senha": self.atm_averba_user_password, |
| 35 | + "codigoatm": self.atm_code, |
| 36 | + } |
| 37 | + |
| 38 | + def generate_atm_token(self): |
| 39 | + self.ensure_one() |
| 40 | + config = self.get_atm_averba_environment() |
| 41 | + |
| 42 | + url = "https://webserver.averba.com.br/rest/Auth" |
| 43 | + headers = { |
| 44 | + "Accept": "application/json", |
| 45 | + "Content-Type": "application/json", |
| 46 | + } |
| 47 | + payload = { |
| 48 | + "usuario": config["usuario"], |
| 49 | + "senha": config["senha"], |
| 50 | + "codigoatm": config["codigoatm"], |
| 51 | + } |
| 52 | + |
| 53 | + try: |
| 54 | + response = requests.post(url, json=payload, headers=headers, timeout=15) |
| 55 | + response.raise_for_status() |
| 56 | + result = response.json() |
| 57 | + except requests.RequestException as e: |
| 58 | + raise UserError(_("Erro na conexão com AT&M: %s") % str(e)) |
| 59 | + except Exception as e: |
| 60 | + raise UserError(_("Erro inesperado: %s") % str(e)) |
| 61 | + |
| 62 | + token = result.get("Bearer") |
| 63 | + if not token: |
| 64 | + raise UserError(_("Token não retornado pela AT&M: %s") % result) |
| 65 | + |
| 66 | + self.atm_token = token |
| 67 | + return token |
0 commit comments