php - Paypal IPN Script not inserting into database -
i send paypal ipn simulator php script on external host. script code follows:
<?php error_reporting(-1); ini_set('display_errors', 'on'); // step 1: read post data // reading posted data directly $_post causes serialization issues array data in post. // instead, read raw post data input stream. $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) $mypost[$keyval[0]] = urldecode($keyval[1]); } // read ipn message sent paypal , prepend 'cmd=_notify-validate' $req = 'cmd=_notify-validate'; 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"; } // step 2: post ipn data paypal validate $ch = curl_init('https://www.paypal.com/cgi-bin/webscr'); 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_ssl_verifypeer, 1); curl_setopt($ch, curlopt_ssl_verifyhost, 2); curl_setopt($ch, curlopt_forbid_reuse, 1); curl_setopt($ch, curlopt_httpheader, array('connection: close')); // in wamp-like environments not come bundled root authority certificates, // please download 'cacert.pem' "http://curl.haxx.se/docs/caextract.html" , set // directory path of certificate shown below: // curl_setopt($ch, curlopt_cainfo, dirname(__file__) . '/cacert.pem'); if( !($res = curl_exec($ch)) ) { // error_log("got " . curl_error($ch) . " when processing ipn data"); curl_close($ch); exit; } curl_close($ch); // step 3: inspect ipn validation result , act accordingly if (strcmp ($res, "verified") == 0) { // ipn verified, process it: // check whether payment_status completed // check txn_id has not been processed // check receiver_email primary paypal email // check payment_amount/payment_currency correct // process notification // assign posted variables local variables $item_name = $_post['item_name1']; $item_number = $_post['item_number1']; $payment_status = $_post['payment_status']; $payment_amount = $_post['mc_gross1']; $payment_currency = $_post['mc_currency']; $txn_id = $_post['txn_id']; $receiver_email = $_post['receiver_email']; $payer_email = $_post['payer_email']; // ipn message values depend upon type of notification sent. // loop through &_post array , print nv pairs screen: foreach($_post $key => $value) { echo $key." = ". $value."<br>"; } // connect db mysql_connect('host','user','pass'); mysql_select_db('dbname'); // query mysql_query("insert ipnlogs (item_name) values ('$item_name')"); } else if (strcmp ($res, "invalid") == 0) { // ipn invalid, log manual investigation echo "the response ipn was: <b>" .$res ."</b>"; } /*mysql_connect('host','user','pass'); $result = mysql_query('show tables dbname '); while ($row = mysql_fetch_row($result)) { echo "table: {$row[0]}\n"; }*/ ?>
there no issues connecting database, , database table exists , inserted test query - insert ipnlogs (item_name) values ('test item')
.
i'd note commented out code @ end printed table: ipnlogs \n
i'll keep question made same silly mistake - url should post paypal verify should different if you're testing sandbox simulator:
for live ipn: $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
for ipn simulator $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
Comments
Post a Comment