Process paypal response


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. define('PAYPAL_TOKEN', '123456981723dasdas_paypal_token');
  2. define('PAYPAL_URL', 'https://www.paypal.com/cgi-bin/webscr');
  3.  
  4. $tx = (isset($_GET['tx'])) ? $_GET['tx'] : '';
  5.  
  6. // Send POST for data response
  7. $ch = curl_init(PAYPAL_URL);
  8. curl_setopt($ch, CURLOPT_POST, 1);
  9. curl_setopt($ch, CURLOPT_POSTFIELDS,'cmd=_notify-synch&tx='.$tx.'&at='.PAYPAL_TOKEN);
  10. curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL
  12. $res = curl_exec($ch);
  13.  
  14. // Log response
  15. $fp = fopen(PATH.'/log/paypal.log', 'a');
  16. fwrite($fp, "\n".date('d.m.Y H:i')."\n");
  17. fwrite($fp, $res);
  18. fclose($fp);
  19.  
  20. // Parse response
  21. $lines = explode("\n", $res);
  22. $processed = (strcmp($lines[0], "SUCCESS") == 0) ? true : false;
  23. unset($lines[0]);
  24. unset($lines[30]);
  25. $keyarray = array();
  26. foreach ($lines as $line)
  27. {
  28. list($key,$val) = explode("=", $line);
  29. $keyarray[urldecode($key)] = urldecode($val);
  30. }
  31.  
  32. if ($processed && $keyarray['payment_status'] == 'Completed') {
  33. echo "OK";
  34. } else {
  35. echo "Something went wrong.<br /> PayPal response: <br />".$res;
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.