In a plugin, after a Stripe Checkout payment is processed, an account is created, the user is logged in, and then redirected to a "thank you page".
On this page, only about 1/3 to 1/2 of the users is still logged in, even though I confirmed that they were logged in on the previous page before redirecting. I looked at user agents of people who did not stay logged in, and couldn't find a pattern (it happened with Edge, Chrome, Firefox, Safari).
The same process with Paypal works fine.
The Paypal process is: User pays on Paypal, is redirected with GET request to our website, payment is verified with Paypal, then the WP account is created, the user is logged in and redirected.
The Stripe process is: User pays on our website using Stripe Checkout. Stripe token is submitted to this same page using a POST request, which is then processed, then the WP account is created, the user is logged in, and redirected.
In both the Stripe and Paypal functions, users are logged in and redirected like this:
wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id, true ); // here I have confirmed that each time, they are logged in // headers_sent() always returns false, if that's relevant wp_safe_redirect( $url ); exit;
But only Paypal users are always logged in on the next page. Stripe users are not. What can be the cause of the login not "sticking"?
UPDATE: On the "thank you page" I log users' cookies, and even though wp_get_current_user() returns 0, the wordpress_logged_in_[hash] cookie is there. Why/how does this happen?
UPDATE 2: I used majick's advice and advice from this thread to log the user in with the following code. However, this made no difference. Some percentage of users still end up on the next page not being logged in.
clean_user_cache( $user_id ); wp_clear_auth_cookie(); wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id , true ); $user = new WP_User($user_id); update_user_caches($user);