Ok, I know I’m necromancing this thread but 1) this continues to go unsolved, and 2) I found some time to dig into this again.. turns out I was a moron who could not follow directions. This is how I found out I’m a dummy, and why I’m now ready to solve this (with some help).
What I ended up doing was exporting a CSV from stripe of all cancelled accounts. I focused on the very same discourse user referenced above in this thread who was a subscriber, had cancelled, but then could not re-subscribe because the subscription page showed as “purchased” even though his active subscriptions page showed no subs. In the CSV data, his record showed as “canceled” with the following data;
- id (sub_JNWejdDo4qhLG5)
- customer_id (cus_JmiMoqZNTzUNxt)
- user_id (discourse user id 4981)
Then I attached to the app container, went into the ruby console, and started poking around. First I searched the for his customer ID in DiscourseSubscriptions::Customer and found it.
[3] pry(main)> DiscourseSubscriptions::Customer.where(customer_id: 'cus_JNWeiIWTs6YOwQ')
=> [#<DiscourseSubscriptions::Customer:0x000055db2d48eb40
id: 16,
customer_id: "cus_JNWeiIWTs6YOwQ",
product_id: "prod_JJbwYnKz0T5Z9h",
user_id: 3256,
created_at: Tue, 27 Apr 2021 07:45:07.455275000 UTC +00:00,
updated_at: Tue, 27 Apr 2021 07:45:07.455275000 UTC +00:00>]
Next I searched DiscourseSubscriptions::Subscription to see if this user had any subscriptions and turns out yes, yes they did;
[1] pry(main)> DiscourseSubscriptions::Subscription.where(external_id: 'sub_JNWejdDo4qhLG5')
=> [#<DiscourseSubscriptions::Subscription:0x000055854e1e1620
id: 16,
customer_id: 16,
external_id: "sub_JNWejdDo4qhLG5",
created_at: Tue, 27 Apr 2021 07:45:07.459681000 UTC +00:00,
updated_at: Tue, 27 Apr 2021 07:45:07.459681000 UTC +00:00>]
So, it appears I can simply remove this subscription record and fix everything.. for this user. The fact is, every cancelled user I checked on my CSV export still had remaining subscriptions in the system. Obviously there is a disconnect somehow / somewhere between the Stripe webhook and the subscription plugin.. I just don’t know where / how.
-
First; how do I safely remove the subscription record data for a particular user? Explain it to me like I’m 5.. I’m at the ruby console prompt.. how do I ensure only the target user’s subscription data is removed and I don’t wipe out everyone’s subscription data?
-
Second, how do I troubleshoot the subscription cancellation webhook mechanism?
All your help is very much appreciated!
UPDATE:
Ok, I got a little further.. I managed to sort out how to use .delete_by to only remove subscription data for the target user;
[1] pry(main)> DiscourseSubscriptions::Subscription.delete_by(external_id: 'sub_JNWejdDo4qhLG5')
[2] pry(main)> DiscourseSubscriptions::Subscription.where(external_id: 'sub_JNWejdDo4qhLG5')
=> []
However, the customer record still has a product_id associated;
[3] pry(main)> DiscourseSubscriptions::Customer.where(customer_id: 'cus_JNWeiIWTs6YOwQ')
=> [#<DiscourseSubscriptions::Customer:0x0000560ec18e4948
id: 16,
customer_id: "cus_JNWeiIWTs6YOwQ",
product_id: "prod_JJbwYnKz0T5Z9h",
user_id: 3256,
created_at: Tue, 27 Apr 2021 07:45:07.455275000 UTC +00:00,
updated_at: Tue, 27 Apr 2021 07:45:07.455275000 UTC +00:00>]
This means the are still seeing the green check and can’t resubscribe.. so I applied the “destroy_all” to the Customer object.. and it worked! But only for that user of course..
[4] pry(main)> DiscourseSubscriptions::Customer.where(customer_id: 'cus_JNWeiIWTs6YOwQ').destroy_all
=> [#<DiscourseSubscriptions::Customer:0x0000560ec19770e0
id: 16,
customer_id: "cus_JNWeiIWTs6YOwQ",
product_id: "prod_JJbwYnKz0T5Z9h",
user_id: 3256,
created_at: Tue, 27 Apr 2021 07:45:07.455275000 UTC +00:00,
updated_at: Tue, 27 Apr 2021 07:45:07.455275000 UTC +00:00>]
So now it’s just down to diagnosing why the Stripe webhook for cancelled subscriptions isn’t working.