Ipn PayPal server giving 301 or 302 errors

RB79
Contributor
Contributor

Trying to use ipn to insert data into database with this code and it fails to validate with this message 

02/15/2024 3:39 PM] - FAIL: IPN Validation Failed.
IPN POST Vars
IPN Response from Paypal Server:
 HTTP/1.1 301 Moved Permanently
<?php require_once('Connections/connsqli.php'); ?>
<?php require_once('webassist/mysqli/queryobj.php'); ?>
<?php
 
// STEP 1: Read POST data
 
// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST
// reading raw POST data from input stream instead. 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
     $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $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 back to paypal to validate
 
$ch = curl_init('https://ipnpb.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 that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as 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 and act accordingly
 
if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
 
    // assign posted variables to local variables
    $txn_id = $_POST['txn_id'];
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
    $payer_email = $_POST['payer_email'];
	$address_street = $_POST['address_street'];
	$address_city = $_POST['address_city'];
	$address_state = $_POST['address_state'];
	$address_zip = $_POST['address_zip'];
    $item_name = $_POST['item_name'];
    $quantity = $_POST['quantity'];
    $payment_amount = $_POST['mc_gross'];
	$mc_fee = $_POST['mc_fee'];
	$mc_shipping = $_POST['mc_shipping'];


    // <---- HERE you can do your INSERT to the database
if (true) {
  $InsertQuery = new WA_MySQLi_Query($connsqli);
  $InsertQuery->Action = "insert";
  $InsertQuery->Table = "paypal_transactions";
  $InsertQuery->bindColumn("transaction_ID", "i", "txn_id", "WA_DEFAULT");
  $InsertQuery->bindColumn("first_name", "s", "first_name", "WA_DEFAULT");
  $InsertQuery->bindColumn("last_name", "s", "last_name", "WA_DEFAULT");
  $InsertQuery->bindColumn("email1", "s", "payer_email", "WA_DEFAULT");
  $InsertQuery->bindColumn("address_street", "s", "address_street", "WA_DEFAULT");
  $InsertQuery->bindColumn("city", "s", "address_city", "WA_DEFAULT");
  $InsertQuery->bindColumn("state", "s", "address_state", "WA_DEFAULT");
  $InsertQuery->bindColumn("zip", "s", "address_zip", "WA_DEFAULT");
  $InsertQuery->bindColumn("item_name", "s", "item_name", "WA_DEFAULT");
  $InsertQuery->bindColumn("quantity", "s", "quantity", "WA_DEFAULT");
  $InsertQuery->bindColumn("payment_gross", "s", "mc_gross", "WA_DEFAULT");
  $InsertQuery->bindColumn("mc_shipping", "s", "mc_shipping", "WA_DEFAULT");
  $InsertQuery->bindColumn("payment_fee", "s", "mc_fee", "WA_DEFAULT");
  $InsertQuery->saveInSession("");
  $InsertQuery->execute();
  $InsertGoTo = "";
  if (function_exists("rel2abs")) $InsertGoTo = $InsertGoTo?rel2abs($InsertGoTo,dirname(__FILE__)):"";
  $InsertQuery->redirect($InsertGoTo);
}
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}

 

Login to Me Too
1 REPLY 1

Kavyar
Moderator
Moderator

Good day @RB79 

 

Thank you for posting to the PayPal community.

 

This error typically indicates a redirection issue.

 

I would suggest to please contact your website developer or the support of the third party platform (eCommerce) regarding the integration code. It is the responsibility of the merchant to handle their internal database and website code.

 

If you notice many Failed or Retrying entries, it is likely that the reason behind the delay in IPN failure to send HTTP 200 OK responses. To resolve this issue, you should review your server logs to identify why the required HTTP 200 OK responses are not being sent and fix your listener accordingly. Once your listener starts sending the required HTTP 200 OK responses, PayPal will post IPNs from the faster-cycling server.

 

If you encounter any issues with receiving the IPN or validating it, kindly create an MTS ticket via the following URL  - https://www.paypal-support.com/s/?language=en_US  .Please ensure that you provide detailed information and error details when submitting the ticket.

 

Sincerely,

Kavya

PayPal MTS

 

If this post or any other was helpful, please enrich the community by giving kudos or accepting it as a solution.

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.