Skip to content

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

Merge with base & expand payment_method

Mateusz Grzybf0ccec9637b1
diff --cc saleor/payment/gateways/stripe/stripe_api.py
index cd116958f3,0f2390446d..1f42870491
--- a/saleor/payment/gateways/stripe/stripe_api.py
+++ b/saleor/payment/gateways/stripe/stripe_api.py
@@@ -223,6 -216,6 +223,7 @@@ def retrieve_payment_intent
payment_intent_id,
api_key=api_key,
stripe_version=STRIPE_API_VERSION,
++ expand=["payment_method"],
)
return payment_intent, None
except StripeError as error:
@@@ -243,6 -236,6 +244,7 @@@ def capture_payment_intent
amount_to_capture=amount_to_capture,
api_key=api_key,
stripe_version=STRIPE_API_VERSION,
++ expand=["payment_method"],
)
return payment_intent, None
except StripeError as error:
diff --cc saleor/payment/gateways/stripe/tests/test_webhooks.py
index c85dabfe80,cddaee536e..5d00d680e4
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@@ -306,47 -348,79 +379,120 @@@ def test_handle_authorized_payment_inte
assert wrapped_checkout_complete.called is False
[email protected]("metadata", [{"key": "value"}, {}, None])
+@patch(
+ "saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
+)
+@patch("saleor.payment.gateways.stripe.webhooks.update_payment_method")
+def test_handle_authorized_payment_intent_with_metadata(
+ wrapped_update_payment_method,
+ _wrapped_checkout_complete,
+ payment_stripe_for_order,
+ checkout_with_items,
+ stripe_plugin,
+ channel_USD,
+ metadata,
+):
+ # given
+ payment = 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["metadata"] = current_metadata
+ payment_intent["payment_method"] = StripeObject()
+ payment_intent["charges"] = {"data": [{"payment_method_details": {"type": "card"}}]}
+ payment_intent["amount"] = payment.total
+ payment_intent["currency"] = payment.currency
+
+ # when
+ handle_authorized_payment_intent(payment_intent, plugin.config, channel_USD.slug)
+
+ # then
+ wrapped_update_payment_method.assert_called_with(
+ plugin.config.connection_params["secret_api_key"],
+ payment_intent.payment_method,
+ channel_USD.slug,
+ metadata or {},
+ )
+
+
+ @pytest.mark.parametrize("called", [True, False])
+ @patch(
+ "saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",
+ wraps=_update_payment_with_new_transaction,
+ )
+ def test_handle_authorized_payment_intent_different_order_channel_slug(
+ wrapped_update_payment_with_new_transaction,
++ channel_PLN,
+ payment_stripe_for_order,
+ checkout_with_items,
+ stripe_plugin,
+ channel_USD,
- channel_PLN,
+ called,
+ ):
+ # given
+ channel = channel_USD if called else channel_PLN
+ payment = payment_stripe_for_order
+ payment.charge_status = ChargeStatus.PENDING
+ payment.checkout = None
+ payment.save()
+ plugin = stripe_plugin()
+ payment_intent = StripeObject(id="token", last_response={})
+ payment_intent["amount"] = payment.total
+ payment_intent["currency"] = payment.currency
+ payment_intent["status"] = AUTHORIZED_STATUS
+
+ # when
+ handle_authorized_payment_intent(payment_intent, plugin.config, channel.slug)
+
+ # then
+ assert wrapped_update_payment_with_new_transaction.called == called
+
+
+ @pytest.mark.parametrize("called", [True, False])
+ @patch(
+ "saleor.payment.gateways.stripe.webhooks._process_payment_with_checkout",
+ wraps=_process_payment_with_checkout,
+ )
+ def test_handle_authorized_payment_intent_different_checkout_channel_slug(
+ wrapped_process_payment_with_checkout,
+ payment_stripe_for_checkout,
+ stripe_plugin,
+ channel_USD,
+ channel_PLN,
+ called,
+ ):
+ # given
+ channel = channel_USD if called else channel_PLN
+ 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()
+ payment_intent = StripeObject(id="ABC", last_response={})
+ payment_intent["amount"] = price_to_minor_unit(payment.total, payment.currency)
+ payment_intent["currency"] = payment.currency
+ payment_intent["status"] = AUTHORIZED_STATUS
+
+ # when
+ handle_authorized_payment_intent(payment_intent, plugin.config, channel.slug)
+
+ # then
+ assert wrapped_process_payment_with_checkout.called == called
+
+
@patch(
"saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
)
diff --cc saleor/payment/gateways/stripe/webhooks.py
index e02af99fa0,b7718b7c14..3fb4aef6a1
--- a/saleor/payment/gateways/stripe/webhooks.py
+++ b/saleor/payment/gateways/stripe/webhooks.py
@@@ -209,10 -226,9 +231,13 @@@ def handle_authorized_payment_intent
)
return
+ if _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment):
+ return
+
+ _update_payment_method_metadata(
+ payment, payment_intent, gateway_config, channel_slug
+ )
+
if payment.order_id:
if payment.charge_status == ChargeStatus.PENDING:
_update_payment_with_new_transaction(
@@@ -294,9 -319,20 +328,12 @@@ def handle_successful_payment_intent
)
return
+ if _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment):
+ return
+
- api_key = gateway_config.connection_params["secret_api_key"]
-
- if payment_intent.setup_future_usage:
- update_payment_method(api_key, payment_intent.payment_method, channel_slug)
-
- 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_method_metadata(
+ payment, payment_intent, gateway_config, channel_slug
+ )
if payment.order_id:
if payment.charge_status in [ChargeStatus.PENDING, ChargeStatus.NOT_CHARGED]:
diff --cc saleor/payment/migrations/0030_auto_20210908_1346.py
index b04fa675be,b04fa675be..77fb68bb1f
--- a/saleor/payment/migrations/0030_auto_20210908_1346.py
+++ b/saleor/payment/migrations/0030_auto_20210908_1346.py
@@@ -1,4 -1,4 +1,4 @@@
--# Generated by Django 3.2.5 on 2021-08-25 13:38
++# Generated by Django 3.2.5 on 2021-09-08 13:46
import django.contrib.postgres.indexes
from django.db import migrations, models
@@@ -35,7 -35,7 +35,7 @@@ class Migration(migrations.Migration)
),
migrations.AddField(
model_name="payment",
-- name="store",
++ name="store_payment_method",
field=models.CharField(
choices=[
("on_session", "On session"),