Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add update payment method details to authorization intent in Stripe payment gateway (#8808)

L3str4ngeefcc73d4dce7
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index e06c02d0c0..6f92d3cb9d 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -317,7 +317,7 @@ class StripeGatewayPlugin(BasePlugin):
payment_intent.status
)
- if kind == TransactionKind.CAPTURE:
+ if kind in (TransactionKind.AUTH, TransactionKind.CAPTURE):
payment_method_info = get_payment_method_details(payment_intent)
else:
diff --git a/saleor/payment/gateways/stripe/tests/conftest.py b/saleor/payment/gateways/stripe/tests/conftest.py
index b49b858e08..a516d8c0b9 100644
--- a/saleor/payment/gateways/stripe/tests/conftest.py
+++ b/saleor/payment/gateways/stripe/tests/conftest.py
@@ -116,3 +116,38 @@ def stripe_payment_intent(payment_stripe_for_checkout):
payment_intent["status"] = SUCCESS_STATUS
return payment_intent
+
+
+def stripe_payment_intent_with_details(stripe_payment_intent):
+ intent = stripe_payment_intent
+
+ intent["charges"] = {
+ "data": [
+ {
+ "payment_method_details": {
+ "card": {
+ "brand": "visa",
+ "country": "IE",
+ "exp_month": 3,
+ "exp_year": 2030,
+ "fingerprint": "ZcPGL2hemVepaHp2",
+ "funding": "credit",
+ "installments": None,
+ "last4": "3220",
+ "network": "visa",
+ "three_d_secure": {
+ "authentication_flow": "challenge",
+ "result": "authenticated",
+ "result_reason": None,
+ "version": "2.1.0",
+ },
+ "wallet": None,
+ },
+ "type": "card",
+ },
+ }
+ ]
+ }
+
+ return intent
diff --git a/saleor/payment/gateways/stripe/tests/test_plugin.py b/saleor/payment/gateways/stripe/tests/test_plugin.py
index c93ab54d7b..0e1026920a 100644
--- a/saleor/payment/gateways/stripe/tests/test_plugin.py
+++ b/saleor/payment/gateways/stripe/tests/test_plugin.py
@@ -1014,13 +1014,19 @@ def test_confirm_payment_for_webhook(kind, stripe_plugin, payment_stripe_for_che
],
)
@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.retrieve")
-def test_confirm_payment(
- mocked_intent_retrieve, kind, status, stripe_plugin, payment_stripe_for_checkout
+def test_confirm_payment_intent_without_details(
+ mocked_intent_retrieve,
+ kind,
+ status,
+ stripe_plugin,
+ payment_stripe_for_checkout,
+ stripe_payment_intent,
):
gateway_response = {
"id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS",
}
+ payment_intent = stripe_payment_intent
payment_intent_id = "payment-intent-id"
payment = payment_stripe_for_checkout
@@ -1034,7 +1040,7 @@ def test_confirm_payment(
currency=payment.currency,
)
- payment_intent = StripeObject(id=payment_intent_id)
+ payment_intent["id"] = payment_intent_id
payment_intent["amount"] = price_to_minor_unit(payment.total, payment.currency)
payment_intent["status"] = status
payment_intent["currency"] = payment.currency
@@ -1054,6 +1060,71 @@ def test_confirm_payment(
assert response.currency == payment.currency
assert response.transaction_id == payment_intent_id
assert response.error is None
+ assert not response.payment_method_info
+
+
+ "kind, status",
+ [
+ (TransactionKind.AUTH, AUTHORIZED_STATUS),
+ (TransactionKind.CAPTURE, SUCCESS_STATUS),
+ ],
+)
+@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.retrieve")
+def test_confirm_payment_intent_with_details(
+ mocked_intent_retrieve,
+ kind,
+ status,
+ stripe_plugin,
+ payment_stripe_for_checkout,
+ stripe_payment_intent_with_details,
+):
+ gateway_response = {
+ "id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS",
+ }
+
+ payment_intent = stripe_payment_intent_with_details
+ payment_intent_id = "payment-intent-id"
+
+ payment = payment_stripe_for_checkout
+ payment.transactions.create(
+ is_success=True,
+ action_required=False,
+ kind=TransactionKind.ACTION_TO_CONFIRM,
+ token=payment_intent_id,
+ gateway_response=gateway_response,
+ amount=payment.total,
+ currency=payment.currency,
+ )
+
+ payment_intent["id"] = payment_intent_id
+ payment_intent["amount"] = price_to_minor_unit(payment.total, payment.currency)
+ payment_intent["status"] = status
+ payment_intent["currency"] = payment.currency
+ mocked_intent_retrieve.return_value = payment_intent
+
+ payment_info = create_payment_information(
+ payment_stripe_for_checkout, payment_token=payment_intent_id
+ )
+
+ plugin = stripe_plugin()
+ response = plugin.confirm_payment(payment_info, None)
+
+ assert response.is_success is True
+ assert response.action_required is False
+ assert response.kind == kind
+ assert response.amount == payment.total
+ assert response.currency == payment.currency
+ assert response.transaction_id == payment_intent_id
+ assert response.error is None
+
+ payment_method_info = response.payment_method_info
+ assert payment_method_info
+ assert payment_method_info.brand == "visa"
+ assert payment_method_info.exp_month == 3
+ assert payment_method_info.exp_year == 2030
+ assert payment_method_info.last_4 == "3220"
+ assert payment_method_info.type == "card"
@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.retrieve")
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.py
index 83d91121b7..5d213f1ddd 100644
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@ -30,6 +30,7 @@ from ..webhooks import (
handle_processing_payment_intent,
handle_refund,
handle_successful_payment_intent,
+ update_payment_method_details_from_intent,
)
@@ -372,12 +373,62 @@ def test_handle_authorized_payment_intent_for_checkout(
assert wrapped_checkout_complete.called
assert payment.checkout_id is None
+ assert not payment.cc_brand
+ assert not payment.cc_last_digits
+ assert not payment.cc_exp_year
+ assert not payment.cc_exp_month
+ assert not payment.payment_method_type
assert payment.order
assert payment.order.checkout_token == str(checkout_with_items.token)
transaction = payment.transactions.get(kind=TransactionKind.AUTH)
assert transaction.token == payment_intent.id
+@patch(
+ "saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
+)
+def test_handle_authorized_payment_intent_for_checkout_with_payment_details(
+ wrapped_checkout_complete,
+ payment_stripe_for_checkout,
+ checkout_with_items,
+ stripe_plugin,
+ channel_USD,
+ stripe_payment_intent_with_details,
+):
+ intent = stripe_payment_intent_with_details
+ payment = payment_stripe_for_checkout
+ payment.to_confirm = True
+ payment.save()
+ payment.transactions.create(
+ is_success=True,
+ action_required=True,
+ kind=TransactionKind.ACTION_TO_CONFIRM,
+ amount=payment.total,
+ currency=payment.currency,
+ token="ABC",
+ gateway_response={},
+ )
+ plugin = stripe_plugin()
+ intent["amount"] = price_to_minor_unit(payment.total, payment.currency)
+ intent["currency"] = payment.currency
+ intent["status"] = AUTHORIZED_STATUS
+ handle_authorized_payment_intent(intent, plugin.config, channel_USD.slug)
+
+ payment.refresh_from_db()
+
+ assert wrapped_checkout_complete.called
+ assert payment.checkout_id is None
+ assert payment.cc_brand == "visa"
+ assert payment.cc_last_digits == "3220"
+ assert payment.cc_exp_year == 2030
+ assert payment.cc_exp_month == 3
+ assert payment.payment_method_type == "card"
+ assert payment.order
+ assert payment.order.checkout_token == str(checkout_with_items.token)
+ transaction = payment.transactions.get(kind=TransactionKind.AUTH)
+ assert transaction.token == intent.id
+
+
@patch("saleor.checkout.complete_checkout._get_order_data")
@patch("saleor.payment.gateway.void")
def test_handle_authorized_payment_intent_when_order_creation_raises_exception(
@@ -1221,3 +1272,34 @@ def test_finalize_checkout_not_created_order_payment_void(
assert not payment_stripe_for_checkout.order
assert void_mock.called
+
+
+def test_update_payment_method_details_from_intent_payment_info_does_not_exist(
+ payment_stripe_for_checkout, stripe_payment_intent
+):
+ payment = payment_stripe_for_checkout
+ update_payment_method_details_from_intent(payment, stripe_payment_intent)
+
+ payment.refresh_from_db()
+
+ assert not payment.cc_brand
+ assert not payment.cc_last_digits
+ assert not payment.cc_exp_year
+ assert not payment.cc_exp_month
+ assert not payment.payment_method_type
+
+
+def test_update_payment_method_details_from_intent_payment_info_exists(
+ payment_stripe_for_checkout, stripe_payment_intent_with_details
+):
+ intent = stripe_payment_intent_with_details
+ payment = payment_stripe_for_checkout
+ update_payment_method_details_from_intent(payment, intent)
+
+ payment.refresh_from_db()
+
+ assert payment.cc_brand == "visa"
+ assert payment.cc_last_digits == "3220"
+ assert payment.cc_exp_year == 2030
+ assert payment.cc_exp_month == 3
+ assert payment.payment_method_type == "card"
diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.py
index 83b0c7a786..3aca6697c3 100644
--- a/saleor/payment/gateways/stripe/webhooks.py
+++ b/saleor/payment/gateways/stripe/webhooks.py
@@ -1,5 +1,5 @@
import logging
-from typing import Optional
+from typing import List, Optional
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ValidationError
@@ -25,6 +25,7 @@ from ...utils import (
gateway_postprocess,
price_from_minor_unit,
update_payment_charge_status,
+ update_payment_method_details,
)
from .consts import (
WEBHOOK_AUTHORIZED_EVENT,
@@ -34,7 +35,11 @@ from .consts import (
WEBHOOK_REFUND_EVENT,
WEBHOOK_SUCCESS_EVENT,
)
-from .stripe_api import construct_stripe_event, update_payment_method
+from .stripe_api import (
+ construct_stripe_event,
+ get_payment_method_details,
+ update_payment_method,
+)
logger = logging.getLogger(__name__)
@@ -235,6 +240,16 @@ def _update_payment_method_metadata(
update_payment_method(api_key, payment_intent.payment_method, metadata)
+def update_payment_method_details_from_intent(
+ payment: Payment, payment_intent: StripeObject
+):
+ if payment_method_info := get_payment_method_details(payment_intent):
+ changed_fields: List[str] = []
+ update_payment_method_details(payment, payment_method_info, changed_fields)
+ if changed_fields:
+ payment.save(update_fields=changed_fields)
+
+
def handle_authorized_payment_intent(
payment_intent: StripeObject, gateway_config: "GatewayConfig", channel_slug: str
):
@@ -251,6 +266,7 @@ def handle_authorized_payment_intent(
return
_update_payment_method_metadata(payment, payment_intent, gateway_config)
+ update_payment_method_details_from_intent(payment, payment_intent)
if payment.order_id:
if payment.charge_status == ChargeStatus.PENDING:
@@ -346,6 +362,7 @@ def handle_successful_payment_intent(
return
_update_payment_method_metadata(payment, payment_intent, gateway_config)
+ update_payment_method_details_from_intent(payment, payment_intent)
if payment.order_id:
if payment.charge_status in [ChargeStatus.PENDING, ChargeStatus.NOT_CHARGED]: