I'm currently using the Paypal REST API to process billing agreements for users with multiple plans.
When a user signs up for a plan, it takes them to PayPal to pay. After they finish that step, PayPal redirects them to a return URL that I have supplied.
The problem is, sometimes the user's session does not persist when they return from PayPal!
I have session_start()
as the first line in all of my files, so the sessions are being created. I have verified, before taking the user to PayPal, that the $_SESSION
array is being populated - it's just when they are brought back after PayPal.
Is there a better way to persist data like that so I can update a users account on return back from PayPal? Or maybe there is a way to pass the email with the PayPalrequest so that it is returned in the object they send back to my website on return?
If you're properly starting the session via session_start()
on both your sign-up page and the page that PayPal redirects back to, your session should be starting properly.
Assuming that you're not inadvertently closing the session, or regenerating the ID, it sounds like there isn't an actual "session management" issue, but perhaps it has to do with the URL that PayPal is redirecting back to.
A common issue with PHP sessions is that, with a default PHP config, they don't carry between subdomains.
For example, if I visit your domain domain at example.com
, my session will only be active on example.com
. If I then go to www.example.com
, I will receive a new session.
You can verify this by going to your site at example.com
or www.example.com
and checking what domain the PHPSESSID
cookie is set for. If it is not .example.com
(note the leading .
), then this is the issue =]
To help resolve this, you can modify your server's config to set the .example.com
as the cookie's domain. Taken from this answer:
session.cookie_domain = ".example.com"
The same answer I gave to a recent question should work here as well:
The solution I used for this same problem was to set
override_merchant_preferences
on the billing agreement, with thereturn_url
containing the user id in the query, likewww.domain.com/api/handler.php?uid=42&action=return
.
Though of course you can pass any identifiers you'd like through the $_GET
that way.