PayPal IPN - 由对等方重置连接

时间:2014-03-08 09:45:41

标签: php paypal paypal-ipn

我现在一直在寻找几个小时,但仍未找到任何有用的东西。

我正在使用PayPal进行即时支付系统,我必须使用Sandbox帐户进行测试,但目前它给我一些问题。我一直收到这个错误:

PHP Warning: stream_get_contents(): SSL: Connection reset by peer in /home/qhpqrbqp/public_html/ipn.php on line 67

这是ipn.php

中的第67行
$res=stream_get_contents($fp, 1024);

ipn.php的完整代码..

<?php
require 'conf.php';
require_once('class.phpmailer.php');
header('Content-Type: text/html; charset=iso-8859-1');

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
 $value = urlencode(stripslashes($value));
 $req .= "&$key=$value";
}
$seller = mysql_real_escape_string($_GET[seller]);
// post back to PayPal system to validate
$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";
$header .= "Conection: Close";
$header .= "Host: www.paypal.com\r\n"; 
/*
$header="POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .="Content-Type: application/x-www-form-urlencoded\r\n";
$header .="Host: www.paypal.com\r\n"; 
$header .="Connection: close\r\n\r\n";
*/
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = mysql_real_escape_string($_POST['txn_id']);
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$date = date("F j, Y - H:i");
$type = "";
$time_end = time()+(86400*30);

error_log($payer_email);

if($_GET[type] == "marketer"){
$total = "14.95";
$type = "marketer";
}elseif($_GET[type] == "elite"){
$total = "29.95";
$type = "elite";
}elseif($_GET[type] == "legendary"){
$total = "49.95";
$type = "legendary";
}

function generateRandomString($length = 10) 
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);

$res=stream_get_contents($fp, 1024);

if (strcmp ($res, "VERIFIED") == 0) {

if($payment_status=='Completed'){

$txn_id_check = mysql_query("SELECT `tx_id` FROM `transactions` WHERE `tx_id`='".$txn_id."'");
if(mysql_num_rows($txn_id_check)!=1){

  if($receiver_email=='mypaypalemail@email.me'){

   if($payment_amount==''.$total.'' && $payment_currency=='USD'){

    //purchase succesful
    mysql_query("INSERT INTO `transactions` (tx_id, payment_email, amount, item, `date`)VALUES('".$txn_id."','".$payer_email."','".$payment_amount."','".$item_name."','".$date."')") or die(mysql_error());

    // create account

    $user = explode("@",$payer_email);
    $username = $user[0].''.generateRandomString(5);

    mysql_query("INSERT INTO `users` (username, password, email, time_end, type)VALUES('".$username."','".md5($item_number)."','".$payer_email."','".$time_end."','".$type."')") or die(mysql_error());

    // Send verification email

    $mail             = new PHPMailer(); // defaults to using php "mail()"
    $mail->IsSendmail(); // telling the class to use SendMail transport

$body             = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
   <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>PHPMailer Test</title>
  </head>
  <body>
  <div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
  <h1>Thanks for buying membership!</h1>

    </div>
   </body>
  </html>';
$body             = str_replace("[\]", "", $body);
$mail->AddReplyTo("noreply@mysite.me","My name");
$mail->SetFrom('noreply@mysite.me', 'My name');
$mail->AddReplyTo("noreply@mysite.me","My name");
$address = $payer_email;
$mail->AddAddress($address, "Member");
$mail->Subject    = "Thanks for your purchase!";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);


}
}
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
     echo "An error has occured";
}
fclose ($fp);
}

?>

1 个答案:

答案 0 :(得分:0)

尝试在标头中使用HTTP / 1.1。这帮助我解决了你遇到的问题。