From 1814e5ade0e0a896777bf0e529240dc8fcad5f60 Mon Sep 17 00:00:00 2001 From: David Parker Date: Fri, 25 Oct 2024 09:33:12 -0400 Subject: [PATCH] Updated Stripe cancelSubscriptionAtGateway for subs table --- .../gateways/class.pmprogateway_stripe.php | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/classes/gateways/class.pmprogateway_stripe.php b/classes/gateways/class.pmprogateway_stripe.php index b490b5272..124662a4b 100644 --- a/classes/gateways/class.pmprogateway_stripe.php +++ b/classes/gateways/class.pmprogateway_stripe.php @@ -3369,25 +3369,14 @@ private function cancelSubscriptionAtGateway( $subscription, $preserve_local_mem return false; } - // If this is already cancelled, return true. - if ( ! empty( $subscription->canceled_at ) ) { - return true; - } - - // Make sure we get the customer for this subscription. - $order = new MemberOrder(); - $order->getLastMemberOrderBySubscriptionTransactionID( $subscription->id ); - - // No order? - if ( empty( $order ) ) { - //lets cancel anyway, but this is suspicious - $r = $subscription->cancel(); - - return true; + // Get the PMPro subscription. + $pmpro_subscription = PMPro_Subscription::get_subscription_from_subscription_transaction_id( $subscription->id, 'stripe', get_option( 'pmpro_gateway_environment', 'sandbox' ) ); + if ( empty( $pmpro_subscription ) ) { + return false; } // Okay have an order, so get customer so we can cancel invoices too - $customer = $this->update_customer_at_checkout( $order ); + $customer = $this->get_customer_for_user( $pmpro_subscription->get_user_id() ); // Get open invoices. $invoices = Stripe_Invoice::all(['customer' => $customer->id, 'status' => 'open']); @@ -3405,12 +3394,11 @@ private function cancelSubscriptionAtGateway( $subscription, $preserve_local_mem // Sometimes we don't want to cancel the local membership when Stripe sends its webhook. if ( $preserve_local_membership ) { - $this->ignoreCancelWebhookForThisSubscription( $subscription->id, $order->user_id ); + $this->ignoreCancelWebhookForThisSubscription( $subscription->id, $pmpro_subscription->get_user_id() ); } // Cancel $r = $subscription->cancel(); - return true; } catch ( \Throwable $e ) { return false;