Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add docstring to main part of Adyen methods (#8845) (#8852)
Marcin Gębala591ef5877739
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.pyindex 9a6f04098d..20be86c7b8 100644--- a/saleor/payment/gateways/adyen/plugin.py+++ b/saleor/payment/gateways/adyen/plugin.py@@ -248,6 +248,17 @@ class AdyenGatewayPlugin(BasePlugin):return urljoin(base_url, "webhooks") # type: ignoredef webhook(self, request: WSGIRequest, path: str, previous_value) -> HttpResponse:+ """Handle a request received from Adyen.++ The method handles two types of requests:+ - webhook notification received from Adyen. It is required to properly+ update the current status of payment and order based on the type of+ received notification.+ - additional actions, called when a user is redirected to an external page+ and after processing a payment is redirecting back to the storefront page.+ The redirect request comes through the Saleor which calls Adyen API to+ validate the current status of payment.+ """config = self._get_gateway_config()if path.startswith(WEBHOOK_PATH):return handle_webhook(request, config)@@ -276,6 +287,11 @@ class AdyenGatewayPlugin(BasePlugin):def initialize_payment(self, payment_data, previous_value) -> "InitializedPaymentResponse":+ """Initialize a payment for ApplePay.++ ApplePay requires an additional action that initializes a payment action. It is+ done by a separate mutation which calls this method.+ """payment_method = payment_data.get("paymentMethod")if payment_method == "applepay":# The apple pay on the web requires additional step@@ -291,6 +307,16 @@ class AdyenGatewayPlugin(BasePlugin):def get_payment_gateways(self, currency: Optional[str], checkout: Optional["Checkout"], previous_value) -> List["PaymentGateway"]:+ """Fetch current configuration for given checkout.++ It calls an Adyen API to fetch all available payment methods for given checkout.+ Adyen defines available payment methods based on the data that we send like+ amount, currency, and country. Some payment methods are only available if the+ given data matches their conditions. Like to display payment method X, which is+ available in UK, we need to set GBP as currency, and country-code needs to+ point to UK. We don't fetch anything if checkout is none, as we don't have+ enough info to provide the required data in the request.+ """local_config = self._get_gateway_config()config = [{@@ -325,6 +351,11 @@ class AdyenGatewayPlugin(BasePlugin):@require_active_plugindef check_payment_balance(self, data: dict, previous_value=None) -> dict:+ """Check current payment balance.++ For Adyen, we use it only for checking the balance of the gift cards. It builds+ a request based on the input and send a request to Adyen's API.+ """request_data = get_request_data_for_check_payment(data, self.config.connection_params["merchant_account"])@@ -355,6 +386,19 @@ class AdyenGatewayPlugin(BasePlugin):def process_payment(self, payment_information: "PaymentData", previous_value) -> "GatewayResponse":+ """Process a payment on Adyen's side.++ This method is called when payment.to_confirm is set to False.+ It builds a request data required for a given payment method and sends the data+ to Adyen's side.+ If Adyen doesn't return any additional action required and the result code is+ a success, the payment is finalized with success. If auto_capture is set to+ True, and the payment status is AUTH, it will immediately call capture.+ If Adyen returns an additional action to process by customer, the payment is+ not finished yet. In that case, in the response from the method, we set+ action_required and add to action_required_data all Adyen's data required to+ finalize payment by the customer.+ """try:payment = Payment.objects.get(pk=payment_information.payment_id)except ObjectDoesNotExist:@@ -516,6 +560,22 @@ class AdyenGatewayPlugin(BasePlugin):def confirm_payment(self, payment_information: "PaymentData", previous_value) -> "GatewayResponse":+ """Confirm a payment on Adyen side.++ In case when we have a transaction with `ACTION_TO_CONFIRM`, we just need to+ finalize the payment process on our side. Transaction ACTION_TO_CONFIRM is+ created only when we receive a webhook with a notification that the payment+ process has been finished with success.+ In case when we can't find an ACTION_TO_CONFIRM transaction, we call logic+ responsible for confirming additional data received as an input. The data+ comes from the storefront when the customer finishes an additional action,+ which was requested in process_payment call. We still check the value of the+ action field in the Adyen's response, as there is a possibility that the given+ payment method will require more additional actions. In that case, we will+ return action_required set to True and action_required_data fulfilled with+ action data received from Adyen and required for the next additional action on+ the customer side.+ """config = self._get_gateway_config()# The additional checks are proceed asynchronously so we try to confirm that# the payment is already processeddiff --git a/saleor/payment/gateways/adyen/utils/common.py b/saleor/payment/gateways/adyen/utils/common.pyindex 3c15e73f60..865fb4fc98 100644--- a/saleor/payment/gateways/adyen/utils/common.py+++ b/saleor/payment/gateways/adyen/utils/common.py@@ -180,6 +180,9 @@ def request_data_for_payment(# klarna in method - because there is a lot of variable klarna methods - like pay# later with klarna or pay with klarna etcif "klarna" in method or method in methods_that_require_checkout_details:+ # Some payment methods like afterpay or klarna, requires more context for+ # processing a payment. If user pick up such payment method we add more+ # checkout details to requestrequest_data = append_checkout_details(payment_information, request_data)return request_datadiff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.pyindex f2d5e9f877..7f0916d92c 100644--- a/saleor/payment/gateways/adyen/webhooks.py+++ b/saleor/payment/gateways/adyen/webhooks.py@@ -213,6 +213,22 @@ def handle_not_created_order(notification, payment, checkout, kind, manager):def handle_authorization(notification: Dict[str, Any], gateway_config: GatewayConfig):+ """Handle authorization notification.++ Handler for processing an authorization notification from Adyen. The notification+ is async so we assume that it can be delivered in two different situations: order+ is already created, order is not created yet.+ - order is already created: we process notification and update the status of+ payment and order in case if we didn't do that previously.+ - order is not created yet: we create an order and update a payment's status.+ In case when checkout_complete raises an exception we will call a refund/void+ for a given payment.+ For both cases we will include an external event to Order history.+ No matter if Adyen has enabled auto capture on their side, we will always receive+ authorization notification. In that case, we can't determine if the payment on+ their side goes to authorization or captured status.+ """+transaction_id = notification.get("pspReference")payment = get_payment(notification.get("merchantReference"), transaction_id)if not payment:@@ -687,6 +703,14 @@ def handle_webhook(request: WSGIRequest, gateway_config: "GatewayConfig"):def handle_additional_actions(request: WSGIRequest, payment_details: Callable, channel_slug: str):+ """Handle redirect with additional actions.++ When a customer uses a payment method with redirect, before customer is redirected+ back to storefront, the request goes through the Saleor. We use the data received+ from Adyen, as a query params or as a post data, to finalize an additional action.+ After that, if payment doesn't require any additional action we create an order.+ In case if action data exists, we don't create an order and we include them in url.+ """payment_id = request.GET.get("payment")checkout_pk = request.GET.get("checkout")