diff --git a/includes/integrations/stripe/Billing.php b/includes/integrations/stripe/Billing.php index 4944b70e..9a302931 100644 --- a/includes/integrations/stripe/Billing.php +++ b/includes/integrations/stripe/Billing.php @@ -69,6 +69,7 @@ protected function prepareData( $test_mode, $user, $plan, $returnUrl, $stripe_ac 'plan' => $plan, 'success_url' => $returnUrl, 'cancel_url' => $this->getBillingURL(), + 'user_id' => $user->ID, ); if ( $stripe_account_id ) { diff --git a/includes/integrations/stripe/StripeWebhookController.php b/includes/integrations/stripe/StripeWebhookController.php index 0feb372a..ab94ac3c 100644 --- a/includes/integrations/stripe/StripeWebhookController.php +++ b/includes/integrations/stripe/StripeWebhookController.php @@ -116,6 +116,11 @@ protected function signatureIsValid( $request ) { * @throws \Exception */ protected function handleCheckoutSessionCompleted( $payload ) { + $subscription = $this->subscriptions->where( 'subscription_id', $payload['data']['object']['subscription'] ); + if ( $subscription ) { + return new \WP_REST_Response( 'Webhook handled', 200 ); + } + if ( $payload['data']['object']['customer_email'] ) { $user = get_user_by( 'email', $payload['data']['object']['customer_email'] ); $user_id = $user->ID; @@ -160,8 +165,14 @@ protected function handleCustomerSubscriptionCreated( $payload ) { return new \WP_REST_Response( 'Webhook handled', 200 ); } - $customer_id = $payload['data']['object']['customer']; + // Check if user_id exists in the metadata + if ( isset( $payload['data']['object']['metadata']['user_id'] ) ) { + $user_id = $payload['data']['object']['metadata']['user_id']; + + return $this->createSubscription( $user_id, $payload, false ); + } + $customer_id = $payload['data']['object']['customer']; $subscription = $this->subscriptions->where( 'customer_id', $customer_id ); if ( ! $subscription ) { throw new \Exception( 'Subscription not found' );