Skip to content

Commit

Permalink
fix: update payment page redirect for program
Browse files Browse the repository at this point in the history
  • Loading branch information
attiyaIshaque committed Jan 30, 2025
1 parent ec41fa4 commit 2d9cb4a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
22 changes: 22 additions & 0 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,28 @@ def get_payment_by_transaction_interaction_id(self, interaction_id: str) -> CTPa
logger.info(f"[CommercetoolsAPIClient] - Attempting to find payment with interaction ID {interaction_id}")
return self.base_client.payments.query(where=f'transactions(interactionId="{interaction_id}")').results[0]

def get_product_variant_by_program(self, product_id: str) -> Optional[CTProductVariant]:
"""
Fetches a program from Commercetools.
Args:
product_id: The ID of the program (bundle) to fetch.
Returns:
CTProductVariant if found, None otherwise.
"""
start_time = datetime.datetime.now()
try:
product_projection = self.base_client.product_projections.get_by_key(product_id)
except CommercetoolsError as exc:
logger.exception(
f'[get_product_variant_by_program] Failed to get CT program for product_id: {product_id} '
f'with exception: {exc}'
)
return None

duration = (datetime.datetime.now() - start_time).total_seconds()
logger.info(f"[Performance Check] get_product_variant_by_program took {duration} seconds")
return product_projection

Check failure on line 392 in commerce_coordinator/apps/commercetools/clients.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04, 3.12, django42)

Missing coverage

Missing coverage on lines 380-392

def get_product_variant_by_course_run(self, cr_id: str) -> Optional[CTProductVariant]:
"""
Args:
Expand Down
3 changes: 2 additions & 1 deletion commerce_coordinator/apps/lms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
enrollment_attribute_key
)
from commerce_coordinator.apps.rollout.utils import is_legacy_order
from commerce_coordinator.apps.rollout.waffle import is_program_new_redirection_enabled

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,7 +86,7 @@ def _redirect_response_payment(self, request):

get_items = list(self.request.GET.lists())
redirect_url = None
if "bundle" in dict(get_items):
if "bundle" in dict(get_items) and not is_program_new_redirection_enabled(request):
ecom_url = urljoin(
settings.ECOMMERCE_URL, settings.ECOMMERCE_ADD_TO_BASKET_API_PATH
)
Expand Down
27 changes: 20 additions & 7 deletions commerce_coordinator/apps/rollout/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
from commerce_coordinator.apps.enterprise_learner.utils import is_user_enterprise_learner
from commerce_coordinator.apps.frontend_app_payment.constants import FRONTEND_APP_PAYMENT_CHECKOUT
from commerce_coordinator.apps.rollout.utils import is_legacy_order
from commerce_coordinator.apps.rollout.waffle import is_redirect_to_commercetools_enabled_for_user
from commerce_coordinator.apps.rollout.waffle import (
is_program_new_redirection_enabled,
is_redirect_to_commercetools_enabled_for_user
)

logger = logging.getLogger(__name__)

Expand All @@ -31,17 +34,29 @@ def run_filter(self, request): # pylint: disable=arguments-differ
"""
Execute a filter with the signature specified.
Arguments:
request: request object passed through from the lms filter
request: request object passed through from the LMS filter
Returns:
dict:
active_order_management_system: result from pipeline steps to determine if
redirect_to_commercetools_checkout flag is enabled and course_run_key
and sku query params exist and detect which checkout to redirect to
redirect_to_commercetools_checkout flag is enabled and course_run_key,
sku, or bundle query params exist, and detect which checkout to redirect to.
"""
sku = request.query_params.get('sku', '').strip()
course_run = request.query_params.get('course_run_key', '').strip()
bundle = request.query_params.get('bundle', '').strip()
commercetools_available_course = None

ct_api_client = CommercetoolsAPIClient()

if bundle and is_program_new_redirection_enabled(request):
commercetools_available_program = ct_api_client.get_product_variant_by_program(bundle)
if commercetools_available_program:
return {ACTIVE_ORDER_MANAGEMENT_SYSTEM_KEY: COMMERCETOOLS_FRONTEND}
logger.warning(

Check failure on line 55 in commerce_coordinator/apps/rollout/pipeline.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04, 3.12, django42)

Missing coverage

Missing coverage on lines 52-55
f'[get_product_variant_by_program] Program {bundle} not found in Commercetools. '
f'Ensure it is properly synced.'
)

if course_run and is_redirect_to_commercetools_enabled_for_user(request):
try:
ct_api_client = CommercetoolsAPIClient()
Expand All @@ -63,9 +78,7 @@ def run_filter(self, request): # pylint: disable=arguments-differ
raise OpenEdxFilterException('Neither course_run_key and waffle flag value nor sku found.'
'Unable to determine active order management system.')

return {
ACTIVE_ORDER_MANAGEMENT_SYSTEM_KEY: active_order_management_system
}
return {ACTIVE_ORDER_MANAGEMENT_SYSTEM_KEY: active_order_management_system}


class DetermineActiveOrderManagementSystemByOrderNumber(PipelineStep):
Expand Down
7 changes: 7 additions & 0 deletions commerce_coordinator/apps/rollout/waffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ def is_redirect_to_commercetools_enabled_for_user(request):
Check if REDIRECT_TO_COMMERCETOOLS_CHECKOUT flag is enabled.
"""
return waffle.flag_is_active(request, REDIRECT_TO_COMMERCETOOLS_CHECKOUT)


def is_program_new_redirection_enabled(request):
"""
Check if PROGRAM_NEW_REDIRECTION flag is enabled.
"""
return waffle.flag_is_active(request, "PROGRAM_NEW_REDIRECTION")

0 comments on commit 2d9cb4a

Please sign in to comment.