Skip to content

Commit

Permalink
Fix currency bug, some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BerglundDaniel committed Nov 15, 2023
1 parent 86186f8 commit aff128c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions api/src/service/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, name=None, **kwargs):
HOST_PUBLIC="",
STRIPE_PRIVATE_KEY=None,
STRIPE_SIGNING_SECRET=None,
STRIPE_CURRENCY=None,
APP_DEBUG=None,
CORS_ALLOWED_ORIGINS="https://medlem.makerspace.se,https://stockholm.makeradmin.se,https://medlem.dev.makerspace.se"
",http://localhost:8009,http://localhost:8011,http://localhost:8080",
Expand Down
2 changes: 1 addition & 1 deletion api/src/shop/stripe_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# All stripe calculations are done with cents (ören in Sweden)
STRIPE_CURRENTY_BASE = 100

CURRENCY = "sek" # TODO fix config.get("STRIPE_CURRENCY")
CURRENCY = config.get("STRIPE_CURRENCY")

STRIPE_SIGNING_SECRET = config.get("STRIPE_SIGNING_SECRET", log_value=False)

Expand Down
1 change: 0 additions & 1 deletion api/src/shop/stripe_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def delete_stripe_customer(member_id: int) -> None:
db_session.flush()


# TODO is this a test help function only?
def attach_and_set_default_payment_method(
member: Member,
payment_method: stripe.PaymentMethod,
Expand Down
34 changes: 30 additions & 4 deletions api/src/shop/stripe_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from datetime import datetime, timezone
from dataclasses import asdict, dataclass
from decimal import Decimal
from enum import Enum
import random
from sqlalchemy import func
import time
from logging import getLogger
from typing import Any, Callable, Dict, List, TypeVar
Expand All @@ -28,6 +26,34 @@
}


def get_category() -> ProductCategory:
offset = 0
while True:
with db_session.begin_nested():
try:
offset += 1
category = (
db_session.query(ProductCategory).filter(ProductCategory.name == "Subscriptions").one_or_none()
)
if category is None:
category = ProductCategory(
name="Subscriptions",
display_order=(db_session.query(func.max(ProductCategory.display_order)).scalar() or 0)
+ offset,
)
db_session.add(category)
db_session.flush()

return category
except Exception as e:
# I think, if this setup happens inside a transaction, we may not be able to see another category with the same display order,
# but we will still be prevented from creating a new one with that display order.
# So we incrementally increase the display order until we find a free one.
# This race condition will basically only happen when executing tests in parallel.
# TODO: Can this be done in a better way?
logger.info("Race condition when creating category. Trying again: ", e)


@dataclass
class StripeRecurring:
interval: str | None = None
Expand Down Expand Up @@ -122,7 +148,7 @@ def eq_makeradmin_stripe(
differences = {"product": eq_makeradmin_stripe_product(makeradmin_product, stripe_product)}
price_types = []
expected_number_of_prices = 1
if makeradmin_product.category.name != "Subscriptions": # TODO cleanup
if makeradmin_product.category.id != get_category().id:
price_types.append(PriceType.REGULAR_PRODUCT)
else:
price_types.append(PriceType.RECURRING)
Expand Down Expand Up @@ -200,7 +226,7 @@ def find_or_create_stripe_prices_for_product(
makeradmin_product: Product, stripe_product: stripe.Product, livemode: bool = False
) -> Dict[PriceType, stripe.Price | None] | None:
price_types = []
if makeradmin_product.category.name != "Subscriptions": # TODO cleanup
if makeradmin_product.category.id != get_category().id:
price_types.append(PriceType.REGULAR_PRODUCT)
else:
price_types.append(PriceType.RECURRING)
Expand Down
2 changes: 1 addition & 1 deletion api/src/shop/test/stripe_util_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logging import getLogger
from typing import Any, Dict, List
from unittest import skipIf, mock
from unittest import skipIf

import membership.models
import shop.models
Expand Down

0 comments on commit aff128c

Please sign in to comment.