Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import stripe
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Set your secret key. Remember to switch to your live secret key in production!
|
6 |
+
stripe.api_key = os.environ["STRIPE_API_KEY"]
|
7 |
+
|
8 |
+
# Function to retrieve product information from Stripe
|
9 |
+
def get_product(product_id):
|
10 |
+
try:
|
11 |
+
product = stripe.Product.retrieve(product_id)
|
12 |
+
price = stripe.Price.list(product=product_id)
|
13 |
+
return product, price.data[0]
|
14 |
+
except Exception as e:
|
15 |
+
st.error(f"Error fetching product information: {e}")
|
16 |
+
return None, None
|
17 |
+
|
18 |
+
# Function to create a checkout session
|
19 |
+
def create_checkout_session(price_id):
|
20 |
+
try:
|
21 |
+
session = stripe.checkout.Session.create(
|
22 |
+
payment_method_types=['card'],
|
23 |
+
line_items=[{
|
24 |
+
'price': price_id,
|
25 |
+
'quantity': 1,
|
26 |
+
}],
|
27 |
+
mode='payment',
|
28 |
+
success_url='https://your-website.com/success',
|
29 |
+
cancel_url='https://your-website.com/cancel',
|
30 |
+
)
|
31 |
+
return session.url
|
32 |
+
except Exception as e:
|
33 |
+
st.error(f"Error creating checkout session: {e}")
|
34 |
+
return None
|
35 |
+
|
36 |
+
# Streamlit app
|
37 |
+
st.title("Stripe Product Checkout")
|
38 |
+
|
39 |
+
product_id = st.text_input("Enter Stripe Product ID", value="your-product-id")
|
40 |
+
|
41 |
+
if st.button("Get Product Info"):
|
42 |
+
product, price = get_product(product_id)
|
43 |
+
if product:
|
44 |
+
st.write("### Product Information")
|
45 |
+
st.write(f"**Name:** {product['name']}")
|
46 |
+
st.write(f"**Description:** {product['description']}")
|
47 |
+
st.write(f"**Price:** ${price['unit_amount'] / 100:.2f}")
|
48 |
+
|
49 |
+
if st.button("Checkout"):
|
50 |
+
checkout_url = create_checkout_session(price['id'])
|
51 |
+
if checkout_url:
|
52 |
+
st.markdown(f"[Proceed to Checkout]({checkout_url})", unsafe_allow_html=True)
|