jesseCampeez
jesseCampeez

Reputation: 117

Get date a user changed the plan they are on in Stripe using Flask

Working with Stripe and trying to set some basic info into the metadata field. I have two plans created, a paid and free. The free plan is used when a customer cancels. Many of these customers have been changed through Stripe Dashboard already so using a webhook will not work.

With this code I am able to get all customers on a specific plan and show that in metadata. The problem is that the .created date gives me the date the customer was created and not the date the plan was changed. If I change the plan in dashboard by adding a new plan and deleting the old one (from that customer) I can use the time the old plan was unsubscribed. But the option in the dashboard to change plan does something different and there is no unsubscribe. My app is a connected account creating charges on other stripe accounts that have a dashboard of their own, so just not using the change plan button is not an option. Here is the code that gets the plan and created date.

canceled=stripe.Subscription.list(
plan='plan_Elm8GW7mwgDj5S',
stripe_account=stripe_keys['acct_num'],
)
for cancel in canceled.auto_paging_iter():


    customer_id=cancel.customer
    cd=cancel.created
    canceled_date=datetime.datetime.fromtimestamp(cd).strftime('%m-%d-%Y')
    stripe.Customer.modify(
    customer_id,
    stripe_account=stripe_keys['acct_num'],
    metadata={'Status': 'Canceled',
    'Canceled On': canceled_date}

    )

thank you!

Upvotes: 0

Views: 337

Answers (1)

taintedzodiac
taintedzodiac

Reputation: 2908

You can use the API to list events. You can specify the type of event to retrieve just those events, and look at the creation time of the event to know when it occurred.

It sounds like you want to focus on the customer.subscription.created event and then look at those events for subscriptions using the free plan. There are also other customer.subscription.* events for updates, deletions, and trials ending.

Upvotes: 1

Related Questions