/    Sign up×
Community /Pin to ProfileBookmark

Superglobal variable not using session

Hi,
I’m trying to integrate paypal to my shopping cart, and everything is working perfectly until I receive the payment confirmation from paypal.
I am going to handle the shipping since this webpage is not in the US, so I wanted the user to select the shipping address before they go to paypal to pay.
After the payment is confirmed, I wanted to make a database entry with the details paypal sends me, along with the shipping details the customer selected before going to paypal.
I’ve tried SESSION variables, but these don’t seem to work, I think it’s because the .php file I use to process the “paypal POST variables” is not initialized when paypal sends them.
Does someone know another way of doing this? Is there some other kind of superglobal I can use?

Hope someone can help…

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscDec 03.2010 — You cannot use session variables - PayPal's system sends an IPN to a script, the session it will initialize in the script that receives the IPN is singular and not related to anything else.

What you need to do is 1 of 2 things. I prefer the second method:

1) Make use of PayPal's "custom" field. You can pass a field to paypal named custom and have it's value returned to you via the IPN

2) As soon as the buyer clicks the buy button make the database entry using AJAX, but just flag it as an "unconfirmed" order (e.g. by setting some field in the database record - I usually store PayPals transactionid with a record in the database so if the transactionid field is null, then I know that order was never completed). That way, you can store whatever information you want before the buyer even pays. Then, when your script receives PayPals IPN, update the field in the database associated with that record to flag it as a confirmed order and, voila, you've already got the shipping details in that record (no thanks to PayPal).

EDIT: You have to be inventive when it comes to programming. The operations that are performed in the IPN-processing script are completely separate from any variable (superglobal or not) that you're buyer might have come across while checking out.

All that being said, you could just set no_shipping to 2 which requires buyers to enter a shipping address. I'd imagine the shipping address probably gets returned to you via the IPN as well.
Copy linkTweet thisAlerts:
@boloydeauthorDec 03.2010 — Thanks aj,

I thought about the no_shipping set to 2, but I already have address books for my customers. Making them insert their address' every time they order something is a nuisance which I wanted to eliminate.

Not sure how to use AJAX...
Copy linkTweet thisAlerts:
@aj_nscDec 03.2010 — Well, you don't HAVE to use AJAX, but if you're versed enough in knowing that you have to process an IPN with a script and use that script to insert info into a database, then you can just skip the AJAX part.

Right now, your form likely submits directly to PayPal - what you want to do is have your form submit to a script on your site that will process the information, create the database record (again flagged as not completed) and then submit to PayPal using a redirect. To the buyer, it will just look like a normal checkout process with a fraction of a second longer time to get to the PayPal website.
Copy linkTweet thisAlerts:
@boloydeauthorDec 03.2010 — Yeah, that's what I thought I should do.

Well thanks aj...
Copy linkTweet thisAlerts:
@boloydeauthorDec 04.2010 — Okay, I tried this, and the SQL part works perfectly, the only problem I'm having is with the $_POST variables I want to send to paypal.

How do I create a separate script in which the variables are redirected to paypal?

Here's the code I created...

[code=php]
$deli_query = "INSERT INTO orders (firstname, lastname, email, country, address, address2, city, zip_code, state, status, amount, paypal_trans_id, dir_id, created_at, payment) VALUES ('-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', 'NOW()', 'pending');
mysql_query($deli_query) or die(mysql_error());

header("Location: https://www.sandbox.paypal.com/cgi-bin/webscr");[/code]


I replaced the actual values with '-' to make it shorter, but that part works fine, it's the redirecting the $_POST variables to paypal, I'm worried about.
Copy linkTweet thisAlerts:
@aj_nscDec 04.2010 — You can use Javascript in one of two ways. (1) AJAX (see below) or (2), have the script that contains the SQL (the one you posted) build out a blank second page with a hidden form that looks identical to your first form (except its hidden and it's action now points to https://www.sandbox.paypal.com/cgi-bin/webscr) and then use this:

<i>
</i>&lt;body onload="document.getElementById('myform').submit()"&gt;


This is why I tend to use AJAX. When the form "submit" button is clicked, I stop the form from being submitted, send all my SQL to a script via AJAX, have it entered into the database and then return a unique invoice number. Then I append to the invoice number to the form and then resubmit the form using Javascript, this time skipping the validation/AJAX and just allowing it to be submitted normally. Works like a treat, too!

Don't get nervous when I use the term AJAX, it's really not that difficult at all. Just a tiny bit of Javascript, and then you obviously already know how to do the complicated part - the PHP/SQL.

If you think you'd be out of your comfort zone with it, then check out the thread on using cURL
Copy linkTweet thisAlerts:
@boloydeauthorDec 06.2010 — I do plan on using AJAX in the near future, just not enough time to do it now, before I the website's due date.

But once again, a million thanks aj, you're a life saver.
×

Success!

Help @boloyde spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...