# Path: ebill_paynet/components/api.py
# class PayNetDWS:
#     """PayNet DWS web services."""
# 
#     def __init__(self, url, test_service):
#         settings = zeep.Settings(xml_huge_tree=True)
#         session = requests.Session()
#         if test_service:
#             session.verify = SSL_TEST_CERTIFICATE
#         else:
#             session.verify = SSL_PROD_CERTIFICATE
#         transport = zeep.transports.Transport(session=session)
#         self.client = zeep.Client(WSDL_DOC, transport=transport, settings=settings)
#         if url:
#             self.service = self.client.create_service(
#                 "{http://www.sap.com/DWS}DWSBinding", url
#             )
#         else:
#             self.service = self.client.service
# 
#     @staticmethod
#     def authorization(userid, password):
#         """Generate Authorization node."""
#         return {"UserName": userid, "Password": password}
# 
#     @staticmethod
#     def handle_fault(fault):
#         msg = ("{}\n" "code: {} -> {}\n" "actor: {}\n" "detail: {}\n").format(
#             fault.message.upper(),
#             fault.code,
#             fault.subcodes,
#             fault.actor,
#             html.tostring(fault.detail),
#         )
#         _logger.info("Paynet DWS fault : {}".format(msg))
#         return msg


import os
import zeep  # isort:skip
from datetime import datetime
from jinja2 import Environment, FileSystemLoader, select_autoescape
from odoo import fields, models
from odoo.modules.module import get_module_root
from odoo.addons.base.models.res_bank import sanitize_account_number
from ..components.api import PayNetDWS
            ("error", "Error"),
        ],
        default="draft",
    )
    ic_ref = fields.Char(
        string="IC Ref", size=14, help="Document interchange reference"
    )
    # Set with invoice_id.number but also with returned data from server ?
    ref = fields.Char("Reference No.", size=35)
    ebill_account_number = fields.Char("Paynet Id", size=20)
    payload = fields.Text("Payload sent")
    response = fields.Text("Response recieved")
    shipment_id = fields.Char(size=24, help="Shipment Id on Paynet service")
    payment_type = fields.Selection(
        selection=[("qr", "QR"), ("isr", "ISR"), ("esp", "ESP"), ("npy", "NPY")],
        default="qr",
        readonly=True,
    )

    def _get_ic_ref(self):
        return "SA%012d" % self.id

    def send_to_paynet(self):
        for message in self:
            message.payload = message._generate_payload()
            try:
                shipment_id = message.service_id.take_shipment(message.payload)
                message.shipment_id = shipment_id
                message.state = "sent"
            except zeep.exceptions.Fault as e:

# Path: ebill_paynet/components/api.py
# class PayNetDWS:
#     """PayNet DWS web services."""
# 
#     def __init__(self, url, test_service):
#         settings = zeep.Settings(xml_huge_tree=True)
#         session = requests.Session()
#         if test_service:
#             session.verify = SSL_TEST_CERTIFICATE
#         else:
#             session.verify = SSL_PROD_CERTIFICATE
#         transport = zeep.transports.Transport(session=session)
#         self.client = zeep.Client(WSDL_DOC, transport=transport, settings=settings)
#         if url:
#             self.service = self.client.create_service(
#                 "{http://www.sap.com/DWS}DWSBinding", url
#             )
#         else:
#             self.service = self.client.service
# 
#     @staticmethod
#     def authorization(userid, password):
#         """Generate Authorization node."""
#         return {"UserName": userid, "Password": password}
# 
#     @staticmethod
#     def handle_fault(fault):
#         msg = ("{}\n" "code: {} -> {}\n" "actor: {}\n" "detail: {}\n").format(
#             fault.message.upper(),
#             fault.code,
#             fault.subcodes,
#             fault.actor,
#             html.tostring(fault.detail),
#         )
#         _logger.info("Paynet DWS fault : {}".format(msg))
#         return msg


# Path: ebill_paynet/models/paynet_invoice_message.py
import os
import zeep  # isort:skip
from datetime import datetime
from jinja2 import Environment, FileSystemLoader, select_autoescape
from odoo import fields, models
from odoo.modules.module import get_module_root
from odoo.addons.base.models.res_bank import sanitize_account_number
from ..components.api import PayNetDWS

            ("error", "Error"),
        ],
        default="draft",
    )
    ic_ref = fields.Char(
        string="IC Ref", size=14, help="Document interchange reference"
    )
    # Set with invoice_id.number but also with returned data from server ?
    ref = fields.Char("Reference No.", size=35)
    ebill_account_number = fields.Char("Paynet Id", size=20)
    payload = fields.Text("Payload sent")
    response = fields.Text("Response recieved")
    shipment_id = fields.Char(size=24, help="Shipment Id on Paynet service")
    payment_type = fields.Selection(
        selection=[("qr", "QR"), ("isr", "ISR"), ("esp", "ESP"), ("npy", "NPY")],
        default="qr",
        readonly=True,
    )

    def _get_ic_ref(self):
        return "SA%012d" % self.id

    def send_to_paynet(self):
        for message in self:
            message.payload = message._generate_payload()
            try:
                shipment_id = message.service_id.take_shipment(message.payload)
                message.shipment_id = shipment_id
                message.state = "sent"
            except zeep.exceptions.Fault as e:

                message.response = PayNetDWS.handle_fault(e)