更新1:
将$res
写入文本文件只会返回单词VERIFIED
:
<?php
/*
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("PayPal") or die(mysql_error());
*/
// 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";
}
// 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";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
$ourFileName = "payment_successful.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $res);
fclose($ourFileHandle);
}
else if (strcmp ($res, "INVALID") == 0) {
$ourFileName = "payment_failed.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $res);
fclose($ourFileHandle);
}
}
fclose ($fp);
}
?>
原始问题:
我有以下IPN(即时付款通知)脚本,该脚本有效,即如果成功则创建成功文件,如果失败则创建失败文件。
paypal是否将帖子值返回到IPN文件,以便我可以确定哪个付款成功或哪个付款失败了?
如果是,我如何访问这些值?
如果不是,我如何确定哪些付款被接受或拒绝?
这是我目前拥有的IPN文件中的脚本:
<?php
// 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";
}
// 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";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
$ourFileName = "payment_successful.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
}
else if (strcmp ($res, "INVALID") == 0) {
$ourFileName = "payment_failed.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
}
}
fclose ($fp);
}
?>
答案 0 :(得分:3)
您的通用脚本中有答案。
Paypal将在字符串中返回VERIFIED或INVALID字样。
($res, "VERIFIED") == 0
一个好方法是将$ res的值写入您的日志,您将在最后看到返回的结果。
确保您也使用沙箱进行测试。
另外在http://x.com有一个论坛(他们为这个域支付了多少钱?)
FWIW,Paypal的IPN是PITA。
答案 1 :(得分:0)
我以这种方式实施了我的
foreach ($_POST as $key => $value)
{
$data[$key] = $value;
}
做一个echo '<pre>'
。print_r($ data,true)。'</pre>'
;
会产生这样的东西:
cmd=_notify-validate
test_ipn=1
payment_type=echeck
payment_date=22:21:28 Mar 24, 2011 PDT
payment_status=Completed
address_status=confirmed
payer_status=verified
first_name=John
last_name=Smith
payer_email=buyer@paypalsandbox.com
payer_id=TESTBUYERID01
address_name=John+Smith
address_country=United+States
address_country_code=US
address_zip=95131
address_state=CA
address_city=San+Jose
address_street=123%2C+any+street
business=seller@paypalsandbox.com
receiver_email=seller@paypalsandbox.com
receiver_id=TESTSELLERID1
residence_country=US
item_name=something
item_number=AK-1234
quantity=1
shipping=3.04
tax=2.02
mc_currency=USD
mc_fee=0.44
mc_gross=12.34
txn_type=web_accept
txn_id=28325521
notify_version=2.1
custom=xyz123
invoice=abc1234
charset=windows-1252
verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31AM3F5ODR-2hb2fIsWPHepIEPzAwg