Skip to content

Commit

Permalink
Merge pull request #3177 from dparker1005/get_stripe_customer_from_su…
Browse files Browse the repository at this point in the history
…bs_table

Allow pulling stripe customer ID from subscriptions in subs table
  • Loading branch information
dparker1005 authored Oct 24, 2024
2 parents 9ab71d2 + 7470da6 commit d8004ea
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions classes/gateways/class.pmprogateway_stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -1868,31 +1868,41 @@ public function get_customer_for_user( $user_id ) {
$customer_id = get_user_meta( $user_id, 'pmpro_stripe_customerid', true );

if ( empty( $customer_id ) ) {
// Try to figure out the cuseromer ID from their last order.
$order = new MemberOrder();
$order->getLastMemberOrder(
$user_id,
array(
'success',
'cancelled'
),
null,
'stripe',
$this->gateway_environment
// Try to figure out the customer ID from their subscription.
$subscription_search_params = array(
'user_id' => $user_id,
'status' => 'active',
'gateway' => 'stripe',
);

// If we don't have a customer ID yet, get the Customer ID from their subscription.
if ( empty( $customer_id ) && ! empty( $order->subscription_transaction_id ) && strpos( $order->subscription_transaction_id, "sub_" ) !== false ) {
$subscriptions = PMPro_Subscription::get_subscriptions( $subscription_search_params );
foreach ( $subscriptions as $subscription ) {
try {
$subscription = Stripe_Subscription::retrieve( $order->subscription_transaction_id );
$stripe_subscription = Stripe_Subscription::retrieve( $subscription->get_subscription_transaction_id() );
} catch ( \Throwable $e ) {
// Assume no customer found.
} catch ( \Exception $e ) {
// Assume no customer found.
}
if ( ! empty( $subscription ) && ! empty( $subscription->customer ) ) {
$customer_id = $subscription->customer;
if ( ! empty( $stripe_subscription ) && ! empty( $stripe_subscription->customer ) ) {
$customer_id = $stripe_subscription->customer;
break;
}
$stripe_subscription = null;
}

// If we don't have a customer ID yet, try to figure out the cuseromer ID from their last order.
if ( empty( $customer_id ) ) {
$order = new MemberOrder();
$order->getLastMemberOrder(
$user_id,
array(
'success',
'cancelled'
),
null,
'stripe',
$this->gateway_environment
);
}

// If we don't have a customer ID yet, get the Customer ID from their charge.
Expand Down Expand Up @@ -2194,6 +2204,14 @@ public function update_subscription_info( $subscription ) {
$update_array['enddate'] = date( 'Y-m-d H:i:s', intval( $stripe_subscription->ended_at ) );
}
$subscription->set( $update_array );

// Check if the user's Stripe customer ID is the same as the subscription's customer ID.
// We are waiting until after the subscription is created in case the customer didn't have a Stripe customer ID before.
$customer = $this->get_customer_for_user( $subscription->get_user_id() );
if ( ! empty( $customer ) && $customer->id !== $stripe_subscription->customer ) {
// Throw an error.
return __( 'Subscription customer ID does not match user\'s Stripe customer ID.', 'paid-memberships-pro' );
}
}
}

Expand Down

0 comments on commit d8004ea

Please sign in to comment.