Jump to content

PHP -- Paypal IPN


flarestar

Recommended Posts

I am trying to get a Paypal IPN working using PHP. I have attached the code I have written that is not working. I have no real way to test it other than nothing is being posted to the databases. The transactions seem to complete with paypal, but I get no information and no emails are generated.

 

Any help would be much appreciated.

 

[attachment deleted by admin]

Link to comment
Share on other sites

in the future can you please post your code between [ code ] [ /code ] tags. Thanks.

<?php
  //------------------------------------------------------------------ 
  // Open log file (in append mode) and write the current time into it. 
  // Open the DB Connection. Open the actual database. 
  //------------------------------------------------------------------- 
  $log = fopen("ipn.log", "a");
  fwrite($log, "\n\nipn - " . gmstrftime("%b %d %Y %H:%M:%S", time()) . "\n");
  require("config.php");
  //------------------------------------------------ 
  // Read post from PayPal system and create reply 
  // starting with: 'cmd=_notify-validate'... 
  // then repeating all values sent - VALIDATION. 
  //------------------------------------------------ 
  $postvars = array();
  while (list($key, $value) = each($HTTP_POST_VARS)) {
      $postvars[] = $key;
  }
  $req = 'cmd=_notify-validate';
  for ($var = 0; $var < count($postvars); $var++) {
      $postvar_key = $postvars[$var];
      $postvar_value = $$postvars[$var];
      $req .= "&" . $postvar_key . "=" . urlencode($postvar_value);
  }
  //-------------------------------------------- 
  // Create message to post back to PayPal... 
  // Open a socket to the PayPal server... 
  //-------------------------------------------- 
  $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  $fp = fsockopen("www.paypal.com", 80, $errno, $errstr, 30);
  //--------------------------------------------- 
  fwrite($log, "Vals: " . $invoice . " " . $receiver_email . " " . $item_name . " " . $item_number . " " . $quantity . " " . $payment_status . " " . $pending_reason . " " . $payment_date . " " . $payment_gross . " " . $payment_fee . " " . $txn_id . " " . $txn_type . " " . $first_name . " " . $last_name . " " . $address_street . " " . $address_city . " " . $address_state . " " . $address_zip . " " . $address_country . " " . $address_status . " " . $payer_email . " " . $payer_status . " " . $payment_type . " " . $notify_version . " " . $verify_sign . "\ n");
  //---------------------------------------------------------------------- 
  // Check HTTP connection made to PayPal OK, If not, print an error msg 
  //---------------------------------------------------------------------- 
  if (!$fp) {
      echo "$errstr ($errno)";
      fwrite($log, "Failed to open HTTP connection!");
      $res = "FAILED";
  }
  //-------------------------------------------------------- 
  // If connected OK, write the posted values back, then... 
  //-------------------------------------------------------- 
  else {
      fputs($fp, $header . $req);
      //------------------------------------------- 
      // ...read the results of the verification... 
      // If VERIFIED = continue to process the TX... 
      //------------------------------------------- 
      while (!feof($fp)) {
          $res = fgets($fp, 1024);
          if (strcmp($res, "VERIFIED") == 0) {
              //---------------------------------------------------------------------- 
              // If the payment_status=Completed... Get the password for the product 
              // from the DB and email it to the customer. 
              //---------------------------------------------------------------------- 
              if (strcmp($payment_status, "Completed") == 0) {
                  //-------------------------------------- 
                  // Insert Transaction details into DB. 
                  //-------------------------------------- 
                  $qry = "INSERT into paypal_table (invoice, receiver_email, item_name, item_number, quantity, payment_status, pending_reason, payment_date, payment_gross, payment_fee, txn_id, txn_type, first_name, last_name, address_street, address_city, address_state, address_zip, address_ country, address_status, payer_email, payer_status, payment_type, notify_version , verify_sign ) VALUES ( \"$invoice\", \"$receiver_email\", \"$item_name\", \"$item_number\", \"$quantity\", \"$payment_status\", \"$pending_reason\", \"$payment_date\", \"$payment_gross\", \"$payment_fee\", \"$txn_id\", \"$txn_type\", \"$first_name\", \"$last_name\", \"$address_street\", \"$address_city\", \"$address_state\", \"$address_zip \", \"$address_country\", \"$address_status\", \"$payer_email\", \"$payer_status \", \"$payment_type\", \"$notify_version\", \"$verify_sign\" ) ";
                  $result1 = mysql_query($qry, $db);
                  $qry = "INSERT into confirmation (invoice, receiver_email, payment_date, txn_id, first_name, last_name, payer_email) VALUES ( \"$invoice\", \"$receiver_email\", \"$payment_date\", \"$txn_id\", \"$first_name\", \"$last_name\", \"$payer_email\" ) ";
                  $result2 = mysql_query($qry, $db);
                  if ($result2) {
                      // ---------------- SEND MAIL FORM ----------------  
                      // send e-mail to ... $to=$payer_email;  
                      // Your subject $subject="Your upload link here"; 
                      // From $header="from: Company <$receiver_email>";  
                      // Your message $message .="Your upload link \r\n"; 
                      $message .= "Click on this link to upload your voice over script \r\n";
                      $message .= "http://www.oururl.com/upload.php?passkey=$txn_id";
                      // send email $sentmail = mail($to,$subject,$message,$header);  } }   
                      //---------------------------------------------------------------------- 
                      // If the payment_status is NOT Completed... You'll have to send the 
                      // password later, by hand, when the funds clear... 
                      //---------------------------------------------------------------------- 
                      else {
                          $message .= "Dear Customer,\n Thankyou for your order.\n\nWe will contact you with more information on sending your file when the funds have cleared.\n\nThankyou \n\n ($receiver_email)";
                          mail($payer_email, "About your order", $message, "From: ($receiver_email)\nReply-To: ($receiver_email)");
                          mail($receiver_email, "Incomplete PayPal Transaction", "An incomplete transaction requires your attention.");
                      }
                      //---------------------------------------------------------------- 
                      // ..If UNVerified - It's 'Suspicious' and needs investigating! 
                      // Send an email to yourself so you investigate it. 
                      //---------------------------------------------------------------- 
                      else {
                          mail($payer_email, "An error occurred with your payment", "Dear Customer,\n an error occurred while PayPal was processing your order. It will be investigated by customer service at the earliest opportunity.\n\nWe apologize for any inconvenience.", "From: ($receiver_email) \nReply-To: ($receiver_email)");
                          mail($receiver_email, "Invalid PayPal Transaction", "An invalid transaction requires your attention.");
                      }
                  }
              }
              //------------------------------------------- 
              // Close PayPal Connection, Log File and DB. 
              //------------------------------------------- 
              fclose($fp);
              fclose($log);
              mysql_close($db);
?>

 

Link to comment
Share on other sites

Parse error: syntax error, unexpected T_ELSE in /home/oururl/public_html/admin/ipn_res.php on line 123

 

I fixed that error but I am now getting this error:

 

Parse error: syntax error, unexpected $end in /home/oururl/public_html/admin/ipn_res.php on line 138

Link to comment
Share on other sites

The $end error means that php reached the end of your file while it was still expecting a php element. This is usually due to a missing closing } but it can also be due to a missing closing-quote or just about any php syntax with a missing closing element.

 

Once you fix all of the syntax problems in that code, you will find that $HTTP_POST_VARS probably does not work. They were depreciated a really long time ago (php4.1 in Dec 2001), turned off by default in php5, finally throw a depreciated error in php5.3, and scheduled to be completely removed in the next major release of php (should that occur in any of our lifetimes.) You need to use $_POST instead of $HTTP_POST_VARS.

Link to comment
Share on other sites

The $end error means that php reached the end of your file while it was still expecting a php element. This is usually due to a missing closing } but it can also be due to a missing closing-quote or just about any php syntax with a missing closing element.

 

Once you fix all of the syntax problems in that code, you will find that $HTTP_POST_VARS probably does not work. They were depreciated a really long time ago (php4.1 in Dec 2001), turned off by default in php5, finally throw a depreciated error in php5.3, and scheduled to be completely removed in the next major release of php (should that occur in any of our lifetimes.) You need to use $_POST instead of $HTTP_POST_VARS.

 

I figured out the $end error and fixed it. I also ran my config file and found a database connection error that I fixed.

 

QUESTION: Would I just replace the $HTTP_POST_VARS with the $_POST or would it be $POST_VARS? Or is there much more to it than just a simple replacement?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.