Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can customers cancel subscription #59

Open
amanca9999 opened this issue Mar 16, 2024 · 2 comments
Open

how can customers cancel subscription #59

amanca9999 opened this issue Mar 16, 2024 · 2 comments

Comments

@amanca9999
Copy link

Please advise, thank you!

Btw, is there an easy way to manage users like tracking users queries, etc. ?

@jozkotrapko
Copy link

import stripe

def get_customer_id(email):
    customers = stripe.Customer.list(email=email).auto_paging_iter()
    for customer in customers:
        return customer['id']
    return None

def cancel_subscription(email):
    customer_id = get_customer_id(email)
    

    subscriptions = stripe.Subscription.list(customer=customer_id).auto_paging_iter()
    for subscription in subscriptions:
        if subscription['status'] != 'canceled':
            if subscription['cancel_at_period_end']:
                st.write(
                    f"Your subscription {subscription['id']} was already accepted to be canceled after the end of the period.")
            else:
                stripe.Subscription.modify(
                    subscription['id'],
                    cancel_at_period_end=True,
                )
                st.write(f"Subscription {subscription['id']} will be canceled at the end of the current period.")

email = st.session_state.email
if 'confirm' not in st.session_state:
    st.session_state.confirm = False

if st.button('Cancel subscription'):
    st.session_state.confirm = not st.session_state.confirm

if st.session_state.confirm:
    if st.button('Are you sure you want to cancel sub?'):
        cancel_subscription(email)
        st.session_state.confirm = False

Make sure you add write permission on Subscriptions in Stripe API key.

Please advise, thank you!

Btw, is there an easy way to manage users like tracking users queries, etc. ?

@oscolabs
Copy link

oscolabs commented Jun 3, 2024

getting this error, how did you approach this?

AttributeError: st.session_state has no attribute "email". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants