|
1 | | -import polar_sdk |
2 | 1 | from os import getenv |
3 | 2 | from fastapi import FastAPI |
4 | 3 | from polar_sdk import Polar |
5 | 4 | from dotenv import load_dotenv |
6 | | -from upstash_redis import Redis |
7 | 5 | from fastapi.responses import RedirectResponse |
8 | 6 |
|
9 | 7 | load_dotenv() |
10 | 8 |
|
11 | 9 | app = FastAPI() |
12 | 10 |
|
| 11 | +product_id = "a-b-c-d" |
13 | 12 |
|
14 | 13 | @app.get("/{amount}") |
15 | | -def read_item(amount: int): |
16 | | - polar = Polar(access_token=getenv("POLAR_API_KEY")) # Organization Access Token |
17 | | - redis = Redis.from_env() |
18 | | - product_id = redis.get(f"dynamic_price_{amount}_product_id") |
19 | | - if not product_id: |
20 | | - res = polar.products.create( |
21 | | - request={ |
22 | | - "recurring_interval": None, |
23 | | - "name": "Dynamic Product Name", |
24 | | - "description": "Dynamic Product Description", |
25 | | - "prices": [ |
26 | | - polar_sdk.ProductPriceFixedCreate( |
27 | | - amount_type="fixed", price_amount=amount * 100 |
28 | | - ) |
29 | | - ], |
30 | | - } |
31 | | - ) |
32 | | - product_id = res.id |
33 | | - redis.set(f"dynamic_price_{amount}_product_id", product_id) |
34 | | - checkoutSession = polar.checkouts.create(request={"products": [product_id]}) |
| 14 | +def open_checkout(amount: int): |
| 15 | + polar = Polar(access_token=getenv("POLAR_API_KEY")) |
| 16 | + checkoutSession = polar.checkouts.create(request={ |
| 17 | + "products": [product_id], |
| 18 | + "prices": { |
| 19 | + product_id: [ |
| 20 | + { |
| 21 | + "price_amount": amount, |
| 22 | + "amount_type": "fixed", |
| 23 | + "price_currency": "usd", |
| 24 | + } |
| 25 | + ] |
| 26 | + } |
| 27 | + }) |
35 | 28 | return RedirectResponse(url=checkoutSession.url) |
0 commit comments