Skip to content

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

Add flag to StripePlugin to control if receipt_email should be included in the request (#12790)

Maciej Korycinski18430f2a7a16
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index adc7d4c03a..16982f1dc5 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -62,6 +62,7 @@ class StripeGatewayPlugin(BasePlugin):
{"name": "supported_currencies", "value": ""},
{"name": "webhook_endpoint_id", "value": None},
{"name": "webhook_secret_key", "value": None},
+ {"name": "include_receipt_email", "value": True},
]
CONFIG_STRUCTURE = {
@@ -86,6 +87,16 @@ class StripeGatewayPlugin(BasePlugin):
" Please enter currency codes separated by a comma.",
"label": "Supported currencies",
},
+ "include_receipt_email": {
+ "type": ConfigurationTypeField.BOOLEAN,
+ "help_text": "Determine whether the `receipt_email` should be included in "
+ "the payment request sent to Stripe. If `receipt_email` is specified for a "
+ "payment in live mode, a receipt will be sent by Stripe, regardless of "
+ "your email settings. More info: "
+ "https://stripe.com/docs/api/payment_intents/create#"
+ "create_payment_intent-receipt_email",
+ "label": "Include receipt email",
+ },
"webhook_endpoint_id": {
"type": ConfigurationTypeField.OUTPUT,
"help_text": "Unique identifier for the webhook endpoint object.",
@@ -110,6 +121,7 @@ class StripeGatewayPlugin(BasePlugin):
"secret_api_key": configuration["secret_api_key"],
"webhook_id": configuration["webhook_endpoint_id"],
"webhook_secret": webhook_secret,
+ "include_receipt_email": configuration["include_receipt_email"],
},
store_customer=True,
)
@@ -179,6 +191,7 @@ class StripeGatewayPlugin(BasePlugin):
return previous_value
api_key = self.config.connection_params["secret_api_key"]
+ include_receipt_email = self.config.connection_params["include_receipt_email"]
auto_capture = self.config.auto_capture
if self.order_auto_confirmation is False:
@@ -227,7 +240,9 @@ class StripeGatewayPlugin(BasePlugin):
setup_future_usage=setup_future_usage,
off_session=off_session,
payment_method_types=payment_method_types,
- customer_email=payment_information.customer_email,
+ customer_email=payment_information.customer_email
+ if include_receipt_email
+ else None,
)
raw_response = None
diff --git a/saleor/payment/gateways/stripe/tests/conftest.py b/saleor/payment/gateways/stripe/tests/conftest.py
index 232bb9f361..a323069d43 100644
--- a/saleor/payment/gateways/stripe/tests/conftest.py
+++ b/saleor/payment/gateways/stripe/tests/conftest.py
@@ -75,6 +75,7 @@ def stripe_plugin(settings, monkeypatch, channel_USD):
webhook_secret_key="ABCD",
active=True,
auto_capture=True,
+ include_receipt_email=None,
):
public_api_key = public_api_key or "test_key"
secret_api_key = secret_api_key or "secret_key"
@@ -95,6 +96,10 @@ def stripe_plugin(settings, monkeypatch, channel_USD):
configuration.append(
{"name": "webhook_secret_key", "value": webhook_secret_key}
)
+ if include_receipt_email is not None:
+ configuration.append(
+ {"name": "include_receipt_email", "value": include_receipt_email}
+ )
PluginConfiguration.objects.create(
identifier=StripeGatewayPlugin.PLUGIN_ID,
name=StripeGatewayPlugin.PLUGIN_NAME,
diff --git a/saleor/payment/gateways/stripe/tests/test_plugin.py b/saleor/payment/gateways/stripe/tests/test_plugin.py
index 9c8df3c89e..726a40d56e 100644
--- a/saleor/payment/gateways/stripe/tests/test_plugin.py
+++ b/saleor/payment/gateways/stripe/tests/test_plugin.py
@@ -214,6 +214,125 @@ def test_process_payment(
assert not mocked_customer.called
+@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_disabled_include_receipt_email(
+ mocked_payment_intent,
+ mocked_customer,
+ stripe_plugin,
+ payment_stripe_for_checkout,
+ channel_USD,
+):
+ # given
+ 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 = "requires_payment_method"
+
+ plugin = stripe_plugin(auto_capture=True, include_receipt_email=False)
+
+ payment_info = create_payment_information(
+ payment_stripe_for_checkout,
+ )
+
+ # when
+ response = plugin.process_payment(payment_info, None)
+
+ # then
+ assert response.is_success is True
+ assert response.action_required is True
+ assert response.kind == TransactionKind.ACTION_TO_CONFIRM
+ 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,
+ metadata={
+ "channel": channel_USD.slug,
+ "payment_id": payment_info.graphql_payment_id,
+ },
+ )
+ assert not mocked_customer.called
+
+
+@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_enabled_include_receipt_email(
+ mocked_payment_intent,
+ mocked_customer,
+ stripe_plugin,
+ payment_stripe_for_checkout,
+ channel_USD,
+):
+ # given
+ 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 = "requires_payment_method"
+
+ plugin = stripe_plugin(auto_capture=True, include_receipt_email=True)
+
+ payment_info = create_payment_information(
+ payment_stripe_for_checkout,
+ )
+
+ # when
+ response = plugin.process_payment(payment_info, None)
+
+ # then
+ assert response.is_success is True
+ assert response.action_required is True
+ assert response.kind == TransactionKind.ACTION_TO_CONFIRM
+ 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,
+ metadata={
+ "channel": channel_USD.slug,
+ "payment_id": payment_info.graphql_payment_id,
+ },
+ receipt_email=payment_stripe_for_checkout.checkout.email,
+ )
+ assert not mocked_customer.called
+
+
@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(