Skip to content

Commit 5c7e1f1

Browse files
Merge pull request #71 from usermisc/patch-1
Update main.py
2 parents 4e5d2e5 + 434fd5e commit 5c7e1f1

4 files changed

Lines changed: 17 additions & 51 deletions

File tree

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
# Polar Organization Access Token
22
POLAR_API_KEY="polar_oat_..."
3-
# Upstash Environment Variables
4-
UPSTASH_REDIS_REST_URL="https://....upstash.io"
5-
UPSTASH_REDIS_REST_TOKEN="...="

dynamic-not-editable-pricing/README.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
# Dynamic Not Editable Pricing
44

5-
This FastAPI application creates dynamic pricing products with Polar integration. It generates products on-demand with custom amounts and caches them using Redis for performance.
5+
This FastAPI application creates dynamic pricing products with Polar integration. It uses [ad-hoc prices](https://polar.sh/docs/features/checkout/session#ad-hoc-prices).
66

77
## Prerequisites
88

99
- Python 3+ installed on your system
1010
- Your Polar API key (Organization Access Token)
11-
- Upstash Redis instance
1211

1312
## Setup
1413

@@ -43,8 +42,6 @@ Add your credentials to the `.env` file:
4342

4443
```
4544
POLAR_API_KEY=your_polar_oat_here
46-
UPSTASH_REDIS_REST_URL=your_upstash_redis_url_here
47-
UPSTASH_REDIS_REST_TOKEN=your_upstash_redis_token_here
4845
```
4946

5047
You can find your API key in your Polar's Organization settings.
@@ -58,23 +55,3 @@ fastapi dev main.py
5855
```
5956

6057
The application will be available at `http://localhost:8000`.
61-
62-
## API Endpoints
63-
64-
### GET `/{amount}`
65-
66-
Creates a dynamic product with the specified amount and redirects to the Polar checkout page.
67-
68-
**Parameters:**
69-
- `amount` (integer): The price amount in dollars (e.g., 10 for $10.00)
70-
71-
**Example:**
72-
```
73-
GET /10
74-
```
75-
76-
This will:
77-
1. Check if a Product ID with amount $10 already exists in Redis cache
78-
2. If not cached, create a new product in Polar with the specified amount
79-
3. Cache the Product ID in Redis for future requests
80-
4. Create a checkout session and redirect to the Polar checkout page
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
import polar_sdk
21
from os import getenv
32
from fastapi import FastAPI
43
from polar_sdk import Polar
54
from dotenv import load_dotenv
6-
from upstash_redis import Redis
75
from fastapi.responses import RedirectResponse
86

97
load_dotenv()
108

119
app = FastAPI()
1210

11+
product_id = "a-b-c-d"
1312

1413
@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+
})
3528
return RedirectResponse(url=checkoutSession.url)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
python-dotenv
22
polar-sdk
3-
upstash-redis
4-
fastapi[standard]
3+
fastapi[standard]

0 commit comments

Comments
 (0)