-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstripe_auth.py
64 lines (55 loc) · 1.65 KB
/
stripe_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import stripe
import urllib.parse
def get_api_key() -> str:
testing_mode = st.secrets.get("testing_mode", False)
return (
st.secrets["stripe_api_key_test"]
if testing_mode
else st.secrets["stripe_api_key"]
)
def redirect_button(
text: str,
customer_email: str,
color="#FD504D",
payment_provider: str = "stripe",
):
testing_mode = st.secrets.get("testing_mode", False)
encoded_email = urllib.parse.quote(customer_email)
if payment_provider == "stripe":
stripe.api_key = get_api_key()
stripe_link = (
st.secrets["stripe_link_test"]
if testing_mode
else st.secrets["stripe_link"]
)
button_url = f"{stripe_link}?prefilled_email={encoded_email}"
elif payment_provider == "bmac":
button_url = f"{st.secrets['bmac_link']}"
else:
raise ValueError("payment_provider must be 'stripe' or 'bmac'")
st.sidebar.markdown(
f"""
<a href="{button_url}" target="_blank">
<div style="
display: inline-block;
padding: 0.5em 1em;
color: #FFFFFF;
background-color: {color};
border-radius: 3px;
text-decoration: none;">
{text}
</div>
</a>
""",
unsafe_allow_html=True,
)
def is_active_subscriber(email: str) -> bool:
stripe.api_key = get_api_key()
customers = stripe.Customer.list(email=email)
try:
customer = customers.data[0]
except IndexError:
return False
subscriptions = stripe.Subscription.list(customer=customer["id"])
return len(subscriptions) > 0