From 248331a68b44c98e0cb588e8b467071eb8c15caf Mon Sep 17 00:00:00 2001 From: Moggach Date: Thu, 18 Jul 2024 14:08:44 +0000 Subject: [PATCH 1/2] add additional attribute check for applies_to before checking product id when redeeming gift subscriptions --- app/utils/stripe.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/utils/stripe.py b/app/utils/stripe.py index 8dd4be4..8b4e108 100644 --- a/app/utils/stripe.py +++ b/app/utils/stripe.py @@ -340,14 +340,14 @@ def create_gift_recipient_subscription( promo_code = stripe.PromotionCode.retrieve( promo_code_id, expand=["coupon.applies_to"] ) - if product_id in promo_code.coupon.applies_to.products: - # the gift card is fit for purpose + if hasattr(promo_code.coupon, 'applies_to') and product_id in promo_code.coupon.applies_to.products: + # the gift card is fit for purpose discount_args = {"promotion_code": promo_code_id} + else: - # if they're in this situation of the gift card being for an old product - # get the right coupon for the target product + # if they're in this situation of the gift card being for an old product + # get the right coupon for the target product coupon = get_gift_card_coupon(product_id) - # invalidate the gift card's promo code so it can't be used again promo_code = stripe.PromotionCode.modify(promo_code_id, active=False) From 36ca2af51343c8f11ca2201d3bba18e69883f5a4 Mon Sep 17 00:00:00 2001 From: Moggach Date: Thu, 18 Jul 2024 16:47:08 +0000 Subject: [PATCH 2/2] add more thorough has attribute checking --- app/utils/stripe.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/utils/stripe.py b/app/utils/stripe.py index 8b4e108..099f274 100644 --- a/app/utils/stripe.py +++ b/app/utils/stripe.py @@ -340,8 +340,11 @@ def create_gift_recipient_subscription( promo_code = stripe.PromotionCode.retrieve( promo_code_id, expand=["coupon.applies_to"] ) - if hasattr(promo_code.coupon, 'applies_to') and product_id in promo_code.coupon.applies_to.products: - # the gift card is fit for purpose + if ( + hasattr(promo_code.coupon, 'applies_to') and + hasattr(promo_code.coupon.applies_to, 'products') and + product_id in promo_code.coupon.applies_to.products + ): # the gift card is fit for purpose discount_args = {"promotion_code": promo_code_id} else: