Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix optional sku in NP payload & add docstrings
Mateusz Grzybde23e02ed679
diff --git a/saleor/payment/gateways/np_atobarai/__init__.py b/saleor/payment/gateways/np_atobarai/__init__.pyindex 33b07ae79d..3a8cbdb33a 100644--- a/saleor/payment/gateways/np_atobarai/__init__.py+++ b/saleor/payment/gateways/np_atobarai/__init__.py@@ -30,6 +30,13 @@ def parse_errors(errors: List[str]) -> str:def process_payment(payment_information: PaymentData, config: ApiConfig) -> GatewayResponse:+ """Create new transaction in NP Atobarai.++ Returns unsuccessful response if payment status from+ NP response is PENDING or FAILED.++ :raises PaymentError: When the specified payment does not exist.+ """payment_id = payment_information.payment_idpayment = Payment.objects.filter(pk=payment_id).first()@@ -64,6 +71,10 @@ def void(payment_information: PaymentData, config: ApiConfig) -> GatewayResponsedef tracking_number_updated(fulfillment: Fulfillment, config: ApiConfig) -> None:+ """Event callback on updating order tracking number.++ Captures payment in NP Atobarai and sends dashboard notification of the result.+ """order = fulfillment.orderpayments = order.payments.filter(gateway=NP_PLUGIN_ID, is_active=True)@@ -91,6 +102,11 @@ def tracking_number_updated(fulfillment: Fulfillment, config: ApiConfig) -> Nonedef refund(payment_information: PaymentData, config: ApiConfig) -> GatewayResponse:+ """Refund an existing transaction.++ A new psp reference may be issued,+ which will be saved in payment object.+ """payment_id = payment_information.payment_idpayment = Payment.objects.filter(pk=payment_id).first()@@ -127,7 +143,6 @@ def refund(payment_information: PaymentData, config: ApiConfig) -> GatewayResponelse:result = api.cancel_transaction(config, payment_information)- # NP may issue a new psp reference on partial refundsif psp_reference := result.psp_reference:payment.psp_reference = psp_referencepayment.save(update_fields=["psp_reference"])diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.pyindex 6ff72ca605..9b7825d9ea 100644--- a/saleor/payment/gateways/np_atobarai/api.py+++ b/saleor/payment/gateways/np_atobarai/api.py@@ -112,7 +112,7 @@ def change_transaction(payment_information: PaymentData,refund_data: Optional[Dict[int, int]],) -> Optional[PaymentResult]:- """Partial refund.+ """Change transaction.If the fulfillment was reported prior to changing given transaction,then this function is a noop and return value is None.@@ -177,7 +177,11 @@ def reregister_transaction_for_partial_return(tracking_number: Optional[str],refund_data: Optional[Dict[int, int]],) -> PaymentResult:- """Partial refund after fulfillment report."""+ """Change transaction.++ 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_referenceaction = TRANSACTION_REGISTRATION@@ -231,6 +235,10 @@ def reregister_transaction_for_partial_return(def report_fulfillment(config: ApiConfig, payment: Payment, fulfillment: Fulfillment) -> Tuple[Union[str, int], List[str], bool]:+ """Report fulfillment.++ After this action, given payment is captured in NP Atobarai.+ """with np_atobarai_opentracing_trace("np-atobarai.checkout.payments.report-fulfillment"):diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.pyindex 480cb3486f..25ef6d3983 100644--- a/saleor/payment/gateways/np_atobarai/api_helpers.py+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py@@ -8,7 +8,7 @@ from posuto import Posutofrom requests.auth import HTTPBasicAuthfrom ....order.models import Order-from ...interface import AddressData, PaymentData+from ...interface import AddressData, PaymentData, PaymentLineDatafrom ...utils import price_to_minor_unitfrom .api_types import NPResponse, error_np_responsefrom .const import NP_ATOBARAI, REQUEST_TIMEOUT@@ -120,6 +120,15 @@ def format_price(price: Decimal, currency: str) -> int:return int(price_to_minor_unit(price, currency))+def _get_goods_name(line: PaymentLineData, config: "ApiConfig") -> str:+ if not config.sku_as_name:+ return line.product_name+ elif sku := line.product_sku:+ return sku+ else:+ return str(line.variant_id)++def get_refunded_goods(config: "ApiConfig",refund_data: Dict[int, int],@@ -127,7 +136,7 @@ def get_refunded_goods() -> List[dict]:return [{- "goods_name": line.product_sku if config.sku_as_name else line.product_name,+ "goods_name": _get_goods_name(line, config),"goods_price": format_price(line.gross, payment_information.currency),"quantity": quantity,}@@ -140,7 +149,7 @@ def get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dicreturn [{"quantity": line.quantity,- "goods_name": line.product_sku if config.sku_as_name else line.product_name,+ "goods_name": _get_goods_name(line, config),"goods_price": format_price(line.gross, payment_information.currency),}for line in payment_information.lines