Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for process_payment
Mateusz Grzyb7b3fbb77c331
diff --git a/saleor/graphql/payment/tests/test_payment.py b/saleor/graphql/payment/tests/test_payment.pyindex 3d16ecf81f..3907d0e82c 100644--- a/saleor/graphql/payment/tests/test_payment.py+++ b/saleor/graphql/payment/tests/test_payment.py@@ -481,7 +481,7 @@ def test_create_payment_with_store(# thencheckout.refresh_from_db()payment = checkout.payments.first()- assert payment.store == store.lower()+ assert payment.store_payment_method == store.lower()@pytest.mark.parametrize(diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.pyindex 40fe37b3ed..1f1ec509cf 100644--- a/saleor/payment/gateways/stripe/plugin.py+++ b/saleor/payment/gateways/stripe/plugin.py@@ -178,20 +178,18 @@ class StripeGatewayPlugin(BasePlugin):payment_method_id = data.get("payment_method_id") if data else Nonesetup_future_usage = None+ # DEPRECATEDif payment_information.reuse_source:setup_future_usage = data.get("setup_future_usage") if data else None+ off_session = data.get("off_session") if data else None+payment_method_types = data.get("payment_method_types") if data else None- # DEPRECATED- if setup_future_usage:- off_session = data.get("off_session") if data else None- else:- store_payment_method = payment_information.store_payment_method- if store_payment_method == StorePaymentMethodEnum.NONE:- off_session = None- else:- off_session = store_payment_method == StorePaymentMethodEnum.OFF_SESSION+ if not setup_future_usage:+ store = payment_information.store_payment_method+ if store != StorePaymentMethodEnum.NONE:+ setup_future_usage = store.lower()customer = None# confirm that we creates customer on stripe side only for log-in customersdiff --git a/saleor/payment/gateways/stripe/tests/test_plugin.py b/saleor/payment/gateways/stripe/tests/test_plugin.pyindex ef1191ba7a..77943c690e 100644--- a/saleor/payment/gateways/stripe/tests/test_plugin.py+++ b/saleor/payment/gateways/stripe/tests/test_plugin.py@@ -8,7 +8,7 @@ from stripe.stripe_object import StripeObjectfrom .....plugins.models import PluginConfigurationfrom .... import TransactionKind-from ....interface import GatewayResponse+from ....interface import GatewayResponse, StorePaymentMethodEnumfrom ....utils import (create_payment_information,create_transaction,@@ -291,7 +291,7 @@ def test_process_payment_with_customer(@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")-def test_process_payment_with_customer_and_future_usage(+def test_process_payment_with_customer_and_deprecated_future_usage(mocked_payment_intent,mocked_customer_create,stripe_plugin,@@ -364,6 +364,160 @@ def test_process_payment_with_customer_and_future_usage()+ "store_payment_method",+ [StorePaymentMethodEnum.OFF_SESSION, StorePaymentMethodEnum.ON_SESSION],+)+@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")+@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")+def test_process_payment_with_customer_and_new_future_usage(+ mocked_payment_intent,+ mocked_customer_create,+ stripe_plugin,+ payment_stripe_for_checkout,+ channel_USD,+ customer_user,+ store_payment_method,+):+ customer = Mock()+ mocked_customer_create.return_value = customer++ payment_intent = Mock()+ mocked_payment_intent.return_value = payment_intent++ client_secret = "client-secret"+ dummy_response = {+ "id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS",+ }+ payment_intent_id = "payment-intent-id"+ payment_intent.id = payment_intent_id+ payment_intent.client_secret = client_secret+ payment_intent.last_response.data = dummy_response+ payment_intent.status = SUCCESS_STATUS++ plugin = stripe_plugin(auto_capture=True)++ payment_stripe_for_checkout.checkout.user = customer_user+ payment_stripe_for_checkout.checkout.email = customer_user.email+ payment_stripe_for_checkout.store_payment_method = store_payment_method++ payment_info = create_payment_information(+ payment_stripe_for_checkout,+ customer_id=None,+ )++ response = plugin.process_payment(payment_info, None)++ assert response.is_success is True+ assert response.action_required is False+ assert response.kind == TransactionKind.CAPTURE+ assert response.amount == payment_info.amount+ assert response.currency == payment_info.currency+ assert response.transaction_id == payment_intent_id+ assert response.error is None+ assert response.raw_response == dummy_response+ assert response.action_required_data == {+ "client_secret": client_secret,+ "id": payment_intent_id,+ }++ api_key = plugin.config.connection_params["secret_api_key"]+ mocked_payment_intent.assert_called_once_with(+ api_key=api_key,+ amount=price_to_minor_unit(payment_info.amount, payment_info.currency),+ currency=payment_info.currency,+ capture_method=AUTOMATIC_CAPTURE_METHOD,+ customer=customer,+ setup_future_usage=store_payment_method.lower(),+ metadata={+ "channel": channel_USD.slug,+ "payment_id": payment_info.graphql_payment_id,+ },+ receipt_email=payment_stripe_for_checkout.checkout.email,+ stripe_version=STRIPE_API_VERSION,+ )++ mocked_customer_create.assert_called_once_with(+ api_key="secret_key",+ email=customer_user.email,+ stripe_version=STRIPE_API_VERSION,+ )+++@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")+@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")+def test_process_payment_with_customer_and_new_future_usage_no_store(+ mocked_payment_intent,+ mocked_customer_create,+ stripe_plugin,+ payment_stripe_for_checkout,+ channel_USD,+ customer_user,+):+ customer = Mock()+ mocked_customer_create.return_value = customer++ payment_intent = Mock()+ mocked_payment_intent.return_value = payment_intent++ client_secret = "client-secret"+ dummy_response = {+ "id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS",+ }+ payment_intent_id = "payment-intent-id"+ payment_intent.id = payment_intent_id+ payment_intent.client_secret = client_secret+ payment_intent.last_response.data = dummy_response+ payment_intent.status = SUCCESS_STATUS++ plugin = stripe_plugin(auto_capture=True)++ payment_stripe_for_checkout.checkout.user = customer_user+ payment_stripe_for_checkout.checkout.email = customer_user.email+ payment_stripe_for_checkout.store_payment_method = StorePaymentMethodEnum.NONE++ payment_info = create_payment_information(+ payment_stripe_for_checkout,+ customer_id=None,+ )++ response = plugin.process_payment(payment_info, None)++ assert response.is_success is True+ assert response.action_required is False+ assert response.kind == TransactionKind.CAPTURE+ assert response.amount == payment_info.amount+ assert response.currency == payment_info.currency+ assert response.transaction_id == payment_intent_id+ assert response.error is None+ assert response.raw_response == dummy_response+ assert response.action_required_data == {+ "client_secret": client_secret,+ "id": payment_intent_id,+ }++ api_key = plugin.config.connection_params["secret_api_key"]+ mocked_payment_intent.assert_called_once_with(+ api_key=api_key,+ amount=price_to_minor_unit(payment_info.amount, payment_info.currency),+ currency=payment_info.currency,+ capture_method=AUTOMATIC_CAPTURE_METHOD,+ customer=customer,+ metadata={+ "channel": channel_USD.slug,+ "payment_id": payment_info.graphql_payment_id,+ },+ receipt_email=payment_stripe_for_checkout.checkout.email,+ stripe_version=STRIPE_API_VERSION,+ )++ mocked_customer_create.assert_called_once_with(+ api_key="secret_key",+ email=customer_user.email,+ stripe_version=STRIPE_API_VERSION,+ )++@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")def test_process_payment_with_customer_and_payment_method(@@ -576,11 +730,8 @@ def test_process_payment_offline(capture_method=AUTOMATIC_CAPTURE_METHOD,customer=customer,payment_method="pm_ID",- # todo: was- # confirm=True,- # off_session=True,- # todo: is- off_session=False,+ confirm=True,+ off_session=True,metadata={"channel": channel_USD.slug,"payment_id": payment_info.graphql_payment_id,@@ -660,11 +811,8 @@ def test_process_payment_with_customer_and_payment_method_raises_card_error(capture_method=AUTOMATIC_CAPTURE_METHOD,customer=customer,payment_method="pm_ID",- # todo: was- # confirm=True,- # off_session=True,- # todo: is- off_session=False,+ confirm=True,+ off_session=True,metadata={"channel": channel_USD.slug,"payment_id": payment_info.graphql_payment_id,diff --git a/saleor/payment/migrations/0031_rename_store_payment_store_payment_method.py b/saleor/payment/migrations/0031_rename_store_payment_store_payment_method.pynew file mode 100644index 0000000000..b5b1b882e6--- /dev/null+++ b/saleor/payment/migrations/0031_rename_store_payment_store_payment_method.py@@ -0,0 +1,18 @@+# Generated by Django 3.2.5 on 2021-09-02 11:54++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("payment", "0030_auto_20210825_1338"),+ ]++ operations = [+ migrations.RenameField(+ model_name="payment",+ old_name="store",+ new_name="store_payment_method",+ ),+ ]diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 4689fd7ad7..67da599222 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -60,7 +60,7 @@ class Payment(ModelWithMetadata):order = models.ForeignKey("order.Order", null=True, related_name="payments", on_delete=models.PROTECT)- store = models.CharField(+ store_payment_method = models.CharField(max_length=11,choices=StorePaymentMethod.CHOICES,default=StorePaymentMethod.NONE,diff --git a/saleor/payment/tests/test_payment.py b/saleor/payment/tests/test_payment.pyindex a3dec267a5..bd397011ae 100644--- a/saleor/payment/tests/test_payment.py+++ b/saleor/payment/tests/test_payment.py@@ -120,7 +120,7 @@ def test_create_payment(checkout_with_item, address):}payment = create_payment(**data)assert payment.gateway == "Dummy"- assert payment.store == StorePaymentMethod.NONE+ assert payment.store_payment_method == StorePaymentMethod.NONEsame_payment = create_payment(**data)assert payment == same_paymentdiff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 3d2eecd9d6..31905a790c 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -92,7 +92,9 @@ def create_payment_information(reuse_source=store_source,data=additional_data or {},graphql_customer_id=graphql_customer_id,- store_payment_method=StorePaymentMethodEnum[payment.store.upper()],+ store_payment_method=StorePaymentMethodEnum[+ payment.store_payment_method.upper()+ ],payment_metadata=payment.metadata,)@@ -159,7 +161,7 @@ def create_payment("total": total,"return_url": return_url,"psp_reference": external_reference or "",- "store": store_payment_method,+ "store_payment_method": store_payment_method,"metadata": {} if metadata is None else metadata,}