用户取消订阅后无法重新购买订阅产品

I’ve got an odd problem cropped up here with a subscriber.

They have no active subscriptions in under the Billing section;

In Stripe it shows as cancelled;

image

yet they cannot resubscribe… says already purchased;

image

Have I set something up incorrectly?

1 个赞

That looks like you might have your webhook endpoint improperly configured. When the subscription period ends, Stripe sends a webhook which the plugin uses to remove group membership, and remove the user from being an existing customer on that product (which allows the user to purchase again).

You’ll need to make sure this is configured per the OP here: Discourse Subscriptions

One way you to confirm from your server:

./launcher enter app
rails c
u = User.find_by_username(<username>)
c = DiscourseSubscriptions::Customer.where(user_id: u.id)
subscription = DiscourseSubscriptions::Subscription.where(customer_id: c.customer_id)

Be sure to replace <username> with the actual username of the user.

If subscription returns [], there’s another issue going on. But if it returns a value, that likely confirms the missing webhook.

To delete the DiscourseSubscriptions::Subscription entry so the user can purchase the product again:

Make sure subscription has only one value by running subscription.count. If the value returned is greater than 1, you’ll have to do a bit more digging to make sure you’re deleting the correct subscription instance. If you delete the wrong one, your subscriptions data will be in an inconsistent state and likely yield weird behavior.

Once you’ve confirmed there’s only one subscription, then you can run:

subscription.destroy_all

Keep in mind that subscription is the value returned from the initial set of queries we ran in the Rails console.

Hope that helps!

2 个赞

Thanks @justin

• got to the last line subscription and ran it… returned this;

NoMethodError: undefined method customer_id' for #<DiscourseSubscriptions::Customer::ActiveRecord_Relation:0x000055be473772f0> from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/relation/delegation.rb:110:in method_missing’

• Checked Stripe webhooks and it’s pointed to https://./s/hooks

• Events sent to webhook;

  • subscription_schedule.updated
    
  • subscription_schedule.expiring
    
  • subscription_schedule.created
    
  • subscription_schedule.canceled
    

• Stripe webhook log has one entry from setup date which looks to be establishing the events supported.

• I have an event entry in Stripe that is a cancellation of a subscription, but… the line entry says (effectively) “[subscription was canceled]” however when I click into the event, it says the event type is customer.subscription.deleted … which I notice is not listed in my webhooks event list.

Could the above be the problem?

1 个赞

Looks like the line should be DiscourseSubscriptions::Subscription instead of ::Customer. Try that and see what you get!

1 个赞

[2] pry(main)> c = DiscourseSubscriptions::Subscription.where(user_id: u.id)
=> #DiscourseSubscriptions::Subscription::ActiveRecord_Relation:0x8020
[3] pry(main)> subscription = DiscourseSubscriptions::Subscription.where(customer_id: c.customer_id)
NoMethodError: undefined method customer_id' for #<DiscourseSubscriptions::Subscription::ActiveRecord_Relation:0x000055ec51164ca0> from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/relation/delegation.rb:110:in method_missing’

1 个赞

Sorry, I misunderstood where this was happening.

Try this instead:

./launcher enter app
rails c
u = User.find_by_username(<username>)
c = DiscourseSubscriptions::Customer.find_by(user_id: u.id)
subscription = DiscourseSubscriptions::Subscription.where(customer_id: c[:customer_id])
1 个赞

Ah…

[3] pry(main)> subscription = DiscourseSubscriptions::Subscription.where(customer_id: c[:customer_id])
=>

1 个赞

Do you think the lack of subscription data has anything to do with the customer.subscription.deleted event being missing?

Is there anything I should explore next at this point?

I’m particularly interested in focusing on why the subscription page shows “purchased” while the active subscriptions section shows no active subs… is there some other place in the database where sub info is stored which might be cleared / empty under the active subs section but somehow a “purchased” boolean is still set to true somewhere … or something like that?

1 个赞

Is there anything else to try here?

1 个赞

Make sure in Stripe the purchase is fully cancelled and not waiting to be cancelled at the end of the period. You can also look under the Admin → Plugins → Discourse Subscriptions → Subscriptions area and look for this user’s username. If they have a subscription listed there, you can cancel it immediately.

Without further looking into your Discourse instance, I’m not sure how much further I can help.

1 个赞

yeah it’s not active under subscriptions in the plugin area, and also not on stripe either… the only thing that indicates anything is still active is if the user goes to the subscription page, they see a little green “purchased” checkmark and “go to billing”.

image

If I go to billing as the user in question, nothing;

image

So my question is - what in the database triggers that green checkmark and can it be cleared?

If this list includes the Stripe product id as per here:

Then the front end will display it as purchased. You may need to dig around the console with these queries to find out where it’s located. I almost guarantee there’s a record hanging around there yet.

1 个赞

好的,我知道我是在“复活”这个帖子,但首先 1) 这个问题一直没有得到解决,其次 2) 我花了一些时间重新研究这个问题……结果发现我是一个连说明都看不懂的白痴。我就是这样发现自己是个笨蛋的,也是为什么我现在准备(在一些帮助下)解决这个问题。

我最终的做法是从 Stripe 导出所有已取消帐户的 CSV。我重点关注了该主题中前面提到的 Discourse 用户,他是一名订阅者,已经取消了订阅,但之后无法重新订阅,因为订阅页面显示为“已购买”,尽管他的活动订阅页面没有显示任何订阅。在 CSV 数据中,他的记录显示为“已取消”,数据如下:

  • id (sub_JNWejdDo4qhLG5)
  • customer_id (cus_JmiMoqZNTzUNxt)
  • user_id (discourse 用户 ID 4981)

然后我附加到应用程序容器,进入 Ruby 控制台,开始进行一些探索。首先,我在 DiscourseSubscriptions::Customer 中搜索了他的 customer_id,找到了它。

[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>]

接下来,我搜索 DiscourseSubscriptions::Subscription,看看这个用户是否有任何订阅,结果是有的,确实有;

[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>]

所以,看起来我只需要删除这个订阅记录就可以解决——至少对这个用户来说。事实是,我在 CSV 导出中检查的每个已取消订阅的用户在系统中仍然有剩余的订阅。显然,Stripe 网页钩子和订阅插件之间在某个地方存在某种断开……我只是不知道在哪里/如何。

  • 首先;我如何安全地删除特定用户的订阅记录数据?像我 5 岁一样解释给我听……我现在在 Ruby 控制台提示符下……我如何确保只删除目标用户的订阅数据,而不会清除所有人的订阅数据?

  • 其次,我如何对订阅取消网页钩子机制进行故障排除?

非常感谢您的所有帮助!

更新:

好的,我取得了一些进展……我设法弄清楚如何使用 .delete_by 来仅删除目标用户的订阅数据;

[1] pry(main)> DiscourseSubscriptions::Subscription.delete_by(external_id: 'sub_JNWejdDo4qhLG5')
[2] pry(main)> DiscourseSubscriptions::Subscription.where(external_id: 'sub_JNWejdDo4qhLG5')
=> []

但是,客户记录仍然有一个与之关联的 product_id

[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>]

这意味着他们仍然看到绿色复选标记,并且无法重新订阅……所以我对 Customer 对象应用了“destroy_all”……它奏效了!但当然只对那个用户有效……

[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>]

所以现在只剩下诊断为什么 Stripe 取消订阅的网页钩子不起作用了。