Spaces:
Sleeping
Sleeping
Primer Commit
Browse files- .gitignore +3 -0
- app.py +105 -0
- ostest.py +5 -0
- requirements.txt +0 -0
- static/script.js +56 -0
- templates/cancel.html +7 -0
- templates/index.html +13 -0
- templates/success.html +7 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/venv/
|
| 2 |
+
/__pycache__/
|
| 3 |
+
stripe.exe
|
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 12 |
+
templates = Jinja2Templates(directory="templates")
|
| 13 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 14 |
+
|
| 15 |
+
stripe.api_key = os.environ["STRIPE_KEY"]
|
| 16 |
+
# This is a terrible idea, only used for demo purposes!
|
| 17 |
+
app.state.stripe_customer_id = None
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.get("/")
|
| 21 |
+
def index(request: Request):
|
| 22 |
+
return templates.TemplateResponse("index.html", {"request": request, "hasCustomer": app.state.stripe_customer_id is not None})
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@app.get("/success")
|
| 26 |
+
async def success(request: Request):
|
| 27 |
+
return templates.TemplateResponse("success.html", {"request": request})
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.get("/cancel")
|
| 31 |
+
async def cancel(request: Request):
|
| 32 |
+
return templates.TemplateResponse("cancel.html", {"request": request})
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@app.post("/create-checkout-session")
|
| 36 |
+
async def create_checkout_session(request: Request):
|
| 37 |
+
data = await request.json()
|
| 38 |
+
|
| 39 |
+
if not app.state.stripe_customer_id:
|
| 40 |
+
customer = stripe.Customer.create(
|
| 41 |
+
description="Demo customer",
|
| 42 |
+
)
|
| 43 |
+
app.state.stripe_customer_id = customer["id"]
|
| 44 |
+
|
| 45 |
+
checkout_session = stripe.checkout.Session.create(
|
| 46 |
+
customer=app.state.stripe_customer_id,
|
| 47 |
+
success_url="http://localhost:8000/success?session_id={CHECKOUT_SESSION_ID}",
|
| 48 |
+
cancel_url="http://localhost:8000/cancel",
|
| 49 |
+
payment_method_types=["card"],
|
| 50 |
+
mode="subscription",
|
| 51 |
+
line_items=[{
|
| 52 |
+
"price": data["priceId"],
|
| 53 |
+
"quantity": 1
|
| 54 |
+
}],
|
| 55 |
+
)
|
| 56 |
+
return {"sessionId": checkout_session["id"]}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@app.post("/create-portal-session")
|
| 60 |
+
async def create_portal_session():
|
| 61 |
+
session = stripe.billing_portal.Session.create(
|
| 62 |
+
customer=app.state.stripe_customer_id,
|
| 63 |
+
return_url="http://localhost:8000"
|
| 64 |
+
)
|
| 65 |
+
return {"url": session.url}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@app.post("/webhook")
|
| 69 |
+
async def webhook_received(request: Request, stripe_signature: str = Header(None)):
|
| 70 |
+
webhook_secret = os.environ["STRIPE_WEBHOOK_SECRET"]
|
| 71 |
+
|
| 72 |
+
print("Entré al webhook 182...")
|
| 73 |
+
time.sleep(1)
|
| 74 |
+
|
| 75 |
+
data = await request.body()
|
| 76 |
+
try:
|
| 77 |
+
event = stripe.Webhook.construct_event(
|
| 78 |
+
payload=data,
|
| 79 |
+
sig_header=stripe_signature,
|
| 80 |
+
secret=webhook_secret
|
| 81 |
+
)
|
| 82 |
+
event_data = event['data']
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return {"error": str(e)}
|
| 85 |
+
|
| 86 |
+
event_type = event['type']
|
| 87 |
+
|
| 88 |
+
print("Voy a imprimir el event type:")
|
| 89 |
+
print(event_type)
|
| 90 |
+
time.sleep(1)
|
| 91 |
+
|
| 92 |
+
if event_type == 'checkout.session.completed':
|
| 93 |
+
print('checkout session completed')
|
| 94 |
+
elif event_type == 'invoice.paid':
|
| 95 |
+
print('invoice paid')
|
| 96 |
+
elif event_type == 'invoice.payment_failed':
|
| 97 |
+
print('invoice payment failed')
|
| 98 |
+
else:
|
| 99 |
+
print(f'unhandled event: {event_type}')
|
| 100 |
+
|
| 101 |
+
return {"status": "success"}
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
if __name__ == '__main__':
|
| 105 |
+
uvicorn.run("app:app", reload=True)
|
ostest.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
api_key = os.environ["STRIPE_WEBHOOK_SECRET"]
|
| 4 |
+
|
| 5 |
+
print(api_key)
|
requirements.txt
ADDED
|
Binary file (722 Bytes). View file
|
|
|
static/script.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
var createCheckoutSession = function(priceId) {
|
| 2 |
+
return fetch("/create-checkout-session", {
|
| 3 |
+
method: "POST",
|
| 4 |
+
headers: {
|
| 5 |
+
"Content-Type": "application/json"
|
| 6 |
+
},
|
| 7 |
+
body: JSON.stringify({
|
| 8 |
+
priceId: priceId
|
| 9 |
+
})
|
| 10 |
+
}).then(function(result) {
|
| 11 |
+
return result.json();
|
| 12 |
+
});
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
const PREMIUM_PRICE_ID = '';
|
| 16 |
+
const BASIC_PRICE_ID = '';
|
| 17 |
+
const stripe = Stripe("");
|
| 18 |
+
|
| 19 |
+
document.addEventListener("DOMContentLoaded", function(event) {
|
| 20 |
+
document
|
| 21 |
+
.getElementById("checkout-premium")
|
| 22 |
+
.addEventListener("click", function(evt) {
|
| 23 |
+
createCheckoutSession(PREMIUM_PRICE_ID).then(function(data) {
|
| 24 |
+
stripe
|
| 25 |
+
.redirectToCheckout({
|
| 26 |
+
sessionId: data.sessionId
|
| 27 |
+
});
|
| 28 |
+
});
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
document
|
| 32 |
+
.getElementById("checkout-basic")
|
| 33 |
+
.addEventListener("click", function(evt) {
|
| 34 |
+
createCheckoutSession(BASIC_PRICE_ID).then(function(data) {
|
| 35 |
+
stripe
|
| 36 |
+
.redirectToCheckout({
|
| 37 |
+
sessionId: data.sessionId
|
| 38 |
+
});
|
| 39 |
+
});
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
const billingButton = document.getElementById("manage-billing");
|
| 43 |
+
if (billingButton) {
|
| 44 |
+
billingButton.addEventListener("click", function(evt) {
|
| 45 |
+
fetch("/create-portal-session", {
|
| 46 |
+
method: "POST"
|
| 47 |
+
})
|
| 48 |
+
.then(function(response) {
|
| 49 |
+
return response.json()
|
| 50 |
+
})
|
| 51 |
+
.then(function(data) {
|
| 52 |
+
window.location.href = data.url;
|
| 53 |
+
});
|
| 54 |
+
})
|
| 55 |
+
}
|
| 56 |
+
});
|
templates/cancel.html
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<head>
|
| 2 |
+
<title>Cancelled Checkout</title>
|
| 3 |
+
</head>
|
| 4 |
+
<body>
|
| 5 |
+
<h1>Payment was cancelled</h1>
|
| 6 |
+
<a href="/">Go home</a>
|
| 7 |
+
</body>
|
templates/index.html
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
<head>
|
| 3 |
+
<script src="https://js.stripe.com/v3/"></script>
|
| 4 |
+
<script src="{{ url_for('static', path='script.js') }}"></script>
|
| 5 |
+
</head>
|
| 6 |
+
<body>
|
| 7 |
+
<button id="checkout-premium">Subscribe Premium</button>
|
| 8 |
+
<button id="checkout-basic">Subscribe Basic</button>
|
| 9 |
+
{% if hasCustomer %}
|
| 10 |
+
<button id="manage-billing">Manage Billing</button>
|
| 11 |
+
{% endif %}
|
| 12 |
+
</body>
|
| 13 |
+
</html>
|
templates/success.html
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<head>
|
| 2 |
+
<title>Checkout Successful</title>
|
| 3 |
+
</head>
|
| 4 |
+
<body>
|
| 5 |
+
<h1>Payment was successful</h1>
|
| 6 |
+
<a href="/">Go home</a>
|
| 7 |
+
</body>
|