Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions dynamic-not-editable-pricing/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Polar Organization Access Token
POLAR_API_KEY="polar_oat_..."
# Upstash Environment Variables
UPSTASH_REDIS_REST_URL="https://....upstash.io"
UPSTASH_REDIS_REST_TOKEN="...="
25 changes: 1 addition & 24 deletions dynamic-not-editable-pricing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

# Dynamic Not Editable Pricing

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.
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).

## Prerequisites

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

## Setup

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

```
POLAR_API_KEY=your_polar_oat_here
UPSTASH_REDIS_REST_URL=your_upstash_redis_url_here
UPSTASH_REDIS_REST_TOKEN=your_upstash_redis_token_here
```

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

The application will be available at `http://localhost:8000`.

## API Endpoints

### GET `/{amount}`

Creates a dynamic product with the specified amount and redirects to the Polar checkout page.

**Parameters:**
- `amount` (integer): The price amount in dollars (e.g., 10 for $10.00)

**Example:**
```
GET /10
```

This will:
1. Check if a Product ID with amount $10 already exists in Redis cache
2. If not cached, create a new product in Polar with the specified amount
3. Cache the Product ID in Redis for future requests
4. Create a checkout session and redirect to the Polar checkout page
37 changes: 15 additions & 22 deletions dynamic-not-editable-pricing/main.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import polar_sdk
from os import getenv
from fastapi import FastAPI
from polar_sdk import Polar
from dotenv import load_dotenv
from upstash_redis import Redis
from fastapi.responses import RedirectResponse

load_dotenv()

app = FastAPI()

product_id = "a-b-c-d"

@app.get("/{amount}")
def read_item(amount: int):
polar = Polar(access_token=getenv("POLAR_API_KEY")) # Organization Access Token
redis = Redis.from_env()
product_id = redis.get(f"dynamic_price_{amount}_product_id")
if not product_id:
res = polar.products.create(
request={
"recurring_interval": None,
"name": "Dynamic Product Name",
"description": "Dynamic Product Description",
"prices": [
polar_sdk.ProductPriceFixedCreate(
amount_type="fixed", price_amount=amount * 100
)
],
}
)
product_id = res.id
redis.set(f"dynamic_price_{amount}_product_id", product_id)
checkoutSession = polar.checkouts.create(request={"products": [product_id]})
def open_checkout(amount: int):
polar = Polar(access_token=getenv("POLAR_API_KEY"))
checkoutSession = polar.checkouts.create(request={
"products": [product_id],
"prices": {
product_id: [
{
"price_amount": amount,
"amount_type": "fixed",
"price_currency": "usd",
}
]
}
})
return RedirectResponse(url=checkoutSession.url)
3 changes: 1 addition & 2 deletions dynamic-not-editable-pricing/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
python-dotenv
polar-sdk
upstash-redis
fastapi[standard]
fastapi[standard]