我正在使用 Paypal IPN。我的应用程序设法收到了来自 Paypal 的第一个通知,但是,在发送回发后,无论回发是有效还是无效,Paypal 似乎都没有回复。
我从贝宝开发者网站的示例代码中获得的大部分代码:
public function process(){
// Read the notification from PayPal and create the acknowledgement response
$req = 'cmd=_notify-validate'; // add 'cmd' to beginning of the acknowledgement you send back to PayPal
//$raw = file_get_contents("php://input");
foreach ($_POST as $key => $value) { // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode the 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";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";
//Open a socket for the acknowledgement request
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// Post request back to PayPal for validation
fputs ($fp, $header . $req);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
//$res=stream_get_contents($fp, 1024);
$this->emailtest(print_r($_POST,true) .'<br /><br />' . $header.$req);
if (strcmp ($res, "VERIFIED") == 0) { // Response is VERIFIED
$this->emailtest('VERIFIED');
// Send an email announcing the IPN message is VERIFIED
//$mail_From = "IPN@example.com";
//$mail_To = "Your-eMail-Address";
//$mail_Subject = "VERIFIED IPN";
//$mail_Body = $req;
//mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
// Notification protocol is complete, OK to process notification contents
// Possible processing steps for a payment might 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 is INVALID
$this->emailtest('INVALID');
// Notification protocol is NOT 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);
}
我的标题看起来像这样:[来自连接 $header.$req]:
Content-Type: application/x-www-form-urlencoded
Content-Length: 960
Connection: close
cmd=_notify-validate&mc_gross=30.00&protection_eligibility=Eligible&address_status=confirmed&payer_id=V6AHFXYY8YVFU&tax=0.00&address_street=1+Main+St&payment_date=09%3A20%3A02+Jul+06%2C+2013+PDT&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=LY&mc_fee=1.37&address_country_code=US&address_name=LY+Y¬ify_version=3.7&custom=&payer_status=verified&business=test-facilitator%40hotmail.com&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=ALty0ZLnfvwh23G8AkAyx34uB78KAtFMUzdUnz11sJNJNFF4NI8JnFNu&payer_email=test%40hotmail.com&txn_id=2VB92036BK537324F&payment_type=instant&last_name=Y&address_state=CA&receiver_email=test-facilitator%40hotmail.com&payment_fee=&receiver_id=82EXQSKTSV6ZN&txn_type=web_accept&item_name=Chef&mc_currency=SGD&item_number=1&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=Chef&payment_gross=&shipping=0.00&ipn_track_id=1dc5a2e7b3934
提前致谢!