2013-11-29 2 views
1

У меня есть PayPal ipn listener ipn.php, который связан с базой данных и обновляет таблицу уведомлений с информацией о уведомлении, а затем отправляет по электронной почте ключ к электронной почте плательщика.PayPal IPN Listener Работа с песочницей, но не вживую

Вот ipn.php

<?php  

include("class.phpmailer.php"); 
include("class.smtp.php"); 

include("config.php"); 

    //Send an empty HTTP 200 OK response to acknowledge receipt of the notification 

ini_set("log_errors", "On"); 
ini_set("error_log", "error.log"); 

$con = mysql_connect($dbhost,$dbuser,$dbpass); 

if($con == false){ echo "Could not connect to mysql: " . mysql_error();} 
else{echo "Connected to database successfully!";} 

header('HTTP/1.0 200 OK'); 
header('Connection: close'); 


$item_name  = $_POST['item_name'] != null ? $_POST['item_name'] : "none"; 
$item_number  = $_POST['item_number'] != null ? $_POST['item_number'] : "none"; 
$payment_status = $_POST['payment_status'] != null ? $_POST['payment_status'] : "none"; 
$payment_amount = $_POST['mc_gross'] != null ? $_POST['mc_gross'] : "none"; 
$payment_currency = $_POST['mc_currency'] != null ? $_POST['mc_currency'] : "none"; 
$txn_id   = $_POST['txn_id'] != null ? $_POST['txn_id'] : "none"; 
$receiver_email = $_POST['receiver_email'] != null ? $_POST['receiver_email'] : "none"; 
$payer_email  = $_POST['payer_email'] != null ? $_POST['payer_email'] : "none"; 

// Build the required acknowledgement message out of the notification just received 
$req = 'cmd=_notify-validate';    // Add 'cmd=_notify-validate' to beginning of the acknowledgement 

foreach ($_POST as $key => $value) {   // Loop through the notification NV pairs 
    $value = urlencode(stripslashes($value)); // Encode these values 
    $req .= "&$key=$value";     // Add the NV pairs to the acknowledgement 
} 

// Set up the acknowledgement request headers 
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";  // HTTP POST request 
$header .= "Host: www.paypal.com\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

// Open a socket for the acknowledgement request 
$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30); 

// Send the HTTP POST request back to PayPal for validation 
fputs($fp, $header . $req); 


    error_log("reached"); 

    $res = stream_get_contents($fp,1024);   // Get the acknowledgement response 

    while(!feof($fp)){ 
     $res = trim(fgets($fp,1024)); 

     if(empty($res)){ 
      break; 
     } 
    } 

    $res = trim(fgets($fp,1024)); 

    if (strcmp ($res, "VERIFIED") == 0) { // Response contains VERIFIED - process notification 


     $sql = "INSERT INTO notifications VALUES(DEFAULT, '$item_name','$item_number','$payment_status','$payment_amount','$payment_currency','$txn_id','$receiver_email','$payer_email')"; 

     mysql_select_db($dbname,$con); 

     if(!mysql_query($sql)){ 
      error_log(mysql_error()); 
     } 

     if(strcmp($payment_status,"Completed") == 0){ 
      mysql_select_db($dbname, $con); 
      $key = get_new_key(); 
      $sql = ""; 
      $sql = "INSERT INTO activationkeys VALUES(DEFAULT, '$key', '0', $item_number)"; 
      if(!mysql_query($sql)){ 
       error_log("Could not execute query: " . mysql_error()); 
      } 

      $mail = new PHPMailer(true); 

      //Send mail using gmail 
      if($config_gmail){ 
       $mail->IsSMTP(); 
       $mail->SMTPAuth = true; 
       $mail->SMTPSecure = "tls"; 
       $mail->Host = "smtp.gmail.com"; 
       $mail->Port = 587; 
       $mail->Username = $config_gmail_username; 
       $mail->Password = $config_gmail_password; 
      } 

      //Typical mail data 
      $mail->AddAddress($payer_email,"Donator"); 
      $mail->SetFrom($config_merchant_domain,$config_merchant_name); 
      $mail->Subject = "You're Donation Key"; 
      $mail->Body = "Hey, your payment was successful.\nHere's your key: " . $key; 

      if(!$mail->send()){ 
       error_log("Message could not be sent: " . $mail->ErrorInfo); 
      } 


     } 
     mysql_close($con); 



     // Authentication protocol is complete - OK to process notification contents 

     // Possible processing steps for a payment include the following: 

     // Check that 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 

    } 
    else if (strcmp ($res, "INVALID") == 0) { //Response contains INVALID - reject notification 

     // Authentication protocol is complete - begin error handling 
     // Send an email announcing the IPN message is INVALID 
     $mail_From = "[email protected]"; 
     $mail_To  = "Your-eMail-Address"; 
     $mail_Subject = "INVALID IPN"; 
     $mail_Body = $req; 

     mail($mail_To, $mail_Subject, $mail_Body, $mail_From); 
    } 

fclose($fp); // Close the file 

function get_new_key(){ 
    $keycharacters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    $key = ""; 
    for($i = 0; $i < 10; $i++){ 
     $key .= $keycharacters[rand(0,strlen($keycharacters)-1)]; 
    } 
    return $key; 
} 

?> 

Проблема, когда я посылаю через песочницу обновляет Databse штраф и электронной почты ключ, но когда я пытаюсь использовать код жить не делает ничего ...

Я в затруднении относительно проблемы.

ответ

0

Получить любую ошибку? Проверяли журналы журналов IPN? Если вы делаете вызовы API, убедитесь, что используете LIVE Credentials, если проблема по-прежнему вызывает мне номер учетной записи или адрес электронной почты, или поднимите билет на paypal.com/mts

0

Мне удалось исправить это, проблема заключалась в том, как я читал ответ, теперь я использую;

if (!$fp) { 
     error_log("could not open socket"); 
    } else { 
     fputs ($fp, $header . $req); 
     while (!feof($fp)) { 
      $res = fgets ($fp, 1024); 
      if (strcmp ($res, "VERIFIED") == 0){ 
       break; 
      } 
     } 
    } 
Смежные вопросы