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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -8,35 +8,58 @@ import time
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.")
 
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 create a Stripe Checkout Session
12
+ def create_checkout_session():
13
+ try:
14
+ session = stripe.checkout.Session.create(
15
+ payment_method_types=['card'],
16
+ line_items=[{
17
+ 'price': 'your-stripe-price-id', # Replace with your actual Stripe price ID
18
+ 'quantity': 1,
19
+ }],
20
+ mode='payment',
21
+ success_url='https://your-website.com/success?session_id={CHECKOUT_SESSION_ID}',
22
+ cancel_url='https://your-website.com/cancel',
23
+ )
24
+ return session.id, session.url
25
+ except Exception as e:
26
+ st.error(f"Error creating checkout session: {e}")
27
+ return None, None
28
+
29
+ # Function to check payment status
30
  def check_payment_status(session_id):
31
  try:
32
  session = stripe.checkout.Session.retrieve(session_id)
33
  return session.payment_status == 'paid'
34
+ except Exception as e:
35
  st.error(f"Error checking payment status: {e}")
36
  return False
37
 
38
  # Streamlit app
39
  st.title("Stripe Payment Integration")
40
 
41
+ if 'session_id' not in st.session_state:
42
+ st.session_state.session_id = None
43
+
44
  # Button to redirect to Stripe Checkout
45
+ if st.button("Create Checkout Session"):
46
+ session_id, checkout_url = create_checkout_session()
47
+ if session_id and checkout_url:
48
+ st.session_state.session_id = session_id
49
+ st.write(f"Checkout URL: {checkout_url}")
50
+ st.markdown(f"[Proceed to Checkout]({checkout_url})", unsafe_allow_html=True)
51
 
52
  # Input for session ID to check payment status
53
+ if st.session_state.session_id:
54
+ st.write(f"Your session ID: {st.session_state.session_id}")
55
+ if st.button("Check Payment Status"):
 
56
  st.write("Checking payment status, please wait...")
57
  time.sleep(2) # Simulating delay for payment processing
58
 
59
+ if check_payment_status(st.session_state.session_id):
60
  st.success("Payment successful!")
61
  st.write("Here's the paid content.")
62
  else:
63
  st.error("Payment not completed yet. Please try again.")
64
+ else:
65
+ st.info("Create a checkout session to get the session ID.")