Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Trace only http requests
Krzysztof Kulig500384bbf2be
diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.pyindex 4f521967c2..395e6e1d23 100644--- a/saleor/payment/gateways/np_atobarai/api.py+++ b/saleor/payment/gateways/np_atobarai/api.py@@ -31,7 +31,7 @@ from .errors import (add_action_to_code,get_error_messages_from_codes,)-from .utils import get_shipping_company_code, np_atobarai_opentracing_trace+from .utils import get_shipping_company_codelogger = logging.getLogger(__name__)@@ -44,57 +44,51 @@ def register_transaction(On pending status from NP the transaction is cancelled andreason for pending is returned as error message."""- with np_atobarai_opentracing_trace("np-atobarai.checkout.payments.register"):- action = TRANSACTION_REGISTRATION- result, error_codes = register(config, payment_information)+ action = TRANSACTION_REGISTRATION+ result, error_codes = register(config, payment_information)- if error_codes:- error_messages = get_error_messages_from_codes(- action, error_codes=error_codes- )- return errors_payment_result(error_messages)+ if error_codes:+ error_messages = get_error_messages_from_codes(action, error_codes=error_codes)+ return errors_payment_result(error_messages)- status = result["authori_result"]- transaction_id = result["np_transaction_id"]- error_messages = []+ status = result["authori_result"]+ transaction_id = result["np_transaction_id"]+ error_messages = []- if status == PaymentStatus.PENDING:- if cancel_error_codes := cancel(config, transaction_id).error_codes:- handle_unrecoverable_state(- order, "cancel", transaction_id, cancel_error_codes- )- error_messages = get_error_messages_from_codes(- action, error_codes=result["authori_hold"]+ if status == PaymentStatus.PENDING:+ if cancel_error_codes := cancel(config, transaction_id).error_codes:+ handle_unrecoverable_state(+ order, "cancel", transaction_id, cancel_error_codes)-- return PaymentResult(- status=status,- psp_reference=transaction_id,- errors=error_messages,+ error_messages = get_error_messages_from_codes(+ action, error_codes=result["authori_hold"])+ return PaymentResult(+ status=status,+ psp_reference=transaction_id,+ errors=error_messages,+ )+def cancel_transaction(config: ApiConfig, payment_information: PaymentData) -> PaymentResult:- with np_atobarai_opentracing_trace("np-atobarai.checkout.payments.cancel"):- action = TRANSACTION_CANCELLATION- psp_reference = payment_information.psp_reference+ action = TRANSACTION_CANCELLATION+ psp_reference = payment_information.psp_reference- if not psp_reference:- return error_payment_result(- add_action_to_code(action, error_code=NO_PSP_REFERENCE)- )+ if not psp_reference:+ return error_payment_result(+ add_action_to_code(action, error_code=NO_PSP_REFERENCE)+ )- result, error_codes = cancel(config, psp_reference)+ result, error_codes = cancel(config, psp_reference)- if error_codes:- error_messages = get_error_messages_from_codes(- action, error_codes=error_codes- )- return errors_payment_result(error_messages)+ if error_codes:+ error_messages = get_error_messages_from_codes(action, error_codes=error_codes)+ return errors_payment_result(error_messages)- return PaymentResult(status=PaymentStatus.SUCCESS)+ return PaymentResult(status=PaymentStatus.SUCCESS)def change_transaction(@@ -108,56 +102,53 @@ def change_transaction(If the fulfillment was reported prior to changing given transaction,then this function is a noop and return value is None."""- with np_atobarai_opentracing_trace("np-atobarai.checkout.payments.change"):- if refund_data:- goods = get_refunded_goods(config, refund_data, payment_information)- else:- goods = get_goods_with_discount(config, payment_information)-- data = {- "transactions": [- {- "np_transaction_id": payment.psp_reference,- "billed_amount": format_price(- payment.captured_amount - payment_information.amount,- payment_information.currency,- ),- "goods": goods,- }- ]- }-- result, error_codes = np_request(- config, "patch", "/transactions/update", json=data- )-- if not error_codes:- status = result["authori_result"]- transaction_id = result["np_transaction_id"]-- if status == PaymentStatus.PENDING:- if cancel_error_codes := cancel(config, transaction_id).error_codes:- handle_unrecoverable_state(- payment.order, "cancel", transaction_id, cancel_error_codes- )- error_messages = result["authori_hold"]- return errors_payment_result(error_messages)+ if refund_data:+ goods = get_refunded_goods(config, refund_data, payment_information)+ else:+ goods = get_goods_with_discount(config, payment_information)++ data = {+ "transactions": [+ {+ "np_transaction_id": payment.psp_reference,+ "billed_amount": format_price(+ payment.captured_amount - payment_information.amount,+ payment_information.currency,+ ),+ "goods": goods,+ }+ ]+ }++ result, error_codes = np_request(config, "patch", "/transactions/update", json=data)++ if not error_codes:+ status = result["authori_result"]+ transaction_id = result["np_transaction_id"]- return PaymentResult(- status=PaymentStatus.SUCCESS,- )+ if status == PaymentStatus.PENDING:+ if cancel_error_codes := cancel(config, transaction_id).error_codes:+ handle_unrecoverable_state(+ payment.order, "cancel", transaction_id, cancel_error_codes+ )+ error_messages = result["authori_hold"]+ return errors_payment_result(error_messages)- if PRE_FULFILLMENT_ERROR_CODE in error_codes:- logger.info(- "Fulfillment for payment with id %s was reported",- payment_information.graphql_payment_id,- )- return None+ return PaymentResult(+ status=PaymentStatus.SUCCESS,+ )- error_messages = get_error_messages_from_codes(- action=TRANSACTION_CHANGE, error_codes=error_codes+ if PRE_FULFILLMENT_ERROR_CODE in error_codes:+ logger.info(+ "Fulfillment for payment with id %s was reported",+ payment_information.graphql_payment_id,)- return errors_payment_result(error_messages)+ return None++ error_messages = get_error_messages_from_codes(+ action=TRANSACTION_CHANGE, error_codes=error_codes+ )+ return errors_payment_result(error_messages)def reregister_transaction_for_partial_return(@@ -173,54 +164,53 @@ def reregister_transaction_for_partial_return(Use it after capturing the payment, otherwise `change_transaction`is the preferred function."""- with np_atobarai_opentracing_trace("np-atobarai.checkout.payments.reregister"):- psp_reference = payment.psp_reference- action = TRANSACTION_REGISTRATION-- if not psp_reference:- return error_payment_result(- add_action_to_code(- action,- error_code=NO_PSP_REFERENCE,- )+ psp_reference = payment.psp_reference+ action = TRANSACTION_REGISTRATION++ if not psp_reference:+ return error_payment_result(+ add_action_to_code(+ action,+ error_code=NO_PSP_REFERENCE,)+ )- if cancel_error_codes := cancel(config, psp_reference).error_codes:- error_messages = get_error_messages_from_codes(- action=TRANSACTION_CANCELLATION, error_codes=cancel_error_codes- )- return errors_payment_result(error_messages)+ if cancel_error_codes := cancel(config, psp_reference).error_codes:+ error_messages = get_error_messages_from_codes(+ action=TRANSACTION_CANCELLATION, error_codes=cancel_error_codes+ )+ return errors_payment_result(error_messages)- if refund_data:- goods = get_refunded_goods(config, refund_data, payment_information)- else:- goods = get_goods_with_discount(config, payment_information)+ if refund_data:+ goods = get_refunded_goods(config, refund_data, payment_information)+ else:+ goods = get_goods_with_discount(config, payment_information)- billed_amount = format_price(- payment.captured_amount - payment_information.amount,- payment_information.currency,- )+ billed_amount = format_price(+ payment.captured_amount - payment_information.amount,+ payment_information.currency,+ )- result, error_codes = register(- config,- payment_information,- billed_amount,- goods,- )+ result, error_codes = register(+ config,+ payment_information,+ billed_amount,+ goods,+ )- if not error_codes:- new_psp_reference = result["np_transaction_id"]+ if not error_codes:+ new_psp_reference = result["np_transaction_id"]- report(config, shipping_company_code, new_psp_reference, tracking_number)+ report(config, shipping_company_code, new_psp_reference, tracking_number)- return PaymentResult(- status=PaymentStatus.SUCCESS,- psp_reference=new_psp_reference,- )+ return PaymentResult(+ status=PaymentStatus.SUCCESS,+ psp_reference=new_psp_reference,+ )- error_messages = get_error_messages_from_codes(action, error_codes=error_codes)+ error_messages = get_error_messages_from_codes(action, error_codes=error_codes)- return errors_payment_result(error_messages)+ return errors_payment_result(error_messages)def report_fulfillment(@@ -230,21 +220,18 @@ def report_fulfillment(After this action, given payment is captured in NP Atobarai."""- with np_atobarai_opentracing_trace(- "np-atobarai.checkout.payments.report-fulfillment"- ):- shipping_company_code = get_shipping_company_code(config, fulfillment)-- result, error_codes = report(- config,- shipping_company_code,- payment.psp_reference,- fulfillment.tracking_number,- )+ shipping_company_code = get_shipping_company_code(config, fulfillment)- errors = get_error_messages_from_codes(- action=FULFILLMENT_REPORT, error_codes=error_codes- )- already_reported = PRE_FULFILLMENT_ERROR_CODE in error_codes+ result, error_codes = report(+ config,+ shipping_company_code,+ payment.psp_reference,+ fulfillment.tracking_number,+ )++ errors = get_error_messages_from_codes(+ action=FULFILLMENT_REPORT, error_codes=error_codes+ )+ already_reported = PRE_FULFILLMENT_ERROR_CODE in error_codes- return errors, already_reported+ return errors, already_reporteddiff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.pyindex e518d3bc62..af0cde297c 100644--- a/saleor/payment/gateways/np_atobarai/api_helpers.py+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py@@ -42,7 +42,8 @@ def _request(path: str = "",json: Optional[dict] = None,) -> requests.Response:- with np_atobarai_opentracing_trace("np-atobarai.utilities.request"):+ trace_name = f"np-atobarai.request.{path.lstrip('/')}"+ with np_atobarai_opentracing_trace(trace_name):response = requests.request(method=method,url=get_url(config, path),