我有一个paypal ipn listener ipn.php,它链接到数据库并使用通知信息更新通知表,然后通过电子邮件将密钥发送给付款人电子邮件。
这是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 = "IPN@example.com";
$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;
}
?>
问题是,当我通过沙箱发送时,它会更新数据库并通过电子邮件发送密钥,但是当我尝试使用代码时,它没有做任何事情......
我不知道问题是什么。
答案 0 :(得分:0)
收到任何错误?检查IPN历史记录日志?如果进行API调用,请确保使用LIVE凭证,如果仍有问题,请将帐号或电子邮件地址告诉我,或者将票证提交至paypal.com/mts
答案 1 :(得分: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;
}
}
}