eagle0504 commited on
Commit
eed0f39
·
verified ·
1 Parent(s): 6f7ed7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -43
app.py CHANGED
@@ -1,52 +1,42 @@
 
 
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://buy.stripe.com/14kbLG4FV5s509OdQV',
29
- cancel_url='https://www.future-minds.io/',
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)
 
 
 
 
 
 
 
1
+ import os
2
+
3
  import streamlit as st
4
  import stripe
5
+ import requests
6
+ import time
7
 
8
  # Set your secret key. Remember to switch to your live secret key in production!
9
  stripe.api_key = os.environ["STRIPE_API_KEY"]
10
 
11
+ # Function to simulate checking payment status (for demo purposes)
12
+ def check_payment_status(session_id):
 
 
 
 
 
 
 
 
 
 
13
  try:
14
+ session = stripe.checkout.Session.retrieve(session_id)
15
+ return session.payment_status == 'paid'
16
+ except stripe.error.StripeError as e:
17
+ st.error(f"Error checking payment status: {e}")
18
+ return False
 
 
 
 
 
 
 
 
 
19
 
20
  # Streamlit app
21
+ st.title("Stripe Payment Integration")
22
+
23
+ # Button to redirect to Stripe Checkout
24
+ if st.button("Go to Checkout"):
25
+ checkout_url = "https://buy.stripe.com/14kbLG4FV5s509OdQV"
26
+ st.markdown(f"[Proceed to Checkout]({checkout_url})", unsafe_allow_html=True)
27
+
28
+ # Input for session ID to check payment status
29
+ session_id = st.text_input("Enter your Stripe Session ID after payment:")
30
+
31
+ if st.button("Check Payment Status"):
32
+ if session_id:
33
+ st.write("Checking payment status, please wait...")
34
+ time.sleep(2) # Simulating delay for payment processing
35
+
36
+ if check_payment_status(session_id):
37
+ st.success("Payment successful!")
38
+ st.write("Here's the paid content.")
39
+ else:
40
+ st.error("Payment not completed yet. Please try again.")
41
+ else:
42
+ st.error("Please enter a valid session ID.")