subscription instanceof Id ? $this->repo->ofId($cmd->subscription) : $cmd->subscription; $ws = $sub->getWorkspace(); // Stop payments if ($sub->getPaymentGateway()->value) { try { $gateway = $this->factory->create( $sub->getPaymentGateway()->value ); $gateway->cancelSubscription($sub->getExternalId()->value); } catch (PaymentException | GatewayNotFoundException $th) { // Couldn't stop the payments, case must be handled manually } } $sub->cancel(); $activeSub = $ws->getSubscription(); if ( !$activeSub || (string) $activeSub->getId()->getValue() !== (string) $sub->getId()->getValue() ) { // This is not the active subscription, so we can end it here $sub->end(); } return $sub; } }