我正在尝试在我的项目中实现PayPal,并设置IPN
(通过PayPal沙箱)以接收付款通知。我创建了一个dev
帐户,并有两个email
地址。一个用于卖方([email protected]
),另一个用于买方。我将php
文件在线存储在我的站点中,并在卖方帐户上配置ipn url
,如下所示:
http://example.org/someFolder/ipnsample.php
当我尝试模拟
IPN
时,会收到一条消息IPN was sent and the handshake was verified.
,因此配置似乎还可以,但是我无法收到任何邮件通知。PHP:
// Send an empty HTTP 200 OK response to acknowledge receipt of the notification
header('HTTP/1.1 200 OK');
// Assign payment notification values 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 = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
// 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";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// Open a socket for the acknowledgement request
// Send the HTTP 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
if (strcmp ($res, "VERIFIED") == 0) { // Response contains VERIFIED - process notification
// Send an email announcing the IPN message is VERIFIED
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "VERIFIED IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
else if (strcmp ($res, "INVALID") == 0) {
// Authentication protocol is complete - begin error handling
// Send an email announcing the IPN message is INVALID
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "INVALID IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
}
fclose($fp); // Close the file
HTML(购买表单):
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="item_name" value="Trex hide">
<input id="PayPalPrice1" type="hidden" name="amount" value="">
<button name="submit" class="btn btn-lg btn-success "> Checkout With <b> PayPal </b> </button>
</form>
最佳答案
首先检查邮件方法是否正常工作:
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "VERIFIED IPN";
$mail_Body = $req;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
只需将代码放在侦听器php文件和刷新页面上即可。如果邮件方法可以正常工作,只需在while()之前使用serialize($ fp)方法调试变量$ fp
$mail_From = "[email protected]";
$mail_To = "[email protected]";
$mail_Subject = "VERIFIED IPN";
$mail_Body = serialize($fp);
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
关于php - PayPal IPN-无法接收电子邮件通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33993906/