php - get POST data sent from PayPal to IPN Listener -
i trying pass custom value paypal , have paypal return php ipn listener. code using. original code published on github. made small modification: added fuction getpostdata()
post data sent paypal, getpostdata()
returns empty string. how can access post data paypal sends me?
this how anchor element looks:
<a id="submit-button" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=someid&custom=customdata">pay paypal</a>
other code:
<?php class paypalipn { private $use_sandbox = false; private $postdata = ""; private $use_local_certs = true; /* * paypal ipn postback endpoints */ const verify_uri = 'https://ipnpb.paypal.com/cgi-bin/webscr'; const sandbox_verify_uri = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr'; /* * possible responses paypal after request issued. */ const valid = 'verified'; 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; } /** * 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 = []; 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]); } } $postdata = $raw_post_data; // 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"; } // 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, ['connection: close']); $res = curl_exec($ch); $info = curl_getinfo($ch); $http_code = $info['http_code']; if ($http_code != 200) { throw new exception("paypal responded http code $http_code"); } if ( ! ($res)) { $errno = curl_errno($ch); $errstr = curl_error($ch); curl_close($ch); throw new exception("curl error: [$errno] $errstr"); } curl_close($ch); // check if paypal verfifes ipn data, , if so, return true. if ($res == self::valid) { return true; } else { return false; } } public function getpostdata(){ return $postdata; } }
usage:
<?php require('paypalipn.php'); echo "im in"; require_once 'login.php'; use paypalipn; $ipn = new paypalipn(); // use sandbox endpoint during testing. $verified = $ipn->verifyipn(); if ($verified) { /* * process ipn * list of variables available here: * https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/ipnandpdtvariables/ */ $conn = new mysqli($hn,$un,$pw,$db); if ($conn->connect_error){ die($conn->connect_error); } $lastname = $ipn->getpostdata(); $name = "ipnname"; $email = "ipnemail"; $query = "insert testtable (lastname ,name ,email) values ('$lastname', '$name', '$email')"; $result = $conn->query($query); if (!$result){ die ("query failed"); } } // reply empty 200 response indicate paypal ipn received correctly. header("http/1.1 200 ok");
Comments
Post a Comment