Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update metadata flow in webhook handlers
Mateusz Grzybfaffb683988f
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.pyindex b397a0feb8..d3d89092a0 100644--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py@@ -20,7 +20,6 @@ from ..consts import (WEBHOOK_SUCCESS_EVENT,)from ..webhooks import (- _update_payment_method_metadata_from_payment_intent,handle_authorized_payment_intent,handle_failed_payment_intent,handle_processing_payment_intent,@@ -29,29 +28,6 @@ from ..webhooks import ()- ["metadata", "payment_method"],- [- ({"key": "value"}, {"metadata": {"key": "value"}}),- ({}, {"metadata": {}}),- (None, {}),- ],-)-def test_update_payment_method_metadata_from_payment_intent(- metadata, payment_method-) -> None:- # given- payment_intent = StripeObject(id="token", last_response={})- payment_intent["metadata"] = metadata- payment_intent["payment_method"] = {}-- # when- _update_payment_method_metadata_from_payment_intent(payment_intent)-- # then- assert payment_intent.payment_method == payment_method--@patch("saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout)@@ -110,23 +86,27 @@ def test_handle_successful_payment_intent_with_metadata():# givenpayment = payment_stripe_for_order+ current_metadata = {"currentkey": "currentvalue"}+ if metadata is not None:+ payment.metadata = metadata+ payment.charge_status = ChargeStatus.PENDING+ payment.save()plugin = stripe_plugin()-payment_intent = StripeObject(id="token", last_response={})payment_intent["amount_received"] = price_to_minor_unit(payment.total, payment.currency)- payment_intent["currency"] = payment.currency- if metadata is not None:- payment_intent["metadata"] = metadata+ payment_intent["metadata"] = current_metadatapayment_intent["charges"] = {"data": [{"payment_method_details": {"type": "card"}}]}+ payment_intent["amount"] = payment.total+ payment_intent["currency"] = payment.currency# whenhandle_successful_payment_intent(payment_intent, plugin.config, channel_USD.slug)# then- payment.refresh_from_db()- assert payment.metadata == (metadata or {})+ assert payment_intent.metadata == current_metadata | (metadata or {})+ assert payment_intent.payment_method.metadata == (metadata or {})@patch(@@ -304,19 +284,24 @@ def test_handle_authorized_payment_intent_with_metadata():# givenpayment = payment_stripe_for_order+ current_metadata = {"currentkey": "currentvalue"}+ if metadata is not None:+ payment.metadata = metadatapayment.charge_status = ChargeStatus.PENDING+ payment.save()plugin = stripe_plugin()payment_intent = StripeObject(id="token", last_response={})- if metadata is not None:- payment_intent["metadata"] = metadata+ payment_intent["metadata"] = current_metadatapayment_intent["charges"] = {"data": [{"payment_method_details": {"type": "card"}}]}+ payment_intent["amount"] = payment.total+ payment_intent["currency"] = payment.currency# whenhandle_authorized_payment_intent(payment_intent, plugin.config, channel_USD.slug)# then- payment.refresh_from_db()- assert payment.metadata == (metadata or {})+ assert payment_intent.metadata == current_metadata | (metadata or {})+ assert payment_intent.payment_method.metadata == (metadata or {})@patch(diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.pyindex f5595daeb0..1342b5ef9c 100644--- a/saleor/payment/gateways/stripe/webhooks.py+++ b/saleor/payment/gateways/stripe/webhooks.py@@ -1,5 +1,5 @@import logging-from typing import List, Optional+from typing import Optionalfrom django.contrib.auth.models import AnonymousUserfrom django.core.handlers.wsgi import WSGIRequest@@ -17,12 +17,7 @@ from ....plugins.manager import get_plugins_managerfrom ... import ChargeStatus, TransactionKindfrom ...interface import GatewayConfig, GatewayResponsefrom ...models import Payment-from ...utils import (- create_transaction,- gateway_postprocess,- price_from_minor_unit,- update_payment_method_details,-)+from ...utils import create_transaction, gateway_postprocess, price_from_minor_unitfrom .consts import (WEBHOOK_AUTHORIZED_EVENT,WEBHOOK_CANCELED_EVENT,@@ -188,16 +183,26 @@ def _process_payment_with_checkout(_finalize_checkout(checkout, payment, payment_intent, kind, amount, currency)-def _update_payment_method_metadata_from_payment_intent(+def _update_payment_intent_metadata_from_database(+ payment: Payment,payment_intent: StripeObject,) -> None:+ metadata = payment.metadata+ if metadata is None:+ return++ if payment_intent.get("metadata") is not None:+ payment_intent.metadata |= metadata+ else:+ payment_intent.metadata = metadata+payment_method = payment_intent.get("payment_method")- metadata = payment_intent.get("metadata")- if metadata is not None:- if payment_method is not None:- payment_method["metadata"] = metadata- else:- payment_intent["payment_method"] = {"metadata": metadata}++ if payment_method is not None:+ payment_method.metadata = metadata+ else:+ payment_intent.payment_method = StripeObject()+ payment_intent.payment_method.metadata = metadatadef handle_authorized_payment_intent(@@ -212,7 +217,7 @@ def handle_authorized_payment_intent()return- _update_payment_method_metadata_from_payment_intent(payment_intent)+ _update_payment_intent_metadata_from_database(payment, payment_intent)payment_method_info = get_payment_method_details(payment_intent)if payment_method_info and payment_method_info.metadata:@@ -300,14 +305,7 @@ def handle_successful_payment_intent()return- _update_payment_method_metadata_from_payment_intent(payment_intent)-- payment_method_info = get_payment_method_details(payment_intent)- if payment_method_info:- changed_fields: List[str] = []- update_payment_method_details(payment, payment_method_info, changed_fields)- if changed_fields:- payment.save(update_fields=changed_fields)+ _update_payment_intent_metadata_from_database(payment, payment_intent)if payment.order_id:if payment.charge_status in [ChargeStatus.PENDING, ChargeStatus.NOT_CHARGED]: