airbrushtutor Posted July 12, 2013 Share Posted July 12, 2013 Hi guys - having trouble with the IPN script. have been working on it for about 2 days and have searched the forum but i've come to a dead end. My script is almost exactly the same as the book - all of the file writing is the same, problem i'm having is that it's not adding the order to the DB.. here's my PHP: // This script needs to respond to paypal's IPN to confirm the IPN and update the database. require('includes/config.inc.php'); /******************************************* // Test item to set a price for the script to check. - cost of Dru Blair course - change this before upload. CURRENCY set in config.inc.php $contact_email set in config.inc.php as mitchl@airbrushtutor.com. delete below on successful test. ********************************************/ $contact_email = 'mitchl-facilitator@airbrushtutor.com'; $test_item = 1799.00; // Open a text file - the text file must be writable by PHP! $file = fopen('../ipn.txt', 'a'); // Write the POST data to the file: fwrite($file, "Received:\n"); fwrite($file, print_r($_POST, true)); fwrite($file, "\n"); // Append command validate to the beggining of the response URL to paypal $req = 'cmd=_notify-validate'; // Add each received key=value pair to the request foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; } // Open a socket connection to PayPal. // If connection failed then trigger error if (!$fp) { trigger_error('Could not connect for the IPN!'); } else { // The connection was successful so send the request/ confirmation to paypal $header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; $header .= "Host: www.sandbox.paypal.com\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; fputs($fp, $header . $req); // Write the PayPal request to the text file: fwrite($file, "Sent:\n"); fwrite($file, "$header\n"); fwrite($file, "$req\n"); // Read in the response while (!feof($fp)) { $response = fgets($fp, 1024); // Write the PayPal response to the text file: fwrite($file, "Received:\n"); fwrite($file, "$response\n"); // if the response is verified, then process the response if (strcmp ($response, "VERIFIED") == 0) { // Check for the values posted from Paypal if (isset($_POST['payment_status']) && ($_POST['payment_status'] == 'Completed') && ($_POST['receiver_email'] == $contact_email) && ($_POST['mc_gross'] == $test_item) && ($_POST['mc_currency'] == CURRENCY) && (!empty($_POST['txn_id']))) { // Check for the transaction in the database - in case this is a repeat order. require(MYSQL); $txn_id = mysqli_real_escape_string($dbc, $_POST['txn_id']); $query = "SELECT id FROM orders WHERE transaction_id='$txn_id'"; $result = mysqli_query($dbc, $query); if (mysqli_num_rows($result) == 0) { // The transaction hasn't taken place already so move forward $uid = (isset($_POST['custom'])) ? (int)$_POST['custom'] : 0; $status = mysqli_real_escape_string($dbc, $_POST['payment_status']); $amount = (float)$_POST['mc_gross']; $product_id = (isset($_POST['prod_id'])) ? (int)$_POST['prod_id'] : 0; $query = "INSERT INTO orders (user_id, transaction_id, payment_status, payment_amount, product_id) VALUES ($uid, '$txn_id', '$status', $amount, $product_id)"; $result = mysqli_query($dbc, $query); } // mysqli_num_rows($result) else { // There was a problem inserting the data $err_msg = 'Couldn\'t insert the data to the database?\n'; $err_msg .= $response; trigger_error($err_msg); } } // if isset payment status } // if strcmp($response, "VERIFIED"); elseif (strcmp($response, "UNVERIFIED") == 0) { $err_msg1 = 'The response from paypal was unverified.'; $err_msg1 .= $response; trigger_error($err_msg1); //require an error log to communicate with paypal } } // while feof(); fclose($fp); } // if ($fp); // Inidicate the end of this transaction in the text file: fwrite($file, "--------------\n"); fclose($file); And here's the response we're printing.. Received: Array ( [mc_gross] => 1799.00 [protection_eligibility] => Ineligible [payer_id] => 922YH4PA9T7YE [tax] => 0.00 [payment_date] => 03:20:23 Jul 12, 2013 PDT [payment_status] => Completed [charset] => windows-1252 [first_name] => Sean [mc_fee] => 52.47 [notify_version] => 3.7 [custom] => 5000 [payer_status] => verified [business] => mitchl-facilitator@airbrushtutor.com [quantity] => 1 [verify_sign] => ACtqhM7JEDsuNzaiyzdEn6QdfOeaAlk0RD3wQcEKNrTTKnrBwGTiA.GH [payer_email] => mastercard@airbrushtutor.com [txn_id] => 8D649478MW552273Y [payment_type] => instant [btn_id] => 2793904 [last_name] => Seaman [receiver_email] => mitchl-facilitator@airbrushtutor.com [payment_fee] => 52.47 [shipping_discount] => 0.00 [insurance_amount] => 0.00 [receiver_id] => SEWT5HDKJVNLE [txn_type] => web_accept [item_name] => Dru Blair Portrait 2 [discount] => 0.00 [mc_currency] => USD [item_number] => 8 [residence_country] => US [test_ipn] => 1 [handling_amount] => 0.00 [shipping_method] => Default [transaction_subject] => 5000 [payment_gross] => 1799.00 [shipping] => 0.00 [ipn_track_id] => 6ca82ff4dac16 ) Sent: POST /cgi-bin/webscr HTTP/1.0 Host: www.sandbox.paypal.com Content-Type: application/x-www-form-urlencoded Content-Length: 928 cmd=_notify-validate&mc_gross=1799.00&protection_eligibility=Ineligible&payer_id=922YH4PA9T7YE&tax=0.00&payment_date=03%3A20%3A23+Jul+12%2C+2013+PDT&payment_status=Completed&charset=windows-1252&first_name=Sean&mc_fee=52.47¬ify_version=3.7&custom=5000&payer_status=verified&business=mitchl-facilitator%40airbrushtutor.com&quantity=1&verify_sign=ACtqhM7JEDsuNzaiyzdEn6QdfOeaAlk0RD3wQcEKNrTTKnrBwGTiA.GH&payer_email=mastercard%40airbrushtutor.com&txn_id=8D649478MW552273Y&payment_type=instant&btn_id=2793904&last_name=Seaman&receiver_email=mitchl-facilitator%40airbrushtutor.com&payment_fee=52.47&shipping_discount=0.00&insurance_amount=0.00&receiver_id=SEWT5HDKJVNLE&txn_type=web_accept&item_name=Dru+Blair+Portrait+2&discount=0.00&mc_currency=USD&item_number=8&residence_country=US&test_ipn=1&handling_amount=0.00&shipping_method=Default&transaction_subject=5000&payment_gross=1799.00&shipping=0.00&ipn_track_id=6ca82ff4dac16 Received: HTTP/1.1 200 OK Received: Date: Fri, 12 Jul 2013 10:20:29 GMT Received: Server: Apache Received: X-Frame-Options: SAMEORIGIN Received: Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=f2OfwB8GwOiWiUWvKnQyQ3ZH8gsM6GPmLgUhoJYvPM59gbb9NKy4E8gO2l9aG9PBFz3FFHLN4jPEqxU3gvGMRjV2GixklFEXo-8nwRPNxCgdS_k2MtjfeAQxv1prTa1tMkwA1W%7cpd56RwTeJP9KYj2t4igaX2ZV6ckVUcZ_sLbmIyEng4DCfF5FRYGI2-DvbiDscHY1s2GWwW%7cgRV0QHM3y5s3kVYjzfgRpDTdawpJ8CkuCVw77ph6dKZKMaaB91WLDGQMyWG5Xe-5UtEox0%7c1373624429; domain=.paypal.com; path=/; Secure; HttpOnly Received: Set-Cookie: cookie_check=yes; expires=Mon, 10-Jul-2023 10:20:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly Received: Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly Received: Set-Cookie: navlns=0.0; expires=Thu, 07-Jul-2033 10:20:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly Received: Set-Cookie: Apache=10.72.109.11.1373624429227530; path=/; expires=Sun, 05-Jul-43 10:20:29 GMT Received: Connection: close Received: Content-Type: text/html; charset=UTF-8 Received: Received: VERIFIED --------------Can't work it out - i'm just going through the debug motions and writing the SQL query to the text file to see what that's printing.. If you can offer any help would be appreciated - otherwise i'll post when i find the solution. any help appreciated. Link to comment Share on other sites More sharing options...
airbrushtutor Posted July 12, 2013 Author Share Posted July 12, 2013 Added this code after the if (isset($_POST['payment_status'])) closing bracket: else { // the POST conditions failed $ps = $_POST['payment_status']; $re = $_POST['receiver_email']; $mcgross = $_POST['mc_gross']; $mccurr = $_POST['mc_currency']; $txn_id = $_POST['txn_id']; fwrite($file, "-----------------------------------\n"); fwrite($file, "DEBUG - the post variables failed. Print them here:\n"); fwrite($file, $ps . "\n" . $re . "\n" . $mcgross . "\n" . $mccurr . "\n" . $txn_id . "\n"); fwrite($file, "Currency constant: " . CURRENCY . "\n"); fwrite($file, "contact email: " . $contact_email . "\n"); fwrite($file, "test item: " . $test_item . "\n"); fwrite($file, "-----------------------------------"); } This allowed me to see that the POST conditional was failing. I had three errors: 2nd - I hadn't uploaded the latest local version of all files to the server - so when the post failed I could see that the constant for CURRENCY was just the string CURRENCY - so i uploaded the latest config file and the query went through. This script is working correctly now - and it is updating the SQL orders table successfully. I've added a bit of simple information to the txt file - just so it prints the SQL INSERT statement so i can see what's going on there aswell. all good now - hope this helps someone! Link to comment Share on other sites More sharing options...
Larry Posted July 12, 2013 Share Posted July 12, 2013 Kudos for figuring it out and thanks for sharing your solution! Link to comment Share on other sites More sharing options...
Recommended Posts