Skip to content

Commit

Permalink
Fixed double subscription bug + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelRobidas committed Aug 12, 2023
1 parent 6789e00 commit fbac674
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cloud_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ periodictable
psutil==5.7.2
psycopg2
pysisyphus
rdkit
requests
scipy
sklearn
scikit-learn
spyrmsd
stripe
wheel
Expand Down
6 changes: 5 additions & 1 deletion frontend/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5118,7 +5118,7 @@ def create_subscription(
user, stripe_sub_id, end_date, allocation, stripe_cus_id, will_renew
):
time_left = max(0, user.allocated_seconds - user.billed_seconds)
sub_amount = allocation - time_left
sub_amount = max(allocation - time_left, 0)

# Make sure we don't create a duplicate subscription and allocation
try:
Expand All @@ -5144,6 +5144,10 @@ def create_subscription(
return

user.refresh_from_db()
if user.stripe_cus_id != "" and user.stripe_cus_id != stripe_cus_id:
logger.warning(
f"Changing the Stripe customer id of user {user.id} from {user.stripe_cus_id} to {stripe_cus_id}"
)
user.stripe_cus_id = stripe_cus_id
user.stripe_will_renew = will_renew
user.save()
4 changes: 2 additions & 2 deletions frontend/templates/frontend/pricing.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ <h1 class="title is-1">Unlock the Potential of Quantum Chemistry</h1>
<form action="/checkout/" method="POST">
{% csrf_token %}
<input type="hidden" name="priceId" value="{{ SUBSCRIPTION_DATA.researcher.monthly }}">
<button class="button is-primary is-fullwidth" type="submit" id="subscribe_researcher_btn_month">Subscribe</button>
<button class="button is-primary is-fullwidth" type="submit" id="subscribe_researcher_btn_month" {% if request.user.is_subscriber %}disabled{% endif %}>Subscribe</button>
</form>
<em id="time_note">*additional time available</em>
</div>
Expand All @@ -214,7 +214,7 @@ <h1 class="title is-1">Unlock the Potential of Quantum Chemistry</h1>
<form action="/checkout/" method="POST">
{% csrf_token %}
<input type="hidden" name="priceId" value="{{ SUBSCRIPTION_DATA.researcher.yearly }}">
<button class="button is-primary is-fullwidth" type="submit" id="subscribe_researcher_btn_year">Subscribe</button>
<button class="button is-primary is-fullwidth" type="submit" id="subscribe_researcher_btn_year" {% if request.user.is_subscriber %}disabled{% endif %}>Subscribe</button>
</form>
<em id="time_note">*additional time available</em>
</div>
Expand Down
24 changes: 16 additions & 8 deletions frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5781,21 +5781,29 @@ def webhook(request):
datetime.fromtimestamp(sub["current_period_end"])
)

if len(sub["items"]["data"]) != 1:
logger.error(
f"Subscription {sub['id']} unexpectedly has more than one item"
)

prod = stripe.Product.retrieve(sub["items"]["data"][0]["price"]["product"])
allocation = int(prod["metadata"]["MONTHLY_ALLOCATION"])

try:
user = User.objects.get(email=customer["email"])
except User.DoesNotExist:
logger.warning(
f"Customer with email {customer['email']} does not have an account"
)
user = create_account_sub(customer["email"])
else:
if user.is_subscriber:
sub = user.subscription_set.latest("pk")
if sub.end_date - timezone.now() > timezone.timedelta(hours=1):
logger.critical(
f"User {user.id} with email {user.email} is already a subscriber, yet has paid for a subscription!"
)
return HttpResponse(status=400)

if len(sub["items"]["data"]) != 1:
logger.error(
f"Subscription {sub['id']} unexpectedly has more than one item"
)

prod = stripe.Product.retrieve(sub["items"]["data"][0]["price"]["product"])
allocation = int(prod["metadata"]["MONTHLY_ALLOCATION"])

tasks.create_subscription(
user,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pysisyphus
rdkit
requests
scipy
sklearn
scikit-learn
spyrmsd
wheel
xkcdpass

0 comments on commit fbac674

Please sign in to comment.