@@ -384,6 +384,61 @@ def stripe_create_charge():
384384 return jsonify (paymentIntent .status )
385385
386386
387+ @background_task
388+ def do_delete_stripe_subscription (
389+ subscription_id , app = None
390+ ):
391+ """Stop (delete) stripe_subscription via its
392+ Stripe subscription_id
393+ """
394+ with app .app_context ():
395+ stripe .api_key = get_stripe_secret_key ()
396+ connect_account_id = get_stripe_connect_account_id ()
397+ subscription = Subscription .query .filter_by (
398+ stripe_subscription_id = subscription_id
399+ ).first ()
400+
401+ log .info (f"Deleting (stopping) Strip subscription { subscription_id } " )
402+ if subscription_id is None :
403+ log .error ("subscription_id cannot be None" )
404+ return False
405+ try :
406+ try :
407+ stripe_subscription = stripe .Subscription .retrieve (
408+ subscription_id , stripe_account = connect_account_id
409+ )
410+
411+ stripe .Subscription .delete (
412+ subscription_id , stripe_account = connect_account_id
413+ )
414+ log .debug (f"Subscription deleted ({ subscription_id } )" )
415+
416+ # instead of inserting cancel directly into the db,
417+ # it refresh status after cancellation
418+ update_stripe_subscription_statuses ()
419+ flash ("Subscription cancelled" )
420+
421+ except stripe ._error .InvalidRequestError as e :
422+ if e .code == "resource_missing" :
423+ msg = (
424+ "stripe subscription id: "
425+ f"{ subscription_id } does not exist "
426+ f"for account { connect_account_id } . "
427+ "perhaps show owner has switched from test mode "
428+ "to live mode and has a new stripe connect account"
429+ )
430+ log .warning (msg )
431+ subscription .stripe_status = "resource_missing"
432+ database .session .commit ()
433+ msg = f"Error could not retrieve subscription ({ subscription_id } ) to stop (is it deleted already?)" # noqa: E501
434+ log .error (f"{ msg } . { e } " )
435+ return False
436+ except Exception as e :
437+ msg = f"Error deleting subscription ({ subscription_id } )"
438+ log .error (f"{ msg } . { e } " )
439+ raise
440+
441+
387442@background_task
388443def do_pause_stripe_subscription_payment_collection (
389444 subscription_id , pause_collection_behavior = "keep_as_draft" , app = None
@@ -481,6 +536,24 @@ def do_pause_all_stripe_subscriptions(app=None):
481536 f"Error trying to pause subscription { subscription .uuid } . Error: { e } " # noqa: E501
482537 )
483538
539+ @background_task
540+ def do_stop_all_stripe_subscriptions (app = None ):
541+ # For each Subscription object, get it's Stripe subscription
542+ # object and stop it using stop_stripe_subscription.
543+ with app .app_context ():
544+ subscriptions = Subscription .query .all ()
545+ for subscription in subscriptions :
546+ log .debug (f"Attempting to stop subscription { subscription .uuid } " )
547+ stripe_subscription_id = subscription .stripe_subscription_id
548+ try :
549+ do_delete_stripe_subscription (
550+ stripe_subscription_id , app = current_app
551+ )
552+ except Exception as e :
553+ log .error (
554+ f"Error trying to stop subscription { subscription .uuid } . Error: { e } " # noqa: E501
555+ )
556+
484557
485558@admin .route ("/stripe/subscriptions/<subscription_id>/actions/pause" )
486559@login_required
@@ -521,6 +594,14 @@ def pause_all_subscribers_subscriptions():
521594 return """All payment collections are being paused in the background. You can move away from this page.""" # noqa: E501
522595
523596
597+ @admin .route ("/stripe/subscriptions/all/actions/stop" )
598+ @login_required
599+ def stop_all_subscribers_subscriptions ():
600+ """Bulk action to stop all subscriptions in the shop"""
601+ do_stop_all_stripe_subscriptions () # Background task
602+ return """All subscriptions are being stopped in the background. You can move away from this page.""" # noqa: E501
603+
604+
524605@admin .route ("/stripe/subscriptions/<subscription_id>/actions/resume" )
525606@login_required
526607def resume_stripe_subscription (subscription_id ):
0 commit comments