Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Check gift card currency when adding gift card to the checkout

IKarbowiak358745797979
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 8e6c190af7..97cc2eb08d 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -486,7 +486,9 @@ def add_promo_code_to_checkout(
manager, checkout_info, lines, promo_code, discounts
)
elif promo_code_is_gift_card(promo_code):
- add_gift_card_code_to_checkout(checkout_info.checkout, promo_code)
+ add_gift_card_code_to_checkout(
+ checkout_info.checkout, promo_code, checkout_info.channel.currency_code
+ )
else:
raise InvalidPromoCode()
diff --git a/saleor/giftcard/tests/test_utils.py b/saleor/giftcard/tests/test_utils.py
index 46129c9b51..2022355acb 100644
--- a/saleor/giftcard/tests/test_utils.py
+++ b/saleor/giftcard/tests/test_utils.py
@@ -11,7 +11,7 @@ def test_add_gift_card_code_to_checkout(checkout, gift_card):
assert checkout.gift_cards.count() == 0
# when
- add_gift_card_code_to_checkout(checkout, gift_card.code)
+ add_gift_card_code_to_checkout(checkout, gift_card.code, gift_card.currency)
# then
assert checkout.gift_cards.count() == 1
@@ -27,7 +27,7 @@ def test_add_gift_card_code_to_checkout_inactive_card(checkout, gift_card):
# when
# then
with pytest.raises(InvalidPromoCode):
- add_gift_card_code_to_checkout(checkout, gift_card.code)
+ add_gift_card_code_to_checkout(checkout, gift_card.code, gift_card.currency)
def test_add_gift_card_code_to_checkout_expired_card(checkout, gift_card):
@@ -40,7 +40,20 @@ def test_add_gift_card_code_to_checkout_expired_card(checkout, gift_card):
# when
# then
with pytest.raises(InvalidPromoCode):
- add_gift_card_code_to_checkout(checkout, gift_card.code)
+ add_gift_card_code_to_checkout(checkout, gift_card.code, gift_card.currency)
+
+
+def test_add_gift_card_code_to_checkout_invalid_currency(checkout, gift_card):
+ # given
+ currency = "EUR"
+
+ assert gift_card.currency != currency
+ assert checkout.gift_cards.count() == 0
+
+ # when
+ # then
+ with pytest.raises(InvalidPromoCode):
+ add_gift_card_code_to_checkout(checkout, gift_card.code, currency)
def test_remove_gift_card_code_from_checkout(checkout, gift_card):
diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.py
index 4c354ceab9..3fa645a807 100644
--- a/saleor/giftcard/utils.py
+++ b/saleor/giftcard/utils.py
@@ -5,13 +5,18 @@ from ..core.utils.promo_code import InvalidPromoCode
from .models import GiftCard
-def add_gift_card_code_to_checkout(checkout: Checkout, promo_code: str):
+def add_gift_card_code_to_checkout(checkout: Checkout, promo_code: str, currency: str):
"""Add gift card data to checkout by code.
Raise InvalidPromoCode if gift card cannot be applied.
"""
try:
- gift_card = GiftCard.objects.active(date=date.today()).get(code=promo_code)
+ # only active gift card with currency the same as channel currency can be used
+ gift_card = (
+ GiftCard.objects.active(date=date.today())
+ .filter(currency=currency)
+ .get(code=promo_code)
+ )
except GiftCard.DoesNotExist:
raise InvalidPromoCode()
checkout.gift_cards.add(gift_card)