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

Commit

Permalink
fix: Remove no longer needed PI status from basket attr
Browse files Browse the repository at this point in the history
  • Loading branch information
julianajlk committed Mar 29, 2024
1 parent db36155 commit de66ddc
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 46 deletions.
1 change: 0 additions & 1 deletion ecommerce/extensions/basket/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
EMAIL_OPT_IN_ATTRIBUTE = "email_opt_in"
PURCHASER_BEHALF_ATTRIBUTE = "purchased_for_organization"
PAYMENT_INTENT_ID_ATTRIBUTE = "payment_intent_id"
PAYMENT_INTENT_STATUS_ATTRIBUTE = "payment_intent_status"
DYNAMIC_PAYMENT_METHODS_ENABLED = "dynamic_payment_methods_enabled"

# .. toggle_name: enable_stripe_payment_processor
Expand Down
14 changes: 4 additions & 10 deletions ecommerce/extensions/basket/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
_set_basket_bundle_status,
apply_voucher_on_basket_and_check_discount,
basket_add_dynamic_payment_methods_enabled,
basket_add_payment_intent_id_attribute,
basket_add_payment_intent_status
basket_add_payment_intent_id_attribute
)
from ecommerce.extensions.catalogue.tests.mixins import DiscoveryTestMixin
from ecommerce.extensions.offer.constants import DYNAMIC_DISCOUNT_FLAG
Expand Down Expand Up @@ -331,12 +330,11 @@ def create_basket_and_add_product(self, product):
basket.add_product(product, 1)
return basket

def create_basket_and_add_product_stripe(self, product, payment_intent_status, payment_intent_id, payment_intent):
def create_basket_and_add_product_stripe(self, product, payment_intent_id, payment_intent):
basket = self.create_empty_basket()
basket.add_product(product, 1)
basket_add_dynamic_payment_methods_enabled(basket, payment_intent)
basket_add_payment_intent_id_attribute(basket, payment_intent_id)
basket_add_payment_intent_status(basket, payment_intent_status)
return basket

def create_seat(self, course, seat_price=100, cert_type='verified'):
Expand Down Expand Up @@ -437,7 +435,6 @@ def assert_expected_response(
messages=None,
summary_discounts=None,
payment_intent_id=None,
payment_intent_status=None,
**kwargs
):
if response is None:
Expand Down Expand Up @@ -485,7 +482,6 @@ def assert_expected_response(
'summary_price': summary_price,
'order_total': order_total,
'payment_intent_id': payment_intent_id,
'payment_intent_status': payment_intent_status,
'products': [
{
'product_type': product_type,
Expand Down Expand Up @@ -735,9 +731,8 @@ def test_enable_stripe_payment_processor_flag(self, enable_stripe_payment_proces
def test_cart_with_stripe_data(self):
"""
For Dynamic Payment Methods, the basket will contain Payment Intent information.
Verify that the basket contains Payment Intent ID, Payment Intent status, and if DPM is enabled.
Verify that the basket contains Payment Intent ID and if DPM is enabled.
"""
payment_intent_status = 'requires_payment_method'
payment_intent_id = 'pi_3OqcQ5H4caH7G0X11y8NKNa4'
payment_intent = {
'payment_method_types': [
Expand All @@ -747,14 +742,13 @@ def test_cart_with_stripe_data(self):
}
seat = self.create_seat(self.course)
basket = self.create_basket_and_add_product_stripe(
seat, payment_intent_status, payment_intent_id, payment_intent
seat, payment_intent_id, payment_intent
)
response = self.client.get(self.path)
self.assert_expected_response(
basket,
response=response,
is_dynamic_payment_methods_enabled=len(payment_intent['payment_method_types']) > 1,
payment_intent_status=payment_intent_status,
payment_intent_id=payment_intent_id
)

Expand Down
15 changes: 0 additions & 15 deletions ecommerce/extensions/basket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
EMAIL_OPT_IN_ATTRIBUTE,
ENABLE_STRIPE_PAYMENT_PROCESSOR,
PAYMENT_INTENT_ID_ATTRIBUTE,
PAYMENT_INTENT_STATUS_ATTRIBUTE,
PURCHASER_BEHALF_ATTRIBUTE,
REDIRECT_WITH_WAFFLE_TESTING_QUERYSTRING
)
Expand Down Expand Up @@ -396,20 +395,6 @@ def basket_add_organization_attribute(basket, request_data):
)


@newrelic.agent.function_trace()
def basket_add_payment_intent_status(basket, payment_intent_status):
payment_intent_status_attribute, __ = BasketAttributeType.objects.get_or_create(
name=PAYMENT_INTENT_STATUS_ATTRIBUTE)
# Do a get_or_create and update value_text after (instead of update_or_create)
# to prevent a particularly slow full table scan that uses a LIKE
basket_attribute, __ = BasketAttribute.objects.get_or_create(
attribute_type=payment_intent_status_attribute,
basket=basket,
)
basket_attribute.value_text = payment_intent_status
basket_attribute.save()


@newrelic.agent.function_trace()
def basket_add_dynamic_payment_methods_enabled(basket, payment_intent):
"""
Expand Down
15 changes: 1 addition & 14 deletions ecommerce/extensions/basket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
from ecommerce.extensions.basket.constants import (
DYNAMIC_PAYMENT_METHODS_ENABLED,
ENABLE_STRIPE_PAYMENT_PROCESSOR,
PAYMENT_INTENT_ID_ATTRIBUTE,
PAYMENT_INTENT_STATUS_ATTRIBUTE
PAYMENT_INTENT_ID_ATTRIBUTE
)
from ecommerce.extensions.basket.exceptions import BadRequestException, RedirectException, VoucherException
from ecommerce.extensions.basket.utils import (
Expand Down Expand Up @@ -683,7 +682,6 @@ def _serialize_context(self, context, lines_data):
self._add_coupons(response, context)
self._add_enable_stripe_payment_processor(response)
self._add_payment_intent_id(response, self.request.basket)
self._add_payment_intent_status(response, self.request.basket)
self._add_is_dynamic_payment_methods(response, self.request.basket)
return response

Expand Down Expand Up @@ -758,17 +756,6 @@ def _add_payment_intent_id(self, response, basket):
except (BasketAttribute.DoesNotExist, BasketAttributeType.DoesNotExist):
response['payment_intent_id'] = None

def _add_payment_intent_status(self, response, basket):
try:
payment_intent_status_attribute = BasketAttributeType.objects.get(name=PAYMENT_INTENT_STATUS_ATTRIBUTE)
payment_intent_attr = BasketAttribute.objects.get(
basket=basket,
attribute_type=payment_intent_status_attribute
)
response['payment_intent_status'] = payment_intent_attr.value_text.strip()
except (BasketAttribute.DoesNotExist, BasketAttributeType.DoesNotExist):
response['payment_intent_status'] = None

def _add_is_dynamic_payment_methods(self, response, basket):
try:
dynamic_payment_methods_enabled_attribute = BasketAttributeType.objects.get(
Expand Down
8 changes: 2 additions & 6 deletions ecommerce/extensions/payment/processors/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from ecommerce.extensions.basket.utils import (
basket_add_dynamic_payment_methods_enabled,
basket_add_payment_intent_id_attribute,
basket_add_payment_intent_status,
get_basket_courses_list,
get_billing_address_from_payment_intent_data
)
Expand Down Expand Up @@ -113,7 +112,7 @@ def create_new_payment_intent_for_basket(self, basket, payment_intent_id):
This is used as a reset of the payment to allow payment retries when the intent gets into unexpected states.
"""
# Cancel existing Payment Intent
stripe.PaymentIntent.cancel(payment_intent_id)
cancelled_payment_intent = stripe.PaymentIntent.cancel(payment_intent_id)

# Create a new Payment Intent and add to Basket
new_payment_intent = stripe.PaymentIntent.create(
Expand All @@ -128,13 +127,11 @@ def create_new_payment_intent_for_basket(self, basket, payment_intent_id):
new_payment_intent_id = new_payment_intent['id']
logger.info(
'Canceled Payment Intent [%s] and created new Payment Intent [%s] for basket [%d]',
payment_intent_id,
cancelled_payment_intent['id'],
new_payment_intent_id,
basket.id,
)
new_payment_intent_status = new_payment_intent['status']
basket_add_payment_intent_id_attribute(basket, new_payment_intent_id)
basket_add_payment_intent_status(basket, new_payment_intent_status)
basket_add_dynamic_payment_methods_enabled(basket, new_payment_intent)
return new_payment_intent

Expand Down Expand Up @@ -185,7 +182,6 @@ def get_capture_context(self, request):
)

basket_add_payment_intent_id_attribute(basket, transaction_id)
basket_add_payment_intent_status(basket, stripe_response['status'])
basket_add_dynamic_payment_methods_enabled(basket, stripe_response)

# Check if payment intent is in unexpected state, ie. 'requires_action'
Expand Down

0 comments on commit de66ddc

Please sign in to comment.