0

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);
  • Are you the author of the plugin in question? – bosco Sep 18 '18 at 16:01
  • I am not the author of the plugin. I am just overwriting some of its functions (like the above) since it doesn't work the way it should. – user3662779 Sep 20 '18 at 7:39
0

Sometimes the user login cache can get in the way, you could try adding this:

wp_cache_delete($user_id, 'users');
$user = new WP_User($user_id);
wp_cache_delete($user->user_login, 'userlogins');
  • I did something similar to your advice (see UPDATE 2 above; I clean all user cache, log them in, and then update all the user cache), but it made no difference whatsoever. The user still is not logged in on the next page. No idea what else I can try. – user3662779 Sep 17 '18 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.