FIX: Admins unable to cancel a subscription for a user (#220)

This commit is contained in:
Blake Erickson 2024-07-12 14:33:04 -06:00 committed by GitHub
parent 0fca89a1f7
commit c02193943b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 11 deletions

View File

@ -53,7 +53,7 @@ module DiscourseSubscriptions
params.require(:id) params.require(:id)
begin begin
refund_subscription(params[:id]) if params[:refund] refund_subscription(params[:id]) if params[:refund]
subscription = ::Stripe::Subscription.delete(params[:id]) subscription = ::Stripe::Subscription.cancel(params[:id])
customer = customer =
Customer.find_by( Customer.find_by(
@ -61,11 +61,8 @@ module DiscourseSubscriptions
customer_id: subscription[:customer], customer_id: subscription[:customer],
) )
Subscription.delete_by(external_id: params[:id])
if customer if customer
user = ::User.find(customer.user_id) user = ::User.find(customer.user_id)
customer.delete
group = plan_group(subscription[:plan]) group = plan_group(subscription[:plan])
group.remove(user) if group group.remove(user) if group
end end

View File

@ -25,7 +25,11 @@ export default class AdminCancelSubscription extends Component {
@label="yes_value" @label="yes_value"
@action={{fn @action={{fn
@model.cancelSubscription @model.cancelSubscription
(hash subscription=@model.subscription refund=this.refund) (hash
subscription=@model.subscription
refund=this.refund
closeModal=@closeModal
)
}} }}
@icon="times" @icon="times"
@isLoading={{@model.subscription.loading}} @isLoading={{@model.subscription.loading}}

View File

@ -40,6 +40,8 @@ export default Controller.extend({
cancelSubscription(model) { cancelSubscription(model) {
const subscription = model.subscription; const subscription = model.subscription;
const refund = model.refund; const refund = model.refund;
const closeModal = model.closeModal;
subscription.set("loading", true); subscription.set("loading", true);
subscription subscription
.destroy(refund) .destroy(refund)
@ -52,6 +54,7 @@ export default Controller.extend({
) )
.finally(() => { .finally(() => {
subscription.set("loading", false); subscription.set("loading", false);
closeModal();
}); });
}, },
}); });

View File

@ -77,18 +77,19 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "deletes a customer" do it "deletes a customer" do
::Stripe::Subscription ::Stripe::Subscription
.expects(:delete) .expects(:cancel)
.with("sub_12345") .with("sub_12345")
.returns(plan: { product: "pr_34578" }, customer: "c_123") .returns(plan: { product: "pr_34578" }, customer: "c_123")
expect { delete "/s/admin/subscriptions/sub_12345.json" }.to change { # We don't want to delete the customer record. The webhook hook will update the status instead.
expect { delete "/s/admin/subscriptions/sub_12345.json" }.not_to change {
DiscourseSubscriptions::Customer.count DiscourseSubscriptions::Customer.count
}.by(-1) }
end end
it "removes the user from the group" do it "removes the user from the group" do
::Stripe::Subscription ::Stripe::Subscription
.expects(:delete) .expects(:cancel)
.with("sub_12345") .with("sub_12345")
.returns( .returns(
plan: { plan: {
@ -107,7 +108,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "does not remove the user from the group" do it "does not remove the user from the group" do
::Stripe::Subscription ::Stripe::Subscription
.expects(:delete) .expects(:cancel)
.with("sub_12345") .with("sub_12345")
.returns( .returns(
plan: { plan: {
@ -126,7 +127,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "refunds if params[:refund] present" do it "refunds if params[:refund] present" do
::Stripe::Subscription ::Stripe::Subscription
.expects(:delete) .expects(:cancel)
.with("sub_12345") .with("sub_12345")
.returns(plan: { product: "pr_34578" }, customer: "c_123") .returns(plan: { product: "pr_34578" }, customer: "c_123")
::Stripe::Subscription ::Stripe::Subscription