6772312ea7
The primary license of the project is changing to: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later with some specific files to be licensed under the one of two licenses: CC0-1.0 LGPL-3.0-or-later This commit is one of the many steps to relicense the entire codebase. Documentation granting permission for this relicensing (from all past contributors who hold copyrights) is on file with Software Freedom Conservancy, Inc.
35 lines
1.5 KiB
Ruby
35 lines
1.5 KiB
Ruby
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
|
module CancelBillingSubscription
|
|
|
|
# @param [Nonprofit] nonprofit
|
|
# @return [BillingSubscription] new billing subscription for the nonprofit
|
|
def self.with_stripe(nonprofit)
|
|
begin
|
|
ParamValidation.new({nonprofit: nonprofit}, {
|
|
nonprofit: {required: true, is_a: Nonprofit}
|
|
})
|
|
rescue ParamValidation::ValidationError => e
|
|
return {json: {error: "Validation error\n #{e.message}", errors: e.data}, status: :unprocessable_entity}
|
|
end
|
|
np_card = nonprofit.active_card
|
|
billing_subscription = nonprofit.billing_subscription
|
|
return {json:{error: 'We don\'t have a subscription for your non-profit. Please contact support.'}, status: :unprocessable_entity} if np_card.nil? || billing_subscription.nil? # stripe_customer_id on Card object
|
|
|
|
# Cancel and delete the subscription on Stripe
|
|
begin
|
|
customer = Stripe::Customer.retrieve(np_card.stripe_customer_id)
|
|
stripe_subscription = customer.subscriptions.retrieve(billing_subscription.stripe_subscription_id)
|
|
s = stripe_subscription.delete(at_period_end: false)
|
|
rescue Stripe::StripeError => e
|
|
return {json: {error: "Oops! There was an error processing your subscription cancellation. Error: #{e}"}, status: :unprocessable_entity}
|
|
end
|
|
|
|
billing_plan_id = Settings.default_bp.id
|
|
billing_subscription.update_attributes({
|
|
billing_plan_id: billing_plan_id,
|
|
status: 'active'
|
|
})
|
|
|
|
return {json:{}, status: :ok}
|
|
end
|
|
end
|