Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

fix: Update Stripe DPM check in handle_processor_response #4158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions ecommerce/extensions/payment/processors/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
42 changes: 29 additions & 13 deletions ecommerce/extensions/payment/tests/views/test_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
Loading