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

Commit

Permalink
fix: coupon/redeem URL redirects user to legacy checkout when a vouch…
Browse files Browse the repository at this point in the history
…er is invalid
  • Loading branch information
NoyanAziz authored Jul 12, 2024
1 parent fd38b22 commit 039fe3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 9 additions & 3 deletions ecommerce/coupons/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,16 @@ def test_sku_not_provided(self):
def test_invalid_voucher_code(self):
""" Verify an error is returned when voucher does not exist. """
code = FuzzyText().fuzz().upper()
expected_message = 'No voucher found with code {code}'.format(code=code)
url = format_url(base=self.redeem_url, params={'code': code, 'sku': self.stock_record.partner_sku})
response = self.client.get(url)
msg = 'No voucher found with code {code}'.format(code=code)
self.assertEqual(response.context['error'], msg)
response = self.client.get(url, follow=True)

messages = []
messages += response.context['messages']

self.assertEqual(messages[0].tags, 'error')
self.assertEqual(messages[0].message, expected_message)
self.assert_redemption_page_redirects(self.get_coupon_redeem_success_expected_redirect_url())

def test_no_product(self):
""" Verify an error is returned when a stock record for the provided SKU doesn't exist. """
Expand Down
20 changes: 15 additions & 5 deletions ecommerce/coupons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,26 @@ def get(self, request): # pylint: disable=too-many-statements
if not sku:
return render(request, template_name, {'error': _('SKU not provided.')})

try:
product = StockRecord.objects.get(partner_sku=sku).product
except StockRecord.DoesNotExist:
return render(request, template_name, {'error': _('The product does not exist.')})

try:
voucher = Voucher.objects.get(code=code)
except Voucher.DoesNotExist:
try:
basket = prepare_basket(request, [product])
except AlreadyPlacedOrderException:
msg = _('You have already purchased {course} seat.').format(course=product.course.name)
return render(request, template_name, {'error': msg})

msg = 'No voucher found with code {code}'.format(code=code)
return render(request, template_name, {'error': _(msg)})
messages.error(self.request, _(msg))

try:
product = StockRecord.objects.get(partner_sku=sku).product
except StockRecord.DoesNotExist:
return render(request, template_name, {'error': _('The product does not exist.')})
redirect_url = get_payment_microfrontend_or_basket_url(self.request) + "?coupon_redeem_redirect=1"
redirect_url = add_stripe_flag_to_url(redirect_url, self.request)
return HttpResponseRedirect(redirect_url)

valid_voucher, msg, hide_error_message = voucher_is_valid(voucher, [product], request)
if not valid_voucher:
Expand Down

0 comments on commit 039fe3f

Please sign in to comment.