Skip to content

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

Fix assigment of payment method details in Stripe

Maciej Korycinski1db3918571f1
diff --git a/saleor/payment/gateways/stripe/stripe_api.py b/saleor/payment/gateways/stripe/stripe_api.py
index f859be4494..b0b5a83d55 100644
--- a/saleor/payment/gateways/stripe/stripe_api.py
+++ b/saleor/payment/gateways/stripe/stripe_api.py
@@ -291,7 +291,9 @@ def get_payment_method_details(
charges_data = charges.get("data", [])
if not charges_data:
return None
- payment_method_details = charges_data[-1]
+ charge_data = charges_data[-1]
+ payment_method_details = charge_data.get("payment_method_details", {})
+
if payment_method_details.get("type") == "card":
card_details = payment_method_details.get("card", {})
exp_year = card_details.get("exp_year", "")
diff --git a/saleor/payment/gateways/stripe/tests/test_stripe_api.py b/saleor/payment/gateways/stripe/tests/test_stripe_api.py
index 481506cb34..d23f9a88fa 100644
--- a/saleor/payment/gateways/stripe/tests/test_stripe_api.py
+++ b/saleor/payment/gateways/stripe/tests/test_stripe_api.py
@@ -479,13 +479,15 @@ def test_get_payment_method_details():
payment_intent.charges = {
"data": [
{
- "type": "card",
- "card": {
- "last4": "1234",
- "exp_year": "2222",
- "exp_month": "12",
- "brand": "visa",
- },
+ "payment_method_details": {
+ "type": "card",
+ "card": {
+ "last4": "1234",
+ "exp_year": "2222",
+ "exp_month": "12",
+ "brand": "visa",
+ },
+ }
}
]
}
diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.py
index 5d72d612b6..c5b05d5e81 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.handlers.wsgi import WSGIRequest
@@ -17,7 +17,12 @@ from ....plugins.manager import get_plugins_manager
from ... import ChargeStatus, TransactionKind
from ...interface import GatewayConfig, GatewayResponse
from ...models import Payment
-from ...utils import create_transaction, gateway_postprocess, price_from_minor_unit
+from ...utils import (
+ create_transaction,
+ gateway_postprocess,
+ price_from_minor_unit,
+ update_payment_method_details,
+)
from .consts import (
WEBHOOK_AUTHORIZED_EVENT,
WEBHOOK_CANCELED_EVENT,
@@ -26,7 +31,7 @@ from .consts import (
WEBHOOK_REFUND_EVENT,
WEBHOOK_SUCCESS_EVENT,
)
-from .stripe_api import construct_stripe_event
+from .stripe_api import construct_stripe_event, get_payment_method_details
logger = logging.getLogger(__name__)
@@ -272,6 +277,14 @@ def handle_successful_payment_intent(
extra={"payment_intent": payment_intent.id},
)
return
+
+ 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)
+
if payment.order_id:
if payment.charge_status in [ChargeStatus.PENDING, ChargeStatus.NOT_CHARGED]:
capture_transaction = _update_payment_with_new_transaction(
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index f01545135b..c456a92531 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -14,7 +14,7 @@ from ..core.tracing import traced_atomic_transaction
from ..order.models import Order
from . import ChargeStatus, GatewayError, PaymentError, TransactionKind
from .error_codes import PaymentErrorCode
-from .interface import AddressData, GatewayResponse, PaymentData
+from .interface import AddressData, GatewayResponse, PaymentData, PaymentMethodInfo
from .models import Payment, Transaction
if TYPE_CHECKING:
@@ -356,31 +356,35 @@ def update_payment(payment: "Payment", gateway_response: "GatewayResponse"):
changed_fields.append("psp_reference")
if gateway_response.payment_method_info:
- _update_payment_method_details(payment, gateway_response, changed_fields)
+ update_payment_method_details(
+ payment, gateway_response.payment_method_info, changed_fields
+ )
if changed_fields:
payment.save(update_fields=changed_fields)
-def _update_payment_method_details(
- payment: "Payment", gateway_response: "GatewayResponse", changed_fields: List[str]
+def update_payment_method_details(
+ payment: "Payment",
+ payment_method_info: Optional["PaymentMethodInfo"],
+ changed_fields: List[str],
):
- if not gateway_response.payment_method_info:
+ if not payment_method_info:
return
- if gateway_response.payment_method_info.brand:
- payment.cc_brand = gateway_response.payment_method_info.brand
+ if payment_method_info.brand:
+ payment.cc_brand = payment_method_info.brand
changed_fields.append("cc_brand")
- if gateway_response.payment_method_info.last_4:
- payment.cc_last_digits = gateway_response.payment_method_info.last_4
+ if payment_method_info.last_4:
+ payment.cc_last_digits = payment_method_info.last_4
changed_fields.append("cc_last_digits")
- if gateway_response.payment_method_info.exp_year:
- payment.cc_exp_year = gateway_response.payment_method_info.exp_year
+ if payment_method_info.exp_year:
+ payment.cc_exp_year = payment_method_info.exp_year
changed_fields.append("cc_exp_year")
- if gateway_response.payment_method_info.exp_month:
- payment.cc_exp_month = gateway_response.payment_method_info.exp_month
+ if payment_method_info.exp_month:
+ payment.cc_exp_month = payment_method_info.exp_month
changed_fields.append("cc_exp_month")
- if gateway_response.payment_method_info.type:
- payment.payment_method_type = gateway_response.payment_method_info.type
+ if payment_method_info.type:
+ payment.payment_method_type = payment_method_info.type
changed_fields.append("payment_method_type")