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

View File

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

View File

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

View File

@ -77,18 +77,19 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "deletes a customer" do
::Stripe::Subscription
.expects(:delete)
.expects(:cancel)
.with("sub_12345")
.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
}.by(-1)
}
end
it "removes the user from the group" do
::Stripe::Subscription
.expects(:delete)
.expects(:cancel)
.with("sub_12345")
.returns(
plan: {
@ -107,7 +108,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "does not remove the user from the group" do
::Stripe::Subscription
.expects(:delete)
.expects(:cancel)
.with("sub_12345")
.returns(
plan: {
@ -126,7 +127,7 @@ RSpec.describe DiscourseSubscriptions::Admin::SubscriptionsController do
it "refunds if params[:refund] present" do
::Stripe::Subscription
.expects(:delete)
.expects(:cancel)
.with("sub_12345")
.returns(plan: { product: "pr_34578" }, customer: "c_123")
::Stripe::Subscription