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

fix: Legacy redirect for invalid coupon do not allow continuation of checkout process #4173

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
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
22 changes: 16 additions & 6 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:
voucher = Voucher.objects.get(code=code)
except Voucher.DoesNotExist:
msg = 'No voucher found with code {code}'.format(code=code)
return render(request, template_name, {'error': _(msg)})

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)
messages.error(self.request, _(msg))

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
Loading