Skip to content

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

Fill card info in payment

Jakub Draganek09b1bdd0ecad
diff --git a/saleor/payment/gateways/stripe_new/__init__.py b/saleor/payment/gateways/stripe_new/__init__.py
index 9dbfcb74b5..f2473d8e4f 100644
--- a/saleor/payment/gateways/stripe_new/__init__.py
+++ b/saleor/payment/gateways/stripe_new/__init__.py
@@ -101,6 +101,7 @@ def confirm(payment_information: PaymentData, config: GatewayConfig) -> GatewayR
kind=TransactionKind.CONFIRM,
success=intent.status == "succeeded",
)
+ response = fill_card_details(intent, response)
except stripe.error.StripeError as exc:
response = _error_response(
kind=TransactionKind.CONFIRM, exc=exc, payment_info=payment_information
@@ -231,6 +232,10 @@ def _success_response(
def fill_card_details(intent: stripe.PaymentIntent, response: GatewayResponse):
+ charges = intent.charges["data"]
+ if not charges:
+ return response
+
card = intent.charges["data"][-1]["payment_method_details"]["card"]
response.card_info = CreditCardInfo(
last_4=card["last4"],
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 4fa867f0da..139b787cd0 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -346,6 +346,8 @@ def call_gateway(operation_type, payment, payment_token, **extra_params):
# Attempt to get errors from response, if none raise a generic one
raise PaymentError(payment_transaction.error or GENERIC_TRANSACTION_ERROR)
+ if gateway_response.card_info:
+ update_card_details(payment, gateway_response)
return payment_transaction
@@ -401,15 +403,6 @@ def _gateway_postprocess(transaction, payment):
payment.is_active = False
changed_fields += ["charge_status", "is_active"]
payment.save(update_fields=changed_fields)
- elif transaction_kind == TransactionKind.CONFIRM:
- payment.captured_amount += transaction.amount
- payment.charge_status = ChargeStatus.PARTIALLY_CHARGED
- if payment.get_charge_amount() <= 0:
- payment.charge_status = ChargeStatus.FULLY_CHARGED
- payment.save(update_fields=["charge_status", "captured_amount"])
- order = payment.order
- if order and order.is_fully_paid():
- handle_fully_paid_order(order)
@require_active_payment
@@ -575,3 +568,13 @@ def retrieve_customer_sources(gateway_name, customer_id):
"""Fetch all customer payment sources stored in gateway."""
gateway, config = get_payment_gateway(gateway_name)
return gateway.list_client_sources(config, customer_id)
+
+
+def update_card_details(payment, gateway_response):
+ payment.cc_brand = gateway_response.card_info.brand or ""
+ payment.cc_last_digits = gateway_response.card_info.last_4
+ payment.cc_exp_year = gateway_response.card_info.exp_year
+ payment.cc_exp_month = gateway_response.card_info.exp_month
+ payment.save(
+ update_fields=["cc_brand", "cc_last_digits", "cc_exp_year", "cc_exp_month"]
+ )
diff --git a/tests/gateway/stripe/test_stripe.py b/tests/gateway/stripe/test_stripe.py
index d767dbe3fe..6008f91971 100644
--- a/tests/gateway/stripe/test_stripe.py
+++ b/tests/gateway/stripe/test_stripe.py
@@ -27,7 +27,7 @@ CARD_SIMPLE_DETAILS = CreditCardInfo(
PAYMENT_METHOD_CARD_3D_SECURE = "pm_card_threeDSecure2Required"
# Set to True if recording new cassette with sandbox using credentials in env
-RECORD = True
+RECORD = False
@pytest.fixture()
diff --git a/tests/test_payment.py b/tests/test_payment.py
index d5ccedfdba..68de13090b 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -13,7 +13,12 @@ from saleor.payment import (
TransactionKind,
get_payment_gateway,
)
-from saleor.payment.interface import GatewayConfig, GatewayResponse, TokenConfig
+from saleor.payment.interface import (
+ GatewayConfig,
+ GatewayResponse,
+ TokenConfig,
+ CreditCardInfo,
+)
from saleor.payment.models import Payment
from saleor.payment.utils import (
ALLOWED_GATEWAY_KINDS,
@@ -41,7 +46,14 @@ EXAMPLE_ERROR = "Example dummy error"
@pytest.fixture
-def gateway_response(settings):
+def card_details():
+ return CreditCardInfo(
+ last_4="1234", exp_year=2020, exp_month=8, brand="visa", name_on_card="Joe Doe"
+ )
+
+
+def gateway_response(settings, card_details):
return GatewayResponse(
is_success=True,
action_required=False,
@@ -54,6 +66,7 @@ def gateway_response(settings):
"credit_card_four": "1234",
"transaction-id": "transaction-token",
},
+ card_info=card_details,
)
@@ -85,7 +98,7 @@ def transaction_token():
@pytest.fixture
-def dummy_response(payment_dummy, transaction_data, transaction_token):
+def dummy_response(payment_dummy, transaction_data, transaction_token, card_details):
return GatewayResponse(
is_success=True,
action_required=False,
@@ -95,6 +108,7 @@ def dummy_response(payment_dummy, transaction_data, transaction_token):
currency=payment_dummy.currency,
kind=TransactionKind.AUTH,
raw_response=None,
+ card_info=card_details,
)
@@ -360,6 +374,10 @@ def test_gateway_capture(
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
assert payment.captured_amount == payment.total
+ assert payment.cc_brand == dummy_response.card_info.brand
+ assert payment.cc_exp_month == dummy_response.card_info.exp_month
+ assert payment.cc_exp_year == dummy_response.card_info.exp_year
+ assert payment.cc_last_digits == dummy_response.card_info.last_4
mock_handle_fully_paid_order.assert_called_once_with(payment.order)