From 6f9cff2e781d7f47561c69c7cfa3b68ecf9d09c3 Mon Sep 17 00:00:00 2001 From: Juliana Kang Date: Wed, 8 May 2024 12:15:55 -0400 Subject: [PATCH] fix: Update Stripe DPM check in handle_processor_response (#4158) REV-4019 --- .../extensions/payment/processors/stripe.py | 35 ++++++++++------ .../payment/tests/views/test_stripe.py | 42 +++++++++++++------ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/ecommerce/extensions/payment/processors/stripe.py b/ecommerce/extensions/payment/processors/stripe.py index e54f6f38d33..1830ae1cd76 100644 --- a/ecommerce/extensions/payment/processors/stripe.py +++ b/ecommerce/extensions/payment/processors/stripe.py @@ -276,7 +276,7 @@ def handle_processor_response(self, response, basket=None): # pretty sure we should simply return/error if basket is None, as not # sure what it would mean if there payment_intent_id = response.get('payment_intent_id', None) - dynamic_payment_methods_enabled = response.get('dynamic_payment_methods_enabled', None) == 'true' + dynamic_payment_methods_enabled = response.get('dynamic_payment_methods_enabled', None) # NOTE: In the future we may want to get/create a Customer. See https://stripe.com/docs/api#customers. # rewrite order amount so it's updated for coupon & quantity and unchanged by the user @@ -301,25 +301,34 @@ def handle_processor_response(self, response, basket=None): logger.exception('Card Error for basket [%d]: %s}', basket.id, err) raise + logger.info( + 'Confirmed Stripe payment intent [%s] for basket [%d] and order number [%s], ' + 'with dynamic_payment_methods_enabled [%s] and status [%s].', + payment_intent_id, + basket.id, + basket.order_number, + dynamic_payment_methods_enabled, + confirm_api_response['status'] + ) + # If the payment has another status other than 'succeeded', we want to return to the MFE something it can handle - if dynamic_payment_methods_enabled: - if confirm_api_response['status'] == 'requires_action': - return InProgressProcessorResponse( - basket_id=basket.id, - order_number=basket.order_number, - status=confirm_api_response['status'], - confirmation_client_secret=confirm_api_response['client_secret'], - transaction_id=confirm_api_response['id'], - payment_method=confirm_api_response['payment_method'], - total=confirm_api_response['amount'], - ) + if confirm_api_response['status'] == 'requires_action': + return InProgressProcessorResponse( + basket_id=basket.id, + order_number=basket.order_number, + status=confirm_api_response['status'], + confirmation_client_secret=confirm_api_response['client_secret'], + transaction_id=confirm_api_response['id'], + payment_method=confirm_api_response['payment_method'], + total=confirm_api_response['amount'], + ) # proceed only if payment went through assert confirm_api_response['status'] == "succeeded" self.record_processor_response(confirm_api_response, transaction_id=payment_intent_id, basket=basket) logger.info( - 'Successfully confirmed Stripe payment intent [%s] for basket [%d] and order number [%s].', + 'Confirmed Stripe payment intent with succeeded status [%s] for basket [%d] and order number [%s].', payment_intent_id, basket.id, basket.order_number, diff --git a/ecommerce/extensions/payment/tests/views/test_stripe.py b/ecommerce/extensions/payment/tests/views/test_stripe.py index fc44256e5d0..8cece888bf9 100644 --- a/ecommerce/extensions/payment/tests/views/test_stripe.py +++ b/ecommerce/extensions/payment/tests/views/test_stripe.py @@ -531,21 +531,37 @@ def test_payment_handle_payment_intent_in_progress(self): with a 'requires_action' for a BNPL payment, which will be handled in the MFE. """ basket = self.create_basket(product_class=SEAT_PRODUCT_CLASS_NAME) + payment_intent_id = 'pi_3LsftNIadiFyUl1x2TWxaADZ' + dynamic_payment_methods_enabled = 'true' - response = self.payment_flow_with_mocked_stripe_calls( - self.stripe_checkout_url, - { - 'payment_intent_id': 'pi_3LsftNIadiFyUl1x2TWxaADZ', - 'skus': basket.lines.first().stockrecord.partner_sku, - 'dynamic_payment_methods_enabled': 'true', - }, - in_progress_payment=True, - ) + with self.assertLogs(level='INFO') as log: + response = self.payment_flow_with_mocked_stripe_calls( + self.stripe_checkout_url, + { + 'payment_intent_id': payment_intent_id, + 'skus': basket.lines.first().stockrecord.partner_sku, + 'dynamic_payment_methods_enabled': dynamic_payment_methods_enabled, + }, + in_progress_payment=True, + ) - assert response.status_code == 201 - # Should return 'requires_action' to the MFE with the same Payment Intent - assert response.json()['status'] == 'requires_action' - assert response.json()['transaction_id'] == 'pi_3LsftNIadiFyUl1x2TWxaADZ' + assert response.status_code == 201 + # Should return 'requires_action' to the MFE with the same Payment Intent + assert response.json()['status'] == 'requires_action' + assert response.json()['transaction_id'] == payment_intent_id + expected_log = ( + 'INFO:ecommerce.extensions.payment.processors.stripe:' + 'Confirmed Stripe payment intent [{}] for basket [{}] and order number [{}], ' + 'with dynamic_payment_methods_enabled [{}] and status [{}].'.format( + payment_intent_id, + basket.id, + basket.order_number, + dynamic_payment_methods_enabled, + response.json()['status'] + ) + ) + actual_log = log.output[6] + assert actual_log == expected_log def test_handle_payment_fails_with_carderror(self): """