Created
July 23, 2026 07:48
-
-
Save xlplugins/196982da40431c19893b6170269ed71d to your computer and use it in GitHub Desktop.
Cancel WooPayment (Stripe Billing ) when sublium subscription Cancelled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Snippet: Cancel the linked WooPayments/Stripe subscription when a Sublium | |
| * subscription is fully cancelled (status 9). | |
| * | |
| * mu-plugin — loads automatically, no activation needed. Standalone — does | |
| * not depend on Sublium WCS Manager. | |
| * | |
| * Requires: WooCommerce Payments (WooPayments) active. The Stripe subscription | |
| * ID is read directly from the Sublium subscription's own meta | |
| * (`_wcpay_subscription_id`). | |
| */ | |
| if ( ! class_exists( 'SWM_Cancel_Stripe_On_Sublium_Cancel' ) ) : | |
| class SWM_Cancel_Stripe_On_Sublium_Cancel { | |
| private static ?self $instance = null; | |
| public static function get_instance(): self { | |
| if ( null === self::$instance ) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| private function __construct() {} | |
| public function init(): void { | |
| add_action( 'sublium_wcs_subscription_status_updated_cancelled', array( $this, 'on_sublium_subscription_cancelled' ), 10, 1 ); | |
| } | |
| /** | |
| * @param object $subscription Sublium_WCS\Includes\Controller\Subscriptions\Subscription instance. | |
| */ | |
| public function on_sublium_subscription_cancelled( $subscription ): void { | |
| if ( ! is_object( $subscription ) || ! method_exists( $subscription, 'get_id' ) || ! method_exists( $subscription, 'get_meta' ) ) { | |
| return; | |
| } | |
| if ( ! $this->wcpay_is_active() ) { | |
| return; | |
| } | |
| $sublium_sub_id = absint( $subscription->get_id() ); | |
| if ( ! $sublium_sub_id ) { | |
| return; | |
| } | |
| $stripe_sub_id = $subscription->get_meta( '_wcpay_subscription_id' ); | |
| if ( ! is_string( $stripe_sub_id ) || ! str_starts_with( $stripe_sub_id, 'sub_' ) ) { | |
| return; // No linked Stripe subscription found. | |
| } | |
| $client = $this->get_wcpay_client(); | |
| try { | |
| $sub = $client->get_subscription( $stripe_sub_id ); | |
| } catch ( Exception $e ) { | |
| $this->log( 'warning', "auto-cancel (lookup) for Sublium sub #{$sublium_sub_id}: " . $e->getMessage() ); | |
| return; | |
| } | |
| if ( ( $sub['status'] ?? '' ) === 'canceled' ) { | |
| return; // Already cancelled — nothing to do. | |
| } | |
| try { | |
| $client->cancel_subscription( $stripe_sub_id ); | |
| $this->log( 'info', "Stripe subscription {$stripe_sub_id} cancelled (Sublium sub #{$sublium_sub_id} cancelled)." ); | |
| } catch ( Exception $e ) { | |
| $this->log( 'error', "failed to cancel Stripe subscription {$stripe_sub_id} for Sublium sub #{$sublium_sub_id}: " . $e->getMessage() ); | |
| } | |
| } | |
| private function wcpay_is_active(): bool { | |
| return class_exists( 'WC_Payments' ) && method_exists( 'WC_Payments', 'get_payments_api_client' ); | |
| } | |
| private function get_wcpay_client(): object { | |
| return WC_Payments::get_payments_api_client(); | |
| } | |
| private function log( string $level, string $message ): void { | |
| if ( function_exists( 'wc_get_logger' ) ) { | |
| wc_get_logger()->log( $level, 'SWM snippet: ' . $message, array( 'source' => 'swm-snippet' ) ); | |
| } | |
| } | |
| } | |
| endif; | |
| SWM_Cancel_Stripe_On_Sublium_Cancel::get_instance()->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment