php - Official PayPal IPN Issue -
after bit of googling noticed paypal using new ipn class got github: https://github.com/paypal/ipn-code-samples
i installed it, , started passing post variables it:
paypalipn.php
<?php class paypalipn { /** * @var bool $use_sandbox indicates if sandbox endpoint used. */ private $use_sandbox = true; /** * @var bool $use_local_certs indicates if local certificates used. */ private $use_local_certs = true; /** production postback url */ const verify_uri = 'https://ipnpb.paypal.com/cgi-bin/webscr'; /** sandbox postback url */ const sandbox_verify_uri = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr'; /** response paypal indicating validation successful */ const valid = 'verified'; /** response paypal indicating validation failed */ const invalid = 'invalid'; /** * sets ipn verification sandbox mode (for use when testing, * should not enabled in production). * @return void */ public function usesandbox() { $this->use_sandbox = true; } /** * sets curl use php curl's built in certs (may required in * environments). * @return void */ public function usephpcerts() { $this->use_local_certs = false; } /** * determine endpoint post verification data to. * @return string */ public function getpaypaluri() { if ($this->use_sandbox) { return self::sandbox_verify_uri; } else { return self::verify_uri; } } /** * verification function * sends incoming post data paypal using curl library. * * @return bool * @throws exception */ public function verifyipn() { if (!count($_post)) { throw new exception("missing post data"); } $raw_post_data = file_get_contents('php://input'); $raw_post_array = explode('&', $raw_post_data); $mypost = array(); foreach ($raw_post_array $keyval) { $keyval = explode('=', $keyval); if (count($keyval) == 2) { // since not want plus in datetime string encoded space, manually encode it. if ($keyval[0] === 'payment_date') { if (substr_count($keyval[1], '+') === 1) { $keyval[1] = str_replace('+', '%2b', $keyval[1]); } } $mypost[$keyval[0]] = urldecode($keyval[1]); } } // build body of verification post request, adding _notify-validate command. $req = 'cmd=_notify-validate'; $get_magic_quotes_exists = false; if (function_exists('get_magic_quotes_gpc')) { $get_magic_quotes_exists = true; } foreach ($mypost $key => $value) { if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { $value = urlencode(stripslashes($value)); } else { $value = urlencode($value); } $req .= "&$key=$value"; } print "<pre>"; print_r($req); print "</pre>"; // post data paypal, using curl. throw exceptions if errors occur. $ch = curl_init($this->getpaypaluri()); curl_setopt($ch, curlopt_http_version, curl_http_version_1_1); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_postfields, $req); curl_setopt($ch, curlopt_sslversion, 6); curl_setopt($ch, curlopt_ssl_verifypeer, 1); curl_setopt($ch, curlopt_ssl_verifyhost, 2); // required if server missing global cert bundle, or using outdated one. if ($this->use_local_certs) { curl_setopt($ch, curlopt_cainfo, __dir__ . "/cert/cacert.pem"); } curl_setopt($ch, curlopt_forbid_reuse, 1); curl_setopt($ch, curlopt_connecttimeout, 30); curl_setopt($ch, curlopt_httpheader, array('connection: close')); $res = curl_exec($ch); print_r($res); if (!($res)) { $errno = curl_errno($ch); $errstr = curl_error($ch); curl_close($ch); throw new exception("curl error: [{$errno}] [{$errstr}]"); } $info = curl_getinfo($ch); $http_code = $info['http_code']; if ($http_code != 200) { throw new exception("paypal responded http code {$http_code}"); } curl_close($ch); // check if paypal verifies ipn data, , if so, return true. if ($res == self::valid) { return true; } else { return false; } } } ?>
to access , test i'm doing:
<?php namespace listener; ini_set('display_errors', 1); error_reporting(-1); require('paypalipn.php'); use \paypalipn; $ipn = new paypalipn(); // use sandbox endpoint during testing. $ipn->usesandbox(); $verified = $ipn->verifyipn(); if ($verified) { print("verified"); exit(0); /* * process ipn * list of variables available here: * https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/ipnandpdtvariables/ */ } // reply empty 200 response indicate paypal ipn received correctly. header("http/1.1 200 ok"); ?>
on testing getting curl errors thrown:
fatal error: uncaught exception: curl error: [0] [] in /home/admin/web/syte.com.com/public_html/gateway/paypalipn.php:127 stack trace: #0 /home/admin/web/syte.com/public_html/gateway/paypal.php(13): paypalipn->verifyipn() #1 {main} thrown in /home/admin/web/syte.com/public_html/gateway/paypalipn.php on line 127
on researching, error code "0" not error seems, means went ok (from have read)
does see glaring errors in code @ all? few threads on other forums have seen, there no posted solution, help.
Comments
Post a Comment