Created
April 12, 2012 23:24
-
-
Save dylanized/2371848 to your computer and use it in GitHub Desktop.
Paypal PDT Handling Script, slightly cleaned up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// PayPal PDT code | |
// read the post from PayPal system and add 'cmd' | |
$req = 'cmd=_notify-synch'; | |
$tx_token = $_GET['tx']; | |
$auth_token = "YOUR TOKEN GOES HERE"; | |
$req .= "&tx=$tx_token&at=$auth_token"; | |
// post back to PayPal system to validate | |
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; | |
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; | |
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; | |
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); | |
// If possible, securely post back to paypal using HTTPS | |
// Your PHP server will need to be SSL enabled | |
// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); | |
if (!$fp) { | |
// HTTP ERROR | |
echo("<h3>HTTP Error</h3>"); | |
} else { | |
fputs ($fp, $header . $req); | |
// read the body data | |
$res = ''; | |
$headerdone = false; | |
while (!feof($fp)) { | |
$line = fgets ($fp, 1024); | |
if (strcmp($line, "\r\n") == 0) { | |
// read the header | |
$headerdone = true; | |
} else if ($headerdone) { | |
// header has been read. now read the contents | |
$res .= $line; | |
} | |
} | |
// 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); | |
} | |
// check the payment_status is Completed | |
// check that txn_id has not been previously processed | |
// check that receiver_email is your Primary PayPal email | |
// check that payment_amount/payment_currency are correct | |
// process payment | |
$firstname = $keyarray['first_name']; | |
$lastname = $keyarray['last_name']; | |
$itemname = $keyarray['item_name']; | |
$amount = $keyarray['payment_gross']; | |
$item_id = $keyarray['item_number']; | |
?> | |
<h3>Payment Details</h3> | |
<p> | |
<strong>Name:</strong> <?= $firstname ?> <?= $lastname ?><br /> | |
<strong>Item:</strong> <?= $itemname ?><br /> | |
<strong>ID:</strong> <?= $item_id ?><br /> | |
<strong>Amount:</strong> <?= $amount ?> | |
</p> | |
<?php | |
} else if (strcmp ($lines[0], "FAIL") == 0) { | |
echo("<h3>There was a problem parsing the data</h3>"); | |
// log for manual investigation | |
} | |
} | |
fclose ($fp); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment