PayPayl PDT PHP

Hello,
I am trying to extract a buyer's email address from a completed Paypal transaction. I have PDT enabled so the buyer is returned to my site. I have successfully been able to extract the transaction number and status BUT I can't figure out how to extract the email address. I need a way to link their information to the transaction to my MySQL database.

The URL contains the tx and st variables but none of the echo or print commands work to be displayed on the page. I never get Success or Failed to show up.

My server is running PHP 5.2.6.


<?php
$pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "RIXnjUkfcG4DUNpvjXz REDACTED kOHBgvhTnbB3GrYQOVRkS";
$req .= '&tx=' .$tx_token. '&at=' .$auth_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);
curl_close($ch);
if(!$res){
    //HTTP ERROR
}else{
     // parse the data
    $lines = explode("\n", $res);
    $keyarray = array();
    if (strcmp ($lines[0], "SUCCESS") == 0) {
        for ($i=1; $i<count($lines);$i++){
        list($key,$val) = explode("=", $lines[$i]);
        $keyarray[urldecode($key)] = urldecode($val);
    }

    }
    else if (strcmp ($lines[0], "FAIL") == 0) {
        // log for manual investigation
    }
}
 
?>

Open in new window

jj1103Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This looks more like the IPN script, can you confirm if that is the case?  Also, you will want to do something about PHP 5.2.  It's not supported any more, not even for security fixes, so it's time to upgrade.
0
jj1103Author Commented:
Shouldn't 5.2 work with PayPal's php script?  I was hesitant to upgrade because there were several posts of folks that upgraded and their paypal script stopped working.

I just need a way to extract the buyer's email. Can I combine the PDT and IPN on the same page?
0
Ray PaseurCommented:
There may be other factors that will matter when you upgrade the PHP version, but I don't think it will make a difference to this script.  You might want to run phpinfo() and look for things like magic quotes and register globals.

I'm looking at this page:
https://www.paypal.com/cgi-bin/webscr?cmd=p/xcl/rec/pdt-techview-outside

I think the first thing we want to do is find out whether the cURL process is working, and what information is coming back (if any) from the cURL request.  Let's try something like this script (put your credentials in the right places).  The var_dump() function will let us see what's getting sent by PayPal.

<?php

// MAKE SURE WE CAN SEE ALL THE ERRORS
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

$pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "RIXnjUkfcG4DUNpvjXz REDACTED kOHBgvhTnbB3GrYQOVRkS";
$req .= '&tx=' .$tx_token. '&at=' .$auth_token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);

// ON FAILURE HANDLE ERROR MESSAGE
if ($res === FALSE)
{
    $err = curl_errno($ch);
    $inf = curl_getinfo($ch);
    echo "CURL FAIL: CURL_ERRNO=$err";
    var_dump($inf);
}

curl_close($ch);

// SHOW WHAT WE GOT BACK, IF ANYTHING
var_dump($res);

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Ray PaseurCommented:
Can I combine the PDT and IPN on the same page?
I don't think so; I believe these scripts have different inputs and different functions.  The PDT documentation link above suggests that you need different scripts.  For example, the PDT appears to be a synchronous web-page script that is started with a GET-method request, whereas I believe that the IPN is started with POST.

A GET-method script is an information-only script.  It violates the laws of HTTP if a GET request causes any change in the data model.  The IPN is allowed to update the data model (data base, log files, etc).  See this page for more.
https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNIntro/
0
jj1103Author Commented:
Ray_Paseur, I get this error message when I pasted your code:

CURL FAIL: CURL_ERRNO=60array(19) { ["url"]=> string(45) "https://www.sandbox.paypal.com/cgi-bin/webscr" ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0.232) ["connect_time"]=> float(0.275) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) } bool(false)
0
jj1103Author Commented:
I'm not using an SSL yet.. I will be once I get this working.  I noticed the script referenced an SSL.
0
Ray PaseurCommented:
Good!  We have something to start debugging.  Here are the cURL error codes.  It looks like the issue lies in the use or non-use of SSL.
http://curl.haxx.se/libcurl/c/libcurl-errors.html

This is just a guess, but you might try removing the lines that say this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

Open in new window

0
jj1103Author Commented:
OK... we are getting places!  I changed this line to:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

and now I have success:

string(940) "SUCCESS mc_gross=0.01 protection_eligibility=Ineligible address_status=confirmed payer_id=DZ9X2GD3NFVAL tax=0.00 address_street=test payment_date=07%3A32%3A19+Sep+26%2C+2013+PDT payment_status=Completed charset=windows-1252 address_zip=20619 first_name=Joanna mc_fee=0.01 address_country_code=US address_name=J+W custom= payer_status=unverified business=admin%40races.com address_country=United+States address_city=California quantity=1 payer_email=J5483%40hotmail.com txn_id=2AV47409XL010174U payment_type=instant btn_id=2826462 last_name=Ws address_state=CA receiver_email=admin%40races.com payment_fee=0.01 shipping_discount=0.00 insurance_amount=0.00 receiver_id=MAMQVLRQ8H5KY txn_type=web_accept item_name=Sample+Buy+Now+Button discount=0.00 mc_currency=USD item_number= residence_country=US shipping_method=Default handling_amount=0.00 transaction_subject= payment_gross=0.01 shipping=0.00 "
0
Ray PaseurCommented:
Yep, looks like you've got "payer_email" in there.

Try taking that string and doing this...

// SHOW WHAT WE GOT BACK, IF ANYTHING
$arr = explode(PHP_EOL, $res);
foreach ($arr as $val)
{
    print_r($val);
}
var_dump($arr);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jj1103Author Commented:
That worked well! I tried this to extract the specific info and it gave me an error: Notice: Undefined offset: 1 in line 67


...
$res = curl_exec($ch);
curl_close($ch);

if(!$res){
    echo "Error!";
}else{
     // parse the data
    $lines = explode("\n", $res);
    $keyarray = array();
    if (strcmp ($lines[0], "SUCCESS") == 0) {
        for ($i=1; $i<count($lines);$i++){
        list($key,$val) = explode("=", $lines[$i]);  //THIS IS LINE 67
        $keyarray[urldecode($key)] = urldecode($val);
    }
 
    $firstname = $keyarray['first_name'];
    $lastname = $keyarray['last_name'];
    $itemname = $keyarray['item_name'];
    $paymentstatus = $keyarray['payment_status'];
	$payer_email = $keyarray['payer_email'];

    echo ("<p><h3>Thank you for your purchase!</h3></p>");

    echo ("<b>Payment Details</b><br>\n");
    echo ("<li>Name: $firstname $lastname</li>\n");
    echo ("<li>Item: $itemname</li>\n");
    echo ("<li>Amount: $paymentstatus</li>\n");
	echo ("<li>Payer Email: $payer_email</li>\n");
    echo ("");
    }
    else if (strcmp ($lines[0], "FAIL") == 0) {
        // log for manual investigation
    }
}

Open in new window

0
jj1103Author Commented:
Ray_Paseur! YOU ROCK!
0
Ray PaseurCommented:
Cool!  Glad you got it working, and thanks for the points, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
E-Commerce

From novice to tech pro — start learning today.