PayPal Single does not return logged in user id after payment

I'm using Membership free plugin on staging website with Paypal Single Gateway Sandbox mode of this membership plugin.
I want to get user id (logged in user id who purchased the membership) after user complete payment on paypal.
I'm using action hook ms_gateway_paypalsingle_payment_processed_pending which works after user pay on paypal but it return wrong user Id (always same user ID). I think this same user ID connected to that membership as I can see on Membership -> Billing page at my wp backend but it always shows same and don't return the latest/exact user ID who purchased the membership.

only logged in users can buy membership on my website.
I want only 2 things.
1: User ID with action hook in my plugin who purchased that membership so I can implement further functionality for that user.
2: The membership name/slug user purchased.
I want only these 2 values after user paid payment on paypal.

So for I've used both parameters $invoice and $subscription of ms_gateway_paypalsingle_payment_processed_pending do_action hook but I can't get user ID who purchased the membership.

Also note that get_current_user_id() function only return 0 on ms_gateway_paypalsingle_payment_processed_pending hook so can you do something like if you send logged in user ID who purchased the membership as hidden parameters and then return that User ID when response from paypal come?

  • Panos

    Hi Rohit ,

    Memberships are assigned to users so users need to be registered in order to have a subscription for a membership. So the invoice ad subscription objects that are passed as arguments in that action hook can be used to retrieve both, user id and membership.

    You could try this:

    add_action( 'ms_gateway_paypalsingle_payment_processed_pending', function( $invoice, $subscription ){
    	$member = $subscription->get_member();
    	// You can get user id with:
    	//$user_id = $member->id;
    	$membership = $subscription->get_membership();
    	//You can get membership name with :
    	//$membership_name = $membership->name;
    	//$membership_name = $membership->title;
    }, 20, 2 );

    In above snippet we use the $subscription object to fetch $member and $membership. In the comments it demonstrates how to get the user id from member, and membership name from membership.

    One note, this is for pending status and I see that when payment is completed, the $status variable is not updated to paid and you can't use the ms_gateway_paypalsingle_payment_processed_paid action.

    In case you prefer to use the ms_gateway_paypalsingle_payment_processed_paid action, I have attached file:
    which should provide this action.

    I would strongly recommend to keep a backup of original file before replacing it. You can simply rename it to class-ms-gateway-paypalsingle.php.txt

    Hope this helps :slight_smile:

  • Rohit

    Hi Panos, thanks.

    Yes that hook ms_gateway_paypalsingle_payment_processed_pending worked fine.

    But I was trying that clicking on membership button should go directly to paypal.
    Default button goes to another page and then again clicking it go to paypal which is not much user friendly.

    Please see here 2 buttons for each membership.
    Top button (copied html from 2nd page) goes directly to paypal and bottom button go another page and then to paypal which is not much user friendly.

    When I try with native button, I'm able to get user id in ms_gateway_paypalsingle_payment_processed_pending hook but if I use custom html of that membership button from 2nd page link, I don't get latest logged in user who paid the payment but it return the Id of user at which time I copied the html from 2nd page.

    How can I use membership button link which go directly to paypal and it return correct information of logged in user who just paid the payment?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.