-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server/subscription: fix handling of PWYW subscriptions
- Loading branch information
1 parent
98be449
commit 54a50b9
Showing
11 changed files
with
563 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,21 @@ | |
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"dev": "mintlify dev", | ||
"generate-webhooks": "tsx .polar/generate-webhooks.mts openapi.yaml snippets/webhooks" | ||
"generate-webhooks": "tsx .polar/generate-webhooks.mts openapi.yaml snippets/webhooks", | ||
"broken-links": "mintlify broken-links" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"packageManager": "[email protected]+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0", | ||
"dependencies": { | ||
"mintlify": "^4.0.388" | ||
"mintlify": "^4.0.395" | ||
}, | ||
"devDependencies": { | ||
"@scalar/openapi-parser": "^0.10.6", | ||
"@types/node": "^22.13.2", | ||
"@scalar/openapi-parser": "^0.10.7", | ||
"@types/node": "^22.13.4", | ||
"js-yaml": "^4.1.0", | ||
"openapi-sampler": "^1.6.1", | ||
"tsx": "^4.19.2" | ||
"tsx": "^4.19.3" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
server/migrations/versions/2025-02-19-1526_update_stripe_subscriptions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Update Stripe Subscriptions | ||
Revision ID: 21585ed16305 | ||
Revises: 69d1834e6285 | ||
Create Date: 2025-02-19 15:26:53.346054 | ||
""" | ||
|
||
import concurrent.futures | ||
import random | ||
import time | ||
import uuid | ||
from typing import Any | ||
|
||
import sqlalchemy as sa | ||
import stripe as stripe_lib | ||
from alembic import op | ||
|
||
# Polar Custom Imports | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "21585ed16305" | ||
down_revision = "69d1834e6285" | ||
branch_labels: tuple[str] | None = None | ||
depends_on: tuple[str] | None = None | ||
|
||
|
||
def process_subscription( | ||
subscription: tuple[str, uuid.UUID, uuid.UUID], retry: int = 1 | ||
) -> None: | ||
stripe_id, product_id, price_id = subscription | ||
metadata = { | ||
"type": "product", | ||
"product_id": str(product_id), | ||
"product_price_id": str(price_id), | ||
} | ||
try: | ||
stripe_lib.Subscription.modify( | ||
stripe_id, | ||
metadata=metadata, | ||
) | ||
except stripe_lib.RateLimitError: | ||
time.sleep(retry + random.random()) | ||
return process_subscription(subscription, retry=retry + 1) | ||
|
||
|
||
def process_subscriptions(results: sa.CursorResult[Any]) -> None: | ||
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: | ||
for result in results: | ||
executor.submit(process_subscription, result._tuple()) | ||
|
||
|
||
def upgrade() -> None: | ||
connection = op.get_bind() | ||
results = connection.execute( | ||
sa.text(""" | ||
SELECT stripe_subscription_id, product_id, price_id | ||
FROM subscriptions | ||
WHERE stripe_subscription_id IS NOT NULL | ||
""") | ||
) | ||
process_subscriptions(results) | ||
|
||
|
||
def downgrade() -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.