Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch duplicate document exception on X3 #180 #182

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/billing/services/financial_processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TransactionProcessorInterface:
Each new transcation processor needs to implements its logic by signing to this class.
"""

def send_transaction_to_processor(self, transaction: Transaction) -> str:
def send_transaction_to_processor(self, transaction: Transaction) -> dict:
raise Exception("This method needs to be implemented")


Expand Down
2 changes: 1 addition & 1 deletion apps/billing/services/processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self) -> None:
self.__user_processor_auth = getattr(settings, "USER_PROCESSOR_AUTH")
self.__user_processor_password = getattr(settings, "USER_PROCESSOR_PASSWORD")

def send_transaction_to_processor(self, transaction: Transaction) -> str:
def send_transaction_to_processor(self, transaction: Transaction) -> dict:
"""
This method sends the transaction informations to the `Sage X3` service.
"""
Expand Down
42 changes: 40 additions & 2 deletions apps/billing/services/transaction_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import xmltodict

from apps.billing.models import Transaction
from apps.billing.services.financial_processor_service import ProcessorInstantiator, TransactionProcessorInterface
from apps.billing.services.processor_service import SageX3Processor
Expand All @@ -12,11 +14,47 @@ class TransactionService:
def __init__(self) -> None:
self.__processor: TransactionProcessorInterface = ProcessorInstantiator(processor=SageX3Processor)

def run_transaction_steps(self, transaction: Transaction):
def __check_transaction_state(self, document_id):
pass

def send_transaction_to_processor(self, transaction: Transaction) -> str:
"""
This method receives a Transaction to send to the processor and deals with the request result.

From the result is extracted the document id, which is provided to invocate the `__check_transaction_state`
method, that checks and updates the transaction status.
"""

try:
response_from_service = self.__processor.send_transaction_to_processor(transaction=transaction)
response_from_service = response_from_service["soapenv:Envelope"]["soapenv:Body"]

if "multiRef" in list(dict(response_from_service).keys()):
message = "Nº Fatura NAU já registada no documento: "
if message in response_from_service["multiRef"]["message"]:
document_id = response_from_service["multiRef"]["message"].replace(message, "")
self.__check_transaction_state(document_id=document_id)

return document_id

document_id = self.__processor.send_transaction_to_processor(transaction=transaction)
result = xmltodict.parse(response_from_service["wss:saveResponse"]["saveReturn"]["resultXml"]["#text"])
document_id = ""
for r in result["RESULT"]["GRP"]:
for field in r["FLD"]:
if field["@NAME"] == "NUM":
document_id = field["#text"]
break

if document_id:
break

return document_id
except Exception as e:
raise e

def run_steps_to_send_transaction(self, transaction: Transaction) -> None:
try:
document_id = self.send_transaction_to_processor(transaction=transaction)
self.__check_transaction_state(document_id=document_id)
except Exception as e:
raise e
Loading
Loading