From 03c04a7d4a0db04619b2bcb56f3b5d5856ca023b Mon Sep 17 00:00:00 2001 From: julianajlk Date: Tue, 7 May 2024 19:32:35 -0400 Subject: [PATCH 1/4] fix: Remove the bool check for dynamic_payment_methods_enabled --- ecommerce/extensions/payment/processors/stripe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecommerce/extensions/payment/processors/stripe.py b/ecommerce/extensions/payment/processors/stripe.py index e54f6f38d33..771c56a374c 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 From d75ae09a4f9133b9ea7bd951f36842e55accbf7e Mon Sep 17 00:00:00 2001 From: julianajlk Date: Wed, 8 May 2024 09:14:18 -0400 Subject: [PATCH 2/4] fix: Add logs after PI confirmation --- .../extensions/payment/processors/stripe.py | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/ecommerce/extensions/payment/processors/stripe.py b/ecommerce/extensions/payment/processors/stripe.py index 771c56a374c..bbaa6c8f928 100644 --- a/ecommerce/extensions/payment/processors/stripe.py +++ b/ecommerce/extensions/payment/processors/stripe.py @@ -301,25 +301,33 @@ 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].', + payment_intent_id, + basket.id, + basket.order_number, + dynamic_payment_methods_enabled + ) + # 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, From 61c055c42559b49167ec9ab7d6e9cea842ac3020 Mon Sep 17 00:00:00 2001 From: julianajlk Date: Wed, 8 May 2024 11:00:31 -0400 Subject: [PATCH 3/4] test: Add test for log in DPM payment --- .../payment/tests/views/test_stripe.py | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/ecommerce/extensions/payment/tests/views/test_stripe.py b/ecommerce/extensions/payment/tests/views/test_stripe.py index fc44256e5d0..40ea5f66f97 100644 --- a/ecommerce/extensions/payment/tests/views/test_stripe.py +++ b/ecommerce/extensions/payment/tests/views/test_stripe.py @@ -531,21 +531,36 @@ 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 [{}].'.format( + payment_intent_id, + basket.id, + basket.order_number, + dynamic_payment_methods_enabled + ) + ) + actual_log = log.output[6] + assert actual_log == expected_log def test_handle_payment_fails_with_carderror(self): """ From f772cb70cbbf60539c5122bc3205b4b50dd7f57e Mon Sep 17 00:00:00 2001 From: julianajlk Date: Wed, 8 May 2024 11:57:57 -0400 Subject: [PATCH 4/4] fix: Add status of the payment intent to log --- ecommerce/extensions/payment/processors/stripe.py | 5 +++-- ecommerce/extensions/payment/tests/views/test_stripe.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ecommerce/extensions/payment/processors/stripe.py b/ecommerce/extensions/payment/processors/stripe.py index bbaa6c8f928..1830ae1cd76 100644 --- a/ecommerce/extensions/payment/processors/stripe.py +++ b/ecommerce/extensions/payment/processors/stripe.py @@ -303,11 +303,12 @@ def handle_processor_response(self, response, basket=None): logger.info( 'Confirmed Stripe payment intent [%s] for basket [%d] and order number [%s], ' - 'with dynamic_payment_methods_enabled [%s].', + 'with dynamic_payment_methods_enabled [%s] and status [%s].', payment_intent_id, basket.id, basket.order_number, - dynamic_payment_methods_enabled + 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 diff --git a/ecommerce/extensions/payment/tests/views/test_stripe.py b/ecommerce/extensions/payment/tests/views/test_stripe.py index 40ea5f66f97..8cece888bf9 100644 --- a/ecommerce/extensions/payment/tests/views/test_stripe.py +++ b/ecommerce/extensions/payment/tests/views/test_stripe.py @@ -552,11 +552,12 @@ def test_payment_handle_payment_intent_in_progress(self): expected_log = ( 'INFO:ecommerce.extensions.payment.processors.stripe:' 'Confirmed Stripe payment intent [{}] for basket [{}] and order number [{}], ' - 'with dynamic_payment_methods_enabled [{}].'.format( + 'with dynamic_payment_methods_enabled [{}] and status [{}].'.format( payment_intent_id, basket.id, basket.order_number, - dynamic_payment_methods_enabled + dynamic_payment_methods_enabled, + response.json()['status'] ) ) actual_log = log.output[6]