Spaces:
Sleeping
Sleeping
Ready Ok!
Browse files- app.py → main.py +2 -49
app.py → main.py
RENAMED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import stripe
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, Request, Header
|
| 5 |
-
from fastapi.staticfiles import StaticFiles
|
| 6 |
-
from fastapi.templating import Jinja2Templates
|
| 7 |
-
import time
|
| 8 |
-
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -19,50 +16,6 @@ def start():
|
|
| 19 |
|
| 20 |
return {"Status":"Deployed"}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
@app.get("/success")
|
| 24 |
-
async def success(request: Request):
|
| 25 |
-
return templates.TemplateResponse("success.html", {"request": request})
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@app.get("/cancel")
|
| 29 |
-
async def cancel(request: Request):
|
| 30 |
-
return templates.TemplateResponse("cancel.html", {"request": request})
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
@app.post("/create-checkout-session")
|
| 34 |
-
async def create_checkout_session(request: Request):
|
| 35 |
-
data = await request.json()
|
| 36 |
-
|
| 37 |
-
if not app.state.stripe_customer_id:
|
| 38 |
-
customer = stripe.Customer.create(
|
| 39 |
-
description="Demo customer",
|
| 40 |
-
)
|
| 41 |
-
app.state.stripe_customer_id = customer["id"]
|
| 42 |
-
|
| 43 |
-
checkout_session = stripe.checkout.Session.create(
|
| 44 |
-
customer=app.state.stripe_customer_id,
|
| 45 |
-
success_url="http://localhost:8000/success?session_id={CHECKOUT_SESSION_ID}",
|
| 46 |
-
cancel_url="http://localhost:8000/cancel",
|
| 47 |
-
payment_method_types=["card"],
|
| 48 |
-
mode="subscription",
|
| 49 |
-
line_items=[{
|
| 50 |
-
"price": data["priceId"],
|
| 51 |
-
"quantity": 1
|
| 52 |
-
}],
|
| 53 |
-
)
|
| 54 |
-
return {"sessionId": checkout_session["id"]}
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
@app.post("/create-portal-session")
|
| 58 |
-
async def create_portal_session():
|
| 59 |
-
session = stripe.billing_portal.Session.create(
|
| 60 |
-
customer=app.state.stripe_customer_id,
|
| 61 |
-
return_url="http://localhost:8000"
|
| 62 |
-
)
|
| 63 |
-
return {"url": session.url}
|
| 64 |
-
|
| 65 |
-
|
| 66 |
@app.post("/webhook")
|
| 67 |
async def webhook_received(request: Request, stripe_signature: str = Header(None)):
|
| 68 |
webhook_secret = os.environ["STRIPE_WEBHOOK_SECRET"]
|
|
@@ -100,4 +53,4 @@ async def webhook_received(request: Request, stripe_signature: str = Header(None
|
|
| 100 |
|
| 101 |
|
| 102 |
if __name__ == '__main__':
|
| 103 |
-
uvicorn.run("
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import stripe
|
| 4 |
import uvicorn
|
| 5 |
from fastapi import FastAPI, Request, Header
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 16 |
|
| 17 |
return {"Status":"Deployed"}
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@app.post("/webhook")
|
| 20 |
async def webhook_received(request: Request, stripe_signature: str = Header(None)):
|
| 21 |
webhook_secret = os.environ["STRIPE_WEBHOOK_SECRET"]
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
if __name__ == '__main__':
|
| 56 |
+
uvicorn.run("main:app", reload=True)
|