I am still having trouble with paypal IPN in my Marketpress shopping cart...it requires code added f

I am still having trouble with paypal IPN in my Marketpress shopping cart...it requires code added for multiple paypal return URLs. How can we add this code?

  • Jack Kitterhing

    Hi there @megbenedicte

    I hope you are well today and thank you for your question.

    You need an IPN forwarding script? Have you seen this thread here https://premium.wpmudev.org/forums/topic/multiples-ipn-dynamically-setting-the-notification-url
    ? That has some good details with setting it up, let me know if you need some clarification etc :slight_smile:

    Thank you!

    Kind Regards
    Jack.

  • megbenedicte

    Hey,

    I'm getting this error

    There was a problem connecting to PayPal to setup your purchase. Please try again.
    10002 - Security error - Security header is not valid

    I created a central IPN forwarder at the following URL

    http://newearthcentral.com/ipn-forwarder.php

    This is the file that I downloaded
    https://premium.wpmudev.org/forums/topic/multiples-ipn-dynamically-setting-the-notification-url#post-165002

    and uploaded it to the root of my sub-domain(community.newearthcentral.com) and root folder of the main website.

    I've membership plugin on community.newearthcentral.com and Marketpress on newearthcentral.com

    I updated the wp-config files with the following code

    define('PSTS_IPN_PASSWORD', 'mypassword');
    define('PSTS_PYPL_PREFIX', 'app2');

    Updated the other wp-config file with

    define('PSTS_IPN_PASSWORD', 'mypassword');
    define('PSTS_PYPL_PREFIX', 'app1');

    My IPN forwarding code is

    //Our password. Check for this in your script to make sure it's from us ($_POST['inc_pass']). Do not change!
    define('INC_PASS', 'mypassword');
    
    // An array of prefix to search for, domain and path to post to, and whether we're using sandbox or not for applications
    $apps = array(
       /* App 1 */
      array('prefix' => 'app1',
            'domain' => 'newearthcentral.com',
            'path'   => '/wp-admin/admin-ajax.php?action=psts_pypl_ipn',
            'live'   => true),
    
      /* App2 */
      array('prefix' => 'app2',
            'domain' => 'community.newearthcentral.com',
            'path'   => '/wp-admin/admin-ajax.php?action=psts_pypl_ipn',
            'live'   => true)
    );
    
    /////////////////////////////////////////////////////////////////////
    ///////////////////////* Begin Script *//////////////////////////////
    if (isset($_POST['payment_status']) || isset($_POST['txn_type'])) {
    
      //figure out where to send it from the prefix and if sandbox
      foreach ($apps as $app) {
        if (strpos($_POST['rp_invoice_id'], $app['prefix']) !== false || strpos($_POST['custom'], $app['prefix']) !== false || strpos($_POST['invoice'], $app['prefix']) !== false) {
          $appDomain = $app['domain'];
          $appPath = $app['path'];
          $live = $app['live'];
        }
      }
    
      //did we find somewhere to send it?
      if (!$appDomain || !$appPath) {
        $error = "No valid prefix: \t".http_build_query($_POST);
        write_to_log($error);
    		exit;
      }
    
      if ($live)
        $domain = 'ssl://www.paypal.com';
    	else
    		$domain = 'ssl://www.sandbox.paypal.com';
    
      ////validate the IPN with PayPal
    	$req = 'cmd=_notify-validate';
    	foreach ($_POST as $k => $v) {
    		if (get_magic_quotes_gpc()) $v = stripslashes($v);
    		$req .= '&' . $k . '=' . urlencode($v);
    	}
    
    	$header = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n"
    			. 'Content-Type: application/x-www-form-urlencoded' . "\r\n"
    			. 'Content-Length: ' . strlen($req) . "\r\n"
    			. "\r\n";
    
    	@set_time_limit(60);
    	if ($conn = @fsockopen($domain, 443, $errno, $errstr, 30)) {
    		fputs($conn, $header . $req);
    		socket_set_timeout($conn, 30);
    
    		$response = '';
    		$close_connection = false;
    		while (true) {
    			if (feof($conn) || $close_connection) {
    				fclose($conn);
    				break;
    			}
    
    			$st = @fgets($conn, 4096);
    			if ($st === false) {
    				$close_connection = true;
    				continue;
    			}
    
    			$response .= $st;
    		}
    
    		$error = '';
    		$lines = explode("\n", str_replace("\r\n", "\n", $response));
    		// looking for: HTTP/1.1 200 OK
    		if (count($lines) == 0) $error = 'Response Error: Header not found while verifying with PayPal';
    		else if (substr($lines[0], -7) != ' 200 OK') $error = 'Response Error: Unexpected HTTP response while verifying with PayPal';
    		else {
    			// remove HTTP header
    			while (count($lines) > 0 && trim($lines[0]) != '') array_shift($lines);
    
    			// first line will be empty, second line will have the result
    			if (count($lines) < 2) $error = 'Response Error: No content found in transaction response while verifying with PayPal';
    			else if (strtoupper(trim($lines[1])) != 'VERIFIED') $error = 'Response Error: Unexpected transaction response while verifying with PayPal: '.$lines[1].$req;
    		}
    
    		if ($error != '') {
          //There was an issue with the paypal verification, log the error
          write_to_log($error);
          header("HTTP/1.1 503 Service Unavailable");
    			exit;
    		}
    	} else {
        //error connecting
        $error = 'Could not make a connection with fsockopen while verifying with PayPal: '.$errstr;
        write_to_log($error);
        header("HTTP/1.1 503 Service Unavailable");
    		exit;
      }
    
      //// Now POST the IPN variables to the proper script as it is now verified
    
      //add our password
      $req .= "&inc_pass=".INC_PASS;
    
    	$header = "POST $appPath HTTP/1.1\r\n"
    			. "Host: $appDomain\r\n"
    			. 'Content-Type: application/x-www-form-urlencoded' . "\r\n"
    			. 'Content-Length: ' . strlen($req) . "\r\n"
    			. "Connection: close\r\n\r\n";
    
    	@set_time_limit(60);
    	if ($conn = @fsockopen($appDomain, 80, $errno, $errstr, 30)) {
    		fputs($conn, $header . $req);
    		socket_set_timeout($conn, 30);
    
    		$response = '';
    		$close_connection = false;
    		while (true) {
    			if (feof($conn) || $close_connection) {
    				fclose($conn);
    				break;
    			}
    
    			$st = @fgets($conn, 4096);
    			if ($st === false) {
    				$close_connection = true;
    				continue;
    			}
    
    			$response .= $st;
    		}
    
    		$error = '';
    		$lines = explode("\n", str_replace("\r\n", "\n", $response));
    		// looking for: HTTP/1.1 200 OK
    		if (count($lines) == 0)
          $error = "Response Error: Header not found: \t".http_build_query($_POST);
    		else if (substr($lines[0], -7) != ' 200 OK')
          $error = 'Could not contact '.$appDomain.$appPath." to send the IPN: {$lines[0]} \t".http_build_query($_POST);
    
    		if ($error != '') {
          //There was an issue with the connection, log the error
          write_to_log($error);
          header("HTTP/1.1 503 Service Unavailable");
    			exit;
    		}
    	} else {
        //error connecting
        $error = "Could not make a connection with fsockopen: $errstr\t".http_build_query($_POST);
        write_to_log($error);
        header("HTTP/1.1 503 Service Unavailable");
    		exit;
      }
    
      //Yeay! Everything worked! Lets log it anyway
      $message = "Successfuly sent to ".$appDomain.$appPath.": \t".http_build_query($_POST);
      write_to_log($message);
      exit;
    
    } else {
    	// Did not find expected POST variables. Possible access attempt from a non PayPal site.
    	header("HTTP/1.1 401 Authorization Required");
    	echo 'Error: Missing POST variables. Identification is not possible.';
    	exit;
    }
    
    function write_to_log($error) {
      //create filename for each month
      $filename = 'logs/IPN_Log_' . date('Y_m') . '.log';
    
      //add timestamp to error
      $message = gmdate('[Y-m-d H:i:s] ') . $error;
    
      //write to file
      $contents = @file_get_contents($filename);
      file_put_contents($filename, trim($contents)."\n".$message);
    }
    ?>

    what am I doing wrong?

  • megbenedicte

    Even after following all the steps I still get this error message.
    My current IPN URL is http://newearthcentral.com/ipn-forwarder.php

    Please check your server that handles PayPal Instant Payment Notifications
    (IPN). IPNs sent to the following URL(s) are failing:

    http://newearthcentral.com/paymentreturn/paypalexpress
    http://newearthcentral.com/paymentreturn

    If you do not recognize this URL, you may be using a service provider that
    is using IPN on your behalf. Please contact your service provider with the
    above information. If this problem continues, IPNs may be disabled for your
    account.

    Thank you for your prompt attention to this issue.

    Thanks,

    PayPal

  • Aaron

    Hey there, first off IPN's are not necessary really for the MarketPress paypal express gateway. Additionally, MP already sets the IPN url dynamically in code, you don't have to enter one or turn them on in your paypal account.

    Now that that's settled, if you are getting this error:

    There was a problem connecting to PayPal to setup your purchase. Please try again.
    10002 - Security error - Security header is not valid

    It means the api key details you entered in the gateway settings are incorrect. Either they are invalid, mistyped, have blank space, or (this is common) you have a mismatch between live and sandbox credentials. If you have sandbox on, then you need sandbox api keys. If you switch to live mode, you need live keys from your live paypal account.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.