Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Stripe cancelSubscriptionAtGateway for subs table #3178

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions classes/gateways/class.pmprogateway_stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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;
Expand Down