From 29445266debcd6954072355bebced04a069fa978 Mon Sep 17 00:00:00 2001 From: Rey Calantaol Date: Mon, 11 Dec 2023 04:20:07 +0800 Subject: [PATCH 1/3] Resolved: The product is not being assigned to the user, but the payment is logged properly in Stripe Resolves https://github.com/WPUserManager/wpum-stripe/issues/9 @polevaultweb --- .../integrations/stripe/StripeWebhookController.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/integrations/stripe/StripeWebhookController.php b/includes/integrations/stripe/StripeWebhookController.php index 0feb372a..74c956b8 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,13 @@ 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' ); From 40ce3562f5b3734b1a87d06318186f5119b586a3 Mon Sep 17 00:00:00 2001 From: Iain Date: Mon, 11 Mar 2024 11:44:32 +0000 Subject: [PATCH 2/3] Send User ID to connect handoff --- includes/integrations/stripe/Billing.php | 1 + 1 file changed, 1 insertion(+) 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 ) { From 7f1809841d5bda32ff08c40f9479616854abf6a8 Mon Sep 17 00:00:00 2001 From: Iain Date: Mon, 11 Mar 2024 11:44:42 +0000 Subject: [PATCH 3/3] Formatting --- includes/integrations/stripe/StripeWebhookController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/integrations/stripe/StripeWebhookController.php b/includes/integrations/stripe/StripeWebhookController.php index 74c956b8..ab94ac3c 100644 --- a/includes/integrations/stripe/StripeWebhookController.php +++ b/includes/integrations/stripe/StripeWebhookController.php @@ -168,6 +168,7 @@ protected function handleCustomerSubscriptionCreated( $payload ) { // 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 ); }